OnTheFence OnTheFence - 1 month ago 12
Java Question

What can I do to make my reverse code work properly?

I have to design and implement a Java program (named

itArrayMethods
), that defines 4 methods as follows:


  • int arrayMax (int [ ] arr)
    determines and returns the maximum value within an array

  • int arrayMin (int [ ] arr)
    determines and returns the minimum value within an array

  • void arraySquared (int[ ]arr)
    changes every value within the array to value squared

  • void arrayReverse (int [ ] arr)
    reverses the array (for example: 7 8 1 2 becomes 2 1 8 7)



Everything works fine but the arrayReverse method. I am stuck.

/*This program uses four different methods to find the minimum, maximum, squared value and reversal of an array entered by the user*/
import java.util.*;
public class ArrayMethods {

//Main method
public static void main(String[] args) {
char reply = 'Y';
while(reply == 'Y'){

Scanner input = new Scanner(System.in); //Create scanner

//Prompt user for inputs
System.out.print("Enter number of elements in the array:");
int n = input.nextInt();
int[] arr = new int[n];

System.out.println("Enter elements of array:");
for(int i = 0; i < n; i++){
arr[i] = input.nextInt();
}

System.out.println ("You entered: ") ; //Display array entered
for(int i = 0;i <arr.length; i++){
System.out.println(arr[i]);
}

//Display min and max numbers from their invoked methods
System.out.println("The largest number in the array is " + arrayMax(arr));
System.out.println("The smallest number in the array is " + arrayMin(arr));

//Invoke arraySquared method and display the result
System.out.println("The Array Squared is: ");
arraySquared(arr);
for(int i = 0 ;i < arr.length; i++){
System.out.println(arr[i]);
}

//Invoke arrayReverse method and display result
//My problem. It displays the same input as the squared integers
System.out.println("The Array Reversed is: ");
arrayReverse(arr);
for(int i = 0;i < arr.length; i++){
System.out.println(arr[i]);
}

System.out.println("Would you like to rerun this program? Enter Y/N");
reply = input.next().charAt(0);

}
System.out.println("Thanks for using this program!");
System.exit(0);

}


//getting max value
public static int arrayMax (int [ ] arr){
int max = arr[0];
for(int i = 0; i < arr.length; i++) {
if(max < arr[i]) {
max = arr[i];
}
}
return (max);
}

//getting min value
public static int arrayMin (int [ ] arr) {
int min = arr[0];
for(int i = 0; i < arr.length; i++){
if(min > arr[i]){
min = arr[i];
}
}
return (min);
}

//getting squares of array numbers
public static void arraySquared (int[ ]arr) {
for(int i=0; i< arr.length; i++){
arr[i] = (int) Math.pow(arr[i], 2);
}
}

//getting reversal of array
public static void arrayReverse(int[ ] arr) { //code obtained from textbook
for(int i = 0, j = arr.length - 1; i < j; ++i, --j) {
int temp = arr[i];
arr[i] = arr[j];
arr[i] = temp;
}


}

}

Answer

You are assigning arr[i] two times, but never assign arr[j]. After assigning arr[i] to arr[j], you assign arr[i] back to the original value of arr[i] that was stored in temp.

Try:

int temp = arr[i];  
arr[i] = arr[j];
arr[j] = temp;
Comments