-4

I am working on an assignment, and while I have completed the beginning part of it I am am stuck on the last step of it. This is what i have to do for the last part.

Modify the main method so after the call to the swap method, the actual values of num1 and num2 are swapped in main and show this in the output. The output will have one additional line, that shows the values of the two numbers in main after they are swapped. The new line in the output needs to be "After swapping the numbers in main method, num1 is 2 and num2 is 1".

Code:

     public class PassByValue {
  /** Main method */
    public static void main(String[] args) {
        // Declare and initialize variables
        int num1 = 1;
        int num2 = 2;

        System.out.println("Before invoking the swap method, num1 is " +
        num1 + " and num2 is " + num2);

        //invoke the swap method to attempt to swap two variables
        swap(num1, num2);

        System.out.println("After invoking the swap method, num1 is " +
         num1 + " and num2 is " + num2);
    }

    /** Swap two variables */
    public static void swap(int n1, int n2) {
        System.out.println("\tInside the swap method");
        System.out.println("\t\tBefore swapping, n1 is " + n1
                + " and n2 is " +n2);

        // Swap n1 with n2
        int temp = n1;
        n1 = n2;
        n2 = temp;

        System.out.println("\t\tAfter swapping, n1 is " + n1 
                + " and n2 is " + n2);
    }
}    
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sean. A
  • 1
  • 2

3 Answers3

1

Your professor might had given this assignment, for you to understand the Pass by value and pass by reference concepts in java and also the OOPS.

Firstly in java, the method argument will be sent as pass by value. If the arguments are primitive types, their value will be sent. If arguments are 'Object' the reference of the object will be still sent as value.

So, fr your problem here, you can solve it by creating a object holding your values.

The code below should solve your problem :

 public class test {
 /** Main method */
public static void main(String[] args) {
    // Declare and initialize variables

    Numberr num1 = new Numberr(1);
    Numberr num2 = new Numberr(2);


    System.out.println("Before invoking the swap method, num1 is " +
    num1.n + " and num2 is " + num2.n);

    //invoke the swap method to attempt to swap two variables
    swap(num1, num2);

    System.out.println("After invoking the swap method, num1 is " +
     num1.n + " and num2 is " + num2.n);
}

/** Swap two variables */
public static void swap(Numberr n1, Numberr n2) {
    System.out.println("\tInside the swap method");
    System.out.println("\t\tBefore swapping, n1 is " + n1.n
            + " and n2 is " +n2.n);

    // Swap n1 with n2
    int temp = n1.n;
    n1.n = n2.n;
    n2.n = temp;

    System.out.println("\t\tAfter swapping, n1 is " + n1 .n
            + " and n2 is " + n2.n);
}


}    
class Numberr {
public Numberr(int i) {
        // TODO Auto-generated constructor stub
    this.n = i;
}

int n;

  }
0

I will show you two methods here choose which is better. First:

public class Swap{

      static int num1 = 1;//declare num1 and num2 as static
      static int num2 = 2;
    /** Swap two variables */
    public static void swap(int n1, int n2) {
        System.out.println("\tInside the swap method");
        System.out.println("\t\tBefore swapping, n1 is " + n1
                + " and n2 is " +n2);

        // Swap n1 with n2
        int temp = n1;
        n1 = n2;
        n2 = temp;

        System.out.println("\t\tAfter swapping, n1 is " + n1 
                + " and n2 is " + n2);
        num1=n1;//after swapping assign the new swapped values to num1 and num2. It will reflect in main as it is declared as static
        num2=n2;
    }

    public static void main(String[] args) throws UnknownHostException {
         // Declare and initialize variables


        System.out.println("Before invoking the swap method, num1 is " +
        num1 + " and num2 is " + num2);

        //invoke the swap method to attempt to swap two variables
        swap(num1, num2);

        System.out.println("After invoking the swap method, num1 is " +
         num1 + " and num2 is " + num2);
     }


}

Method 2:

public class Swap{

      static int num1 = 1;
      static int num2 = 2;
    /** Swap two variables */
    public static void swap() {//without functional arguments
        System.out.println("\tInside the swap method");
        System.out.println("\t\tBefore swapping, n1 is " + num1
                + " and n2 is " +num2);

        // Swap n1 with n2
        int temp = num1;//<-- swap values here and it will reflect
        num1 = num2;
        num2 = temp;

        System.out.println("\t\tAfter swapping, n1 is " + num1 
                + " and n2 is " + num2);     
    }

    public static void main(String[] args) throws UnknownHostException {
         // Declare and initialize variables


        System.out.println("Before invoking the swap method, num1 is " +
        num1 + " and num2 is " + num2);

        //invoke the swap method to attempt to swap two variables
        swap();//dont pass arguments as num1 and num2 are declared static, swap the values in the method itself

        System.out.println("After invoking the swap method, num1 is " +
         num1 + " and num2 is " + num2);
     }


}
0

I think the point of the exercise is to show you it is impossible, because Java only has "pass by value". To do something like this you need to pass the swap method an object that contains the values. For example, pass it an array, and then swap can swap the first two values in the array. Or define some other object.

owler
  • 1,059
  • 8
  • 13