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

Write a method named, reverse, which reverses the order of an array\'s elements

ID: 3858202 • Letter: W

Question

Write a method named, reverse, which reverses the order of an array's elements without creating another array. The header for the method, reverse, is: void reverse (int arr[]){... where arr is an array containing any number of integer elements. For example, if the array is {6, 4, 9, 1, 7} BEFORE calling reverse, then the array should be {7, 1, 9, 4, 6} AFTER calling reverse. Your code should work for any length of integer array containing any values. Your reverse method should not do any keyboard input and no printing to the console. You should write the method, reverse, in a class named, FinalExam. In the FinalExam class you must also include a main method that tests the method, reverse. The main method may do console output, but does not need any keyboard input. Arrays created in main for use in testing the reverse method should be created using static initialization as in the example below. Here are four tests you should run from the main method by calling the reverse method: Pass an array with 10 elements and then verify the array elements are reversed upon returning from the method, reverse. Here is an example of doing this: //create the array to test int[] myArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}: int [] myArrayRev = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}: reverse (myArray): if (Arrays.equals (myArray, myArrRev)) System.out.println("reverse worked for 10 elements."): Pass an array with 9 elements and then verify the array elements are reversed upon returning from the method, reverse. Pass an array with 1 element and then verify the array element is unchanged upon returning from the method, reverse. Pass an array with 0 elements and then verify the array is unchanged reversed upon returning from the method, reverse.

Explanation / Answer

Reverse operation is performed by swapping first and last element, second and second last element and so on, without the help of any additional array.

The code inside main method is the same which is given in the question. Only the commented part prints the reverse array. The reverse method is implemented using the above swapping logic.

Comments have been added for detailed explanation.

CODE:

import java.util.*;

public class FinalExam {

void reverse(int arr[] ){
//created left and right as two index positons
int left=0,right; //left initialized to 0
right=arr.length-1; //right initialized to last elements index position
  
//work variable is used for temporarily storing the left integer while switching
while(left<right){ //This loop runs till we reach the center of the array

//Swapping operation
int work=arr[left];
arr[left]=arr[right];
arr[right]=work;

left++;
right--;

}//while end
  
}//reverse method ends

public static void main(String[] args) {
  
FinalExam obj=new FinalExam();// object creation for method call
  
//Given code snippet
int[] myArray={0,1,2,3,4,5,6,7,8,9};
int [] myArrRev={9,8,7,6,5,4,3,2,1,0};
obj.reverse(myArray);
  
  
  
/* To print the array which is reversed.
for(int i=0;i<myArray.length;i++){
System.out.println(myArray[i]);
}
*/

if(Arrays.equals(myArray,myArrRev)){
System.out.println("Reverse worked for "+myArray.length+" elements.");
}
  
  
}//main method ends
}

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