You will create a basic class for representing a 2D point. Your class will need
ID: 646955 • Letter: Y
Question
You will create a basic class for representing a 2D point. Your class will need to store the x and y coordinates of the point and provide the following methods: Constructors: Your Point2D object should be immutable, that is, once you create a Point2D object, there should be no way to change the points location. To compute the distance between two points (x1, y1) and (x2, y2), use the following equation: Root (x1-x2)^2+(y1-y2)^2 In your main method, write a program that tests your Point2D class by instantiating points, calling static methods versus instance methods and printing t heir results to the console. You do not need to get input from the user but come up with good test cases.Explanation / Answer
import java.util.Scanner;
//distance = square root of [(x2-x1)squared + (y2-y1)squared]
public class PointDistance
{
public static void main(String[] args)
{
double distance(double x1, double y1, double x2, double y2)
{
return math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));
}
double x1, x2, y, y2;
double distance;
Scanner scan = new Scanner (System.in);
System.out.println("Enter the x coordinate for point 1: ");
x1 = scan.nextDouble();
System.out.println("Enter the y coordinate for point 1: ");
y1 = scan.nextDouble();
System.out.println("Enter the x coordinate for point 2: ");
x2 = scan.nextDouble();
System.out.println("Enter the y coordinate for point 2: ");
y2 = scan.nextDouble();
distance = distance(x1,y1,x2,y2);
System.out.println("The distance between the two points is " + distance + " .");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.