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

Write a function that swaps two Point objects in java. Use the code given below:

ID: 3631920 • Letter: W

Question

Write a function that swaps two Point objects in java. Use the code given below:

import java.util.*;

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 (in.nextDouble(), in.nextDouble());

System.out. print("Enter x and y coordinates of second point: ");
Point p2 = new Point (in.nextDouble(), in.nextDouble());

swap(p1, p2);

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

public void swap(Point p, Point q)
{
// Enter your code here
}

public static void main(String[] args)
{
Main myApp = new Main();
}
}

Explanation / Answer

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(); } } Note: As call by reference is not possible in JAVA, i have modified the code little to print the updated or swapped points.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote