Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. //TASK 1.1
  2.     private void callerSideEffect() {
  3.  
  4.         myWindow.clearOut();
  5.  
  6.         //actual parameters
  7.         int[] arrayActual = {1, 8, 16, 4, 20};                                    //array of int as reference type
  8.         int intActual = arrayActual[0];                                           //element of array as value type
  9.  
  10.         calledSideEffect(arrayActual, intActual);
  11.  
  12.  
  13.         myWindow.writeOutLine("\nAFTER METHOD CALL:");
  14.         myWindow.writeOutLine("The value of intActual after method call is: " + intActual + "\nThe value of arrayActual after method call is: " + arrayActual[0]);
  15.     }
  16.     private void calledSideEffect(int[] arrayFormal, int intFormal) {           //formal parameters
  17.  
  18.         arrayFormal[0]++;                                                       //1 incremented by 1
  19.         //above arrayFormal equals to the arrayActual (references to the same address, so the address after call is not changed)
  20.         intFormal++;                                                            //also incremented by 1
  21.  
  22.         myWindow.writeOutLine("ON METHOD CALL:");
  23.         myWindow.writeOutLine("The value of arrayFormal on method call is: " + arrayFormal[0] + "\nThe value of intFormal on method call is: " + intFormal );
  24.     }
  25.  
  26.     //TASK 1.3
  27.     private void callerSwapParameters() {
  28.         myWindow.clearOut();
  29.  
  30.         //actual parameters
  31.         int n1 = 8;
  32.         int n2 = 12;
  33.  
  34.         calledSwapParameters(n1, n2);
  35.  
  36.         myWindow.writeOutLine("\nAFTER METHOD CALL:");
  37.         myWindow.writeOut("The value of n1 after the call is: " + n1 + "\nThe value of n2 after the call is: " + n2);
  38.         //this methods values will be different to the calledSwapParameters values
  39.  
  40.     }
  41.     private void calledSwapParameters(int n2, int n1) {
  42.  
  43.         myWindow.writeOutLine("ON METHOD CALL:");
  44.         myWindow.writeOutLine("The value of n1 on the method call is: " + n1 + "\nThe value of n2 on the method call is: " + n2);
  45.         //ESSENTIALLY, SWAPPED ON METHOD CALL
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement