1. Define the following function…public static boolean isEven(int number) This f
ID: 3671183 • Letter: 1
Question
1. Define the following function…public static boolean isEven(int number)
This function takes an integer input and returns true if the input is even, false otherwise
2. Define the following function, using isEven()public static int numberOfEvens(int[] input)
This function takes an array and determines the count of even numbers seen in the array
3. Define the following function, using numberOfEvens()public static int numberOfOdds(int[] input)
This function takes an array and determines the count of odd numbers seen in the array
To build this, use this formula: totalOdds = input.length – numberOfEvens().
4. Define the following function, using isEven()public static double median(int[] input)This function determines the median of a given array, as defined here:If input has an odd number of elements {1,2,3}, return the middle element (2).
Use isEven() to determine this.
If input has an even number of elements {1,2,3,4}, return the average of the middle two elements ((2+3)/2 = 2.5)
Use isEven() to determine this.
Return to your main() in the Exercise2.java driver and call each of the above functions to test them. Use the console to report and display the output of your tests; remember that you’re graded on building the code above and building the driver code to test these functions. If unsure, see the first sections example on this.
Explanation / Answer
/**
* @author Srinivas Palli
*
*/
public class Exercise2 {
/**
* @param args
*
*/
public static void main(String[] args) {
int data[] = { 1, 2, 3, 4, 5, 6, 9 };
try {
System.out.println("Is the first arrray element even? "
+ isEven(data[0]));
System.out
.println("Is the third element even? " + isEven(data[2]));
System.out.println("No of evens in data: " + numberOfEvens(data));
System.out.println("No of odds in data: " + numberOfOdds(data));
System.out.println("Median Value : " + median(data));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* function takes an integer input and returns true if the input is even,
* false otherwise
*
* @param number
* @return
*/
public static boolean isEven(int number) {
if (number % 2 == 0)
return true;
else
return false;
}
/**
* function takes an array and determines the count of even numbers seen in
* the array
*
* @param input
* @return
*/
public static int numberOfEvens(int[] input) {
int evenCount = 0;
for (int i = 0; i < input.length; i++) {
if (isEven(input[i]))
evenCount++;
}
return evenCount;
}
/**
* function takes an array and determines the count of odd numbers seen in
* the array To build this, use this formula: totalOdds = input.length –
* numberOfEvens()
*
* @param input
* @return
*/
public static int numberOfOdds(int[] input) {
int totalOdds = input.length - numberOfEvens(input);
return totalOdds;
}
/**
* function determines the median of a given array
*
* @param input
* @return
*/
public static double median(int[] input) {
double median = 0.0d;
if (isEven(input.length)) {
median = (input[input.length / 2] + input[(input.length / 2) + 1]) / 2.0d;
} else {
median = input[input.length / 2];
}
return median;
}
}
OUTPUT:
Is the first arrray element even? false
Is the third element even? false
No of evens in data: 3
No of odds in data: 4
Median Value : 4.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.