please help finish this lab Building Each Exercise Driver Do
please help finish this lab
Building Each Exercise Driver
Download the drivers (ExerciseX.java) and start by reading the code. Notice in Exercise1.java’s main how each method for the problems below have already been invoked for you (Skeleton Level: Strong). As you complete additional exercises, notice the comments in each file. As exercises progress, you’ll be required to uncomment lines in main or even build parts of the main driver yourself. We’ll start with skeleton files that are almost complete, and then look at drivers where you need to provide additional code to get things to function correctly.
\"Exercise2.java \"
// Skeleton file for use with basic numerical analysis, such as:// Are numbers even? Odd? How many evens? What is the median of a set?//// Skeleton Level: Medium.// You need to uncomment and fill-in functions below to complete// this driver. You also need to uncomment lines in main to// invoke the functions you\'ve defined.//
public class Exercise2 {
public static void main(String[] args) {
int[] 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[0]));
//System.out.println(\"Is the third element even? \" +
isEven(data[0]));
//System.out.println(\"Number of evens in data: \" +
numberOfEvens(data));
//System.out.println(\"Number of odds in data: \" +
numberOfOdds(data));
//System.out.println(\"Median value: \" + median(data));
}public static boolean isEven(int input) {//build first
return false;
}//todo:public static int numberOfEvens(int[] input) {//must use isEven//todo:public static int numberOfOdds(int[] input) {//must use
numberOfEvens()}
\"Exercise3.java \"
// Skeleton file for use with the well-ordering principle of integers, such as:// Which is the least element? The greatest? What is the range?//// Skeleton Level: Weak.// You need to define functions below to compile this driver.// You also need to uncomment some lines in main to invoke thefunctions// you\'ve defined. Finally, you\'ll need to add extra lines to main// to call the findMax() and findRange() functions. Be sure you call// every function twice; once for each array defined in main below.//
public class Exercise3 {
public static void main(String[] args) {//int[] data = {50,100,20,45};//int[] data2 = {-4,-1,-3,-10,-2};
//System.out.println(\"Min element in data is:\" + findMin(data));//System.out.println(\"Min element data2 is:\" + findMin(data2));
//todo: use the code examples above to solve for the following//\"Max element of data is: ?\";//\"Max element of data2 is: ?\";
//\"Range of data is: ?\";//\"Range of data2 is: ?\";
}//todo:findMin(int[] input)//todo:findMax(int[] input)//todo:findRange(int[] input) //must reuse findMin() and findMax()//todo:sum(int[] input)
}
\"Exercise4.java \"
// Skeleton file for use with basic list processing, such as:// Shift elements left to make some room for a new element, or// Shift elements right to overwrite/remove a target element.// Optional challenge extends shifts to include rotates, but these// should reuse arrayShiftLeft() and right(), so do those first.//// Examples:// shifing left == {3,9,2,0,0,0} -> {9,2,0,0,0,0}// shifing right == {9,2,0,0,0,0} -> {9,9,2,0,0,0}// rotate left == {1,2,3} -> {2,3,1}// rotate right == {1,2,3} -> {3,1,2}////// Skeleton Level: Minimal.// You need to call each function below from main; see comment stubs inmain to start.// You need to define each function below; see comment stubs to getstarted.//import java.util.Arrays;
public class Exercise4 {
public static void main(String[] args) {
//int[] data = {3,9,2,0,0,0}; //a six-element array with only three
values so far
//int[] data2 = {1,5,3,2,9,6}; //a six-element array filled with
values.
//arrayShiftLeft(data);//...do the same for data2
//print out each element in data here// for(int i = 0; i < data.length...//print out each element in data2 here (see for loop above)
//TODO: following the pattern above for arrayShiftLeft,//add code to main to call arrayShiftRight and then print out arrays
}
//public static void arrayShiftLeft(int[] input, int startIndex) {//for(i = startIndex) { //why -1 here?
//input[i] = input[?];
//}
//}
//public static void arrayShiftRight(int[] input, int startIndex) {
//optional - for extra challenge -//public static void rotateLeft(int[] input) {//hint: use arrayShiftLeft inside rotateLeft
//public static void rotateRight(int[] input) {//hint: use arrayShiftRight inside rotateRight
}
Solution
// Skeleton file for use with basic numerical analysis, such as:// Are numbers even? Odd? How many evens? What is the median of a set?//// Skeleton Level: Medium.// You need to uncomment and fill-in functions below to complete// this driver. You also need to uncomment lines in main to// invoke the functions you\'ve defined.//
public class Exercise2 {
public static void main(String[] args) {
int[] 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[0]));
System.out.println(\"Is the third element even? \" + isEven(data[2]));
System.out.println(\"Number of evens in data: \" + numberOfEvens(data));
System.out.println(\"Number of odds in data: \" + numberOfOdds(data));
System.out.println(\"Median value: \" + median(data));
}
public static boolean isEven(int input) {//build first
if(input %2 ==0){
return true;
}
return false;
}
//todo:
public static int numberOfEvens(int[] input) {
int countEven=0;
for(int i=0;i<input.length;i++){
if(isEven(input[i]) == true){
countEven+=1;
}
}
return countEven;
}
//must use isEven//todo:
public static int numberOfOdds(int[] input) {
int countOdd;
countOdd=input.length-numberOfEvens(input);
/*for(int i=0;i<input.length;i++){
if(isEven(input[i]) == false){
countOdd+=1;
}
}*/
return countOdd;
}
public static double median(int[] input){
int middle = input.length/2;
if (input.length%2 == 1) {
return input[middle];
}
else {
return (input[middle-1] + input[middle]) / 2.0;
}
}
}


