Create class called as PointTest (with a main method) Write a program that reads
ID: 3537193 • Letter: C
Question
Create class called as PointTest (with a main method) Write a program that reads from
the JAVA console the (x, y) coordinates for 2 points in the plane. (Use the Scanner class for
reading data)You can assume that all numbers are integers.
Using the Point class from JAVA (you may need to look it up on Point class), instantiate 2 Point
objects with your input data, than output the data for both point objects.
1) Write a static method named as euclideanDistance that returns a double to
compute the Euclidean distance between the 2 given points, your method should
signature should have 4 parameters of the Point class (x1,y1 and x2, y2). Make sure to
use the Decimal Format class to format your output to display 2 digits after decimal
point.
2) Write another static method named as cityBlockDistance that returns an int to
compute the City block distance between the given 2 points, again your method should
signature should have 4 parameters of the Point class (x1,y1 and x2, y2).
Explanation / Answer
Your code
//Point Class
public class Point {
private int x1;
private int y1;
private int x2;
private int y2;
public Point() {
super();
// TODO Auto-generated constructor stub
}
public Point(int x1, int y1, int x2,
int y2) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public static double euclideanDistance(int x1, int y1, int x2, int y2)
{
int xDiff = x2-x1;
int yDiff = y2-y1;
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
public static int cityBlockDistance(int x1, int y1, int x2, int y2)
{
int xDiff = x2-x1;
int yDiff = y2-y1;
return (int) Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
}
//PointTest Class
import java.text.DecimalFormat;
import java.util.Scanner;
/*Create class called as PointTest (with a main method) Write a program that reads from
the JAVA console the (x, y) coordinates for 2 points in the plane. (Use the Scanner class for
reading data)You can assume that all numbers are integers.
2) .*/
public class PointTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the X co-ordinate of first Point. Make sure that is an integer");
int xCoordinate1 = scanner.nextInt();
System.out.println("Enter the Y co-ordinate of first Point. Make sure that is an integer");
int yCoordinate1 = scanner.nextInt();
System.out.println("Enter the X co-ordinate of second Point. Make sure that is an integer");
int xCoordinate2 = scanner.nextInt();
System.out.println("Enter the Y co-ordinate of second Point. Make sure that is an integer");
int yCoordinate2 = scanner.nextInt();
double distance = Point.euclideanDistance(xCoordinate1, yCoordinate1, xCoordinate2, yCoordinate2);
// printing precision upto two decimal places as mentioned in the question
DecimalFormat df = new DecimalFormat("#.00");
String distanceCal= df.format(distance);
System.out.println("Distance between two points is : "+distanceCal);
System.out.println(" ");
System.out.println("Now lets check the distance between two cities");
System.out.println("Enter the X co-ordinate of first City. Make sure that is an integer");
int xCoordinateCity1 = scanner.nextInt();
System.out.println("Enter the Y co-ordinate of first City. Make sure that is an integer");
int yCoordinateCity1 = scanner.nextInt();
System.out.println("Enter the X co-ordinate of second City. Make sure that is an integer");
int xCoordinateCity2 = scanner.nextInt();
System.out.println("Enter the Y co-ordinate of second City. Make sure that is an integer");
int yCoordinateCity2 = scanner.nextInt();
int distanceCities = Point.cityBlockDistance(xCoordinateCity1, yCoordinateCity1, xCoordinateCity2, yCoordinateCity2);
System.out.println("Distance between two points is : "+distanceCities);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.