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

java help!im a littleconfused with this swap. Could someone please explain the s

ID: 674492 • Letter: J

Question

java help!im a littleconfused with this swap. Could someone please explain the steps. possibly with a memory diagram? thanks!!

public class tricky {

  
   public static void tricky1(Point arg1, Point arg2) {
    arg1.x = 100;
    arg1.y = 100;
    Point temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}

public static void main(String [] args) {
    Point pnt1 = new Point(0,0);
    Point pnt2 = new Point(0,0);
    System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
    System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
    System.out.println(" "); tricky1(pnt1,pnt2);
    System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
    System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}

Explanation / Answer

//You didn't specified the Point class. Probably, it is like reading 2 variables like x-axis and y-axis.
public class tricky {
  
public static void tricky1(Point arg1, Point arg2) //Input two points arg1, and arg2, and then updates the point arg1 to (100,100).
//Then it will exchange the values of arg1 and arg2.
//This means arg2 is the point (100,100), and arg1 will be the second point arg2.
{
arg1.x = 100;   //arg1.x is assigned with 100.
arg1.y = 100; //arg1.y is assigned with 100.
Point temp = arg1; //Values of arg1 are copied to temp.
arg1 = arg2; //Copies the values of arg2 to arg1. Don't know what the values of arg2 as of now.
arg2 = temp; //Copies the values of temp to arg2.
}
public static void main(String [] args) {
Point pnt1 = new Point(0,0); //pnt1 is a point at origin. (0,0).
Point pnt2 = new Point(0,0); //pnt2 is a point at origin. (0,0).
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
//Displays a message: "X: 0 Y: 0"
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
//Displays a message: "X: 0 Y: 0"
System.out.println(" "); //Prints a space.
//Now applies the function tricky as described in the function.
tricky1(pnt1,pnt2);
//After the application, now, pnt1 is a point at (0,0), and pnt2 is a point at (100,100).
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
//Displays a message: "X: 100 Y: 100"
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
//Displays a message: "X: 0 Y: 0"
}
}