Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method called evenFloorOddCeil that takes in one parameter and return a

ID: 3751477 • Letter: W

Question

Write a method called evenFloorOddCeil that takes in one parameter and return a double array: an array populated with doubles. Floor the values at even indices (0 is considered to be an even index), and ceil the values at off indices. Replace the values in the original array. Do not create a new array.

Example #1:

evenFloorOddCeil(new double[]{10.7, 3.4, 2.1}) would return a double array [10.0, 4.0, 2.0]

Example #2:

evenFloorOddCeil(new double[100.9, 9.2, 78.0, 190.8, 199.1); would return a double array [100.0, 10.0, 78.0, 191.0, 199.0]

Hint: Math.ceil(double a) and Math.floor(double a) may come in handy.

Explanation / Answer

Java code:

public class ArrayTest

{

//main method

public static void main(String[]arg)

{

//calling the method

double[] result=evenFloorOddCeil(new double[]{10.7, 3.4, 2.1});

System.out.print("[");

for(int i=0;i<result.length;i++) {

if(i<result.length-1){

System.out.print(result[i]+", ");

}else {

System.out.print(result[i]);

}

}

System.out.print("]");

}

//method implementation

public static double[] evenFloorOddCeil(double arr[])

{

for(int i=0;i<arr.length;i++) {

if(i%2==0){ //checking for even indices

arr[i]= Math.floor(arr[i]);

}else{

arr[i]= Math.ceil(arr[i]);

}

}//end of for loop

//returning double array

return arr;

}

}

Output:

[10.0, 4.0, 2.0]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote