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

I need to make the following changes to the code below: In Ex4, you swap method

ID: 1937977 • Letter: I

Question

I need to make the following changes to the code below: In Ex4, you swap method does not work because you swapped the references which are local to the method. ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* package lab1_4; import java.awt.Point; import java.util.Scanner; public class Main { public Main() { Scanner in = new Scanner(System.in); System.out. print("Enter x and y coordinates of first point: "); Point p1 = new Point ((int)in.nextDouble(), (int)in.nextDouble()); System.out. print("Enter x and y coordinates of second point: "); Point p2 = new Point ((int)in.nextDouble(), (int)in.nextDouble()); swap(p1, p2); } public void swap(Point p, Point q) { Point tmp = p; p=q; q=tmp; System.out.println("p1 = " + p); System.out.println("p2 = " + q); } public static void main(String[] args) { Main myApp = new Main(); } }

Explanation / Answer

Below is the working code checked in eclipse

--------------------------------------------


import java.awt.Point;

import java.util.Scanner;

public class Main

{

Point p1,p2;

public Main()

{

Scanner in = new Scanner(System.in);

System.out. print("Enter x and y coordinates of first point: ");

p1 = new Point ((int)in.nextDouble(), (int)in.nextDouble());

System.out. print("Enter x and y coordinates of second point: ");

p2 = new Point ((int)in.nextDouble(), (int)in.nextDouble());

swap(p1, p2);

}

public void swap(Point p, Point q)

{

Point tmp = p;

this.p1=q;

this.p2=tmp;

System.out.println("p1 = " + p1);

System.out.println("p2 = " + p2);

}

public static void main(String[] args)

{

Main myApp = new Main();

}

}

-------------------------------

output

---------------

Enter x and y coordinates of first point: 2 3

Enter x and y coordinates of second point: 4 5

p1 = java.awt.Point[x=4,y=5]

p2 = java.awt.Point[x=2,y=3]