Section Two: Statistical Algorithms 1. Find the section of code in the driver en
ID: 3671203 • Letter: S
Question
Section Two: Statistical Algorithms
1. Find the section of code in the driver entitled “SectionTwo”, and build the following functions next to it a. Call each of the functions from the sectionTwo() method, just as you did in section one.
2. Define the following function
!
a. public boolean isEven(int number) i. This function takes an integer input and returns true if the input is even, false otherwise
3. Define the following function, using isEven()
a. public int numberOfEvens(int[] input) i. This function takes an array and determines the count of even numbers seen in the array
4. Define the following function, using numberOfEvens() a. public int numberOfOdds(int[] input) i. This function takes an array and determines the count of odd numbers seen in the array ii. To build this, use this formula: totalOdds = input.length – numberOfEvens().
5. Define the following function, using isEven() a. public double median(int[] input) i. This function determines the median of a given array, as defined here: 1. If input has an odd number of elements {1,2,3}, return the middle element (2). a. Use isEven() to determine this. 2. 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) a. Use isEven() to determine this.
public class Exercise2 public static void main(String args) t intll data 1,2,3,4,5,6,9) //note that these are already sorted for you System.out.println("Is the first array element even? "+ isEven (data(01)) //System.out.println("Is the third element even? "+ isEven (data[0])) //System.out.println("Number of evens in data: " numberofEvens (data)) //syatem.out.println("Number of evens in data: . . numberotEvena (data))i //system.out.println("Number of odds in data: " +numberofOdds (data)) //System.out.println "Median value:median(data)) public static boolean isEven(int input) t//build first return false; //todo:public static int numberofEvens (int] input) //must use isEven //todo:public static int numberOfOdds (int[] input) //must use numberOfEvensExplanation / Answer
2.
public static boolean isEven(int input){
if(input % 2 == 0)
return true;
else
return false;
}
3.
public static int numberOfEvens(int[] input){
int totalEvens = 0;
for(int i=0;i<input.length;i++)
if(isEven(input[i]))
totalEvens++;
return totalEvens;
}
4.
public int numberOfOdds(int[] input){
int totalOdds = input.length – numberOfEvens()
return totalOdds;
}
5.
public double median(int[] input){
float median;
int size = input.length;
if(isEven(size))
median = (input[size/2]+input[size/2-1])/2.0;
else
median = input[size/2]
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.