PROGRAM THE FOLLOWING IN JAVA PROGRAMMINHG. JAVA PROGRAMMING Question 2 – 25 Poi
ID: 3701808 • Letter: P
Question
PROGRAM THE FOLLOWING IN JAVA PROGRAMMINHG.
JAVA PROGRAMMING
Question 2 – 25 Points
Write a function check_all_equal(int[ ] array)that:
Takes as input array of integers. The array can have any length.
If the array length is 0, the function returns Boolean value true.
If the array length is not 0:
The function returns trueif all values in the array are equal to each other.
The function returns falseotherwise.
For example:
If a1 = {20, 20, 20, 20, 20}, then check_all_equal(a1)returns true, since all values equal 20.
If a1 = {1, 1, 1, 2}, then check_all_equal(a1)returns false, since not all values are equal to each other.
If a1 = { }, then check_all_equal(a1)returns true, because the array is empty.
Question 3 – 25 Points
Write a function positions (int[ ] numbers, int N)that returns an arrayof integers, containing all positions innumberswhere the value stored is NOT equal to N.
If Nappears in all positions, or if numbersis an empty array, the function should return an empty array. Note: you can create an empty array by giving it a size of 0 when you declare it.
IMPORTANT: MAKE SURE THAT YOUR FUNCTION RETURNS AN ARRAY, NOT AN ARRAY LIST.
For Example:
If a1 = {5, 20, 20, 10, 40, 20}, then positions(a1, 20)returns array {0, 3, 4}, because value 20 doesNOTappear at positions 0, 3 and 4 of a1.
If a1 = {2, 1, 2, 2}, then positions(a1, 2)returns array {1}, because value 2 does NOTappear at positions 1 of a1.
If a1 = {3, 3, 3, 3}, then positions(a1, 3)returns the empty array{ }, because value 3 appears in every position of a1.
If a1 = { }, then positions(a1, 2)returns the empty array { }, because a1 is empty.
Question 4 – 25 Points
Write a function select_strings(ArrayList<String> strings)that returns an ArrayListof strings, containing every string in stringsthat has at least one numerical digit in its characters. A numerical digit is any of the characters in string “0123456789”.
IMPORTANT: MAKE SURE THAT YOUR FUNCTION RETURNS AN ARRAY LIST, NOT AN ARRAY.
For example:
If a1 = [“hello”, “123”, “az7”, “az”], thenselect_strings(a1)returns array list [“123”, “az7”], because “123” and “az7” are the only strings in a1 that contain a numerical digit.
If a1 = [“{hello”, “az”], then select_strings(a1)returns the empty array list [ ], because none of the strings ina1contains a numerical digit.
BONUS QUESTION - 5 POINTS
Write a function most_frequent (int[] array), that returns the value in arraythat occurs the most times.
For Example:
If a1 = {5, 20, 10, 40, 20}, then most_frequent(a1)returns 20, because value 20 appears twice in a1, and no other value appears more than once.
If a1 = {2, 2, 1, 2, 1, 1, 1}, then most_frequent(a1)returns 1, because value 1 appears four times in a1, and no other value appears more than three times.
If there is a tie among multiple values for the highest number of occurrences, your function should return the last of those values that are tied.
For example: if a1 = {2, 2, 1, 7, 3, 7}, then most_frequent(a1)should return 7, since both 2 and 7 appear twice, but the last 7 occurs after the last 2.
Explanation / Answer
If you post more than 1 question, as pe chegg guidelines I have to solve only first question.
Ques 2.
public boolean check_all_equal(int[ ] array)
{
// if array is empty
if( array.length == 0 )
return true;
// set x as first element
int x = array[0];
int i;
// traverse through the array
for( i = 1; i < array.length ; i++ )
// if x is not same as current element
if( x != array[i] )
return false;
return true;
}
Ques 3.
int[] positions (int[ ] numbers, int N)
{
// if numbers is empty
if( numbers.length == 0 )
// return an empty array
return new int[0];
// store the number of elements not equal to N
int size = 0;
int i;
// get the number of elements not equal to N
for( i = 0 ; i < numbers.length ; i++ )
// if current element is not equal to N
if( numbers[i] != N )
size++;
// if each element is same as N
if( size == numbers.length )
// return an empty array
return new int[0];
int[] arr = new int[size];
int index = 0;
for( i = 0 ; i < numbers.length ; i++ )
// if current element is not equal to N
if( numbers[i] != N )
arr[index++] = numbers[i];
return arr;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.