0
public static void Reversed (int[] array){
  for (int i = 0; i < array.length / 2; i++){
     int temp = array[i];
     array[i] = array[array.length - (1 + i)];
     array[array.length - (1 + i)] = temp;
  }
}
public static void Squared (int[] array){
  for (int i = 0; i < array.length; i++){
     array[i] = array[i] * array[i];
  }
}
public static void main (String[] args){
  int[] list = new int[5];
  for (int i = 0; i < list.length; i++)
     list[i] = (int) (Math.random() * 1001);

  System.out.println ("The original set is " + Arrays.toString(list) + ".");
  System.out.println ("\nThe largest number is " + Max(list) + ".");
  System.out.println ("The smallest numer is " + Min(list) + ".");

  Reversed(list);
  System.out.println ("The reversed array is " + Arrays.toString(list) + ".");    

  Squared(list);
  System.out.println ("The array squared is " + Arrays.toString(list) +       
    ".");
  }

The methods work as they are supposed to, but the output reverses the squared numbers too, even though they are not supposed to be reversed. The squared set is supposed to be the same order as the original randomly generated set. I tried putting the Squared method ahead of the Reversed method, but it did not have any affect. Is there a way to make a method "self-contained" of sorts?

Thanks in advance!!!

Poorna Senani Gamage
  • 1,246
  • 2
  • 19
  • 30
BDizzle
  • 21
  • 3
  • I just tried switching the println's of Reversed and Squared in the main method and it caused the reversed string to be squared as well. So, I guess to restructure my question: is there a way to nullify the order effects when I invoke previous methods in my main method? – BDizzle Mar 09 '18 at 06:47
  • Can you provide inputs and expected outputs? And just a note, don't change your initial array, instead of having two static void functions, makes them return a new array that you use to print your list, this way you won't be changing the structure of your array, because the array is a reference type and not value type. – Paul Karam Mar 09 '18 at 07:20
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Paul Karam Mar 09 '18 at 07:22
  • Exercise 5: Design and implement a Java program that defines 4 methods as follows: int Max(int[] arr) returns the largest value within an array int Min(int[] arr) returns the smallest value within an array void Reverse(int[] arr) reverses the array void Squared(int[] arr) changes every value within the array to value² Test your methods by creating an rand array of length 5 within your main() method. Your main should invoke each method once, with each invocation use the original array as the actual parameter. Use only one array in program. All printing must be done by the main. – BDizzle Mar 09 '18 at 13:41
  • This is the instructions for the assignment. The inputs are randomly generated numbers and the outputs are just demographics. And we are constrained by only being allowed to use one initial array. Everything works through the methods I created, but I'm trying to nullify the order effects in my main method. – BDizzle Mar 09 '18 at 13:44
  • Then I guess @L.Splillner gave you the right answer according to your question. Just reverse it again after squaring it. – Paul Karam Mar 09 '18 at 14:11

1 Answers1

0

Okay let me guide you through your own program.

You are creating an array of random number, so far so good. Then you reverse the list by calling Reversed() and the the array is reversed, also fine. In third place you are squaring the the numbers of the array by calling Squared(), which does work as well.

Now think about it, you have reversed the array once, why shouldn't you be able to reverse it again with the same method? Because that is,what method are used for! They do not only keep your code more readable, they also allow you to reuse the same piece of code over and over again.

Regarding this you can call Reversed() a 2nd time before printing your squared array to the screen:

public static void main (String[] args){
    int[] list = new int[5];
    for (int i = 0; i < list.length; i++)
       list[i] = (int) (Math.random() * 1001);

    System.out.println ("The original set is " + Arrays.toString(list) + ".");

    Reversed(list);
    System.out.println ("The reversed array is " + Arrays.toString(list) + ".");    

    Squared(list);
    Reversed( list );
    System.out.println ("The array squared is " + Arrays.toString(list) +       
      ".");
    }

Best regards.

L.Spillner
  • 1,772
  • 10
  • 19
  • Thank you for the simple fix! I should have realized that, but it was about 2AM. I do have a question though: Is there a way to define the previous methods so that when I call them in the main they will not affect subsequent outputs? For the assignment, we had to use the 'public static void XXX' for the method type, but couldn't I have used 'public static int Rev' and created another array in that method to reference in the main? – BDizzle Mar 09 '18 at 13:48
  • This is my first semester of programming, so I'm learning the gist of things as I go haha. – BDizzle Mar 09 '18 at 13:49
  • @BDizzle yes there is a possibility. First of all you should take a look at the question Paul Karam linked in the comments under your original post. You could copy the array to another local array within the method and then reverse the order, print it to the screen and then exit the method. This way your original array wouldn't be affected at all but you printed the data once. This does only proof useful if you are certain that you'll not need the reversed order anymore. – L.Spillner Mar 09 '18 at 13:54