Create a Java program called Point. This project will have Point.java as its le.
ID: 3763919 • Letter: C
Question
Create a Java program called Point. This project will have Point.java as its
le. The Point class will represent a point in 2-Dimensional space.
{ The Point class will have two instance variables: x and y. Both
instance variables will have type double. Both instance variables will be
private.
{ You will create two constructors: a default no argument con-
structor and a constructor that takes two doubles as arguments(representing
x and y coordinates).
{ You will also create accessors(getters) and mutators(setters)
methods for each instance variable.
{ You will then implement the following instance
methods:
//Distance from Origin, Assumed to be (0,0)
public double distFromOrg()
//Distance between two Points
public double DistFromPoint(Point P)
//What Quadrant is the point object in
public int whatQuadrant()
//Slope between two points
public double slope(Point P)
Finally, in the main method(in the Point class) that does the following:
{ Creates 3 Point Objects, values of x and y coordinates can be
de ned by YOU.
{ Print the three Point objects to the console showing their x and y
coordinates.
{ Compute the distance between two Point objects. Which two Point
objects is up to you. Print the distance to the console.
{ Compute the distance between a Point object and the origin.
Which Point object you used is up to you. Print the distance to the con-
sole.
{ Compute the slop of a line passing through two point Objects.
Which two Point objects is up to you. Print the slop the console.
{ Determine which quadrant all 3 Point objects are in. Print the
quadrants to the console.
{ Please document/comment your code where necessary.
Example Output:
Point 1 x-coord: 3.00 y-coord: 2.00
Point 2 x-coord: 2.00 y-coord: -5.00
Point 3 x-coord: -1.00 y-coord: 2.00
Distance between Point 1 and Point 2 : 7.0710678118654755
Distance between Point 3 and the origin(0,0) : 2.23606797749979
Slope of a line passing through Point 1 and Point 2 : 7.0
Point 1 is in quadrant 1
Point 2 is in quadrant 4
Point 3 is in quadrant 2
Explanation / Answer
public class Point {
private double x, y;
public Point() {
this.x = 0;
this.y = 0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @return the y
*/
public double getY() {
return y;
}
/**
* @param x
* the x to set
*/
public void setX(double x) {
this.x = x;
}
/**
* @param y
* the y to set
*/
public void setY(double y) {
this.y = y;
}
public double distFromOrg() {
return Math.sqrt(x * x + y * y);
}
public double DistFromPoint(Point P) {
return Math.sqrt((P.x - x) * (P.x - x) + (P.y - y) * (P.y - y));
}
public int whatQuadrant() {
if (x > 0 && y > 0)
return 1;
else if (x < 0 && y > 0)
return 2;
else if (x < 0 && y < 0)
return 3;
else
return 4;
}
public double slope(Point P) {
return (P.y - y) / (P.x - x);
}
@Override
public String toString() {
return "x-coord: " + x + " y-coord: " + y;
}
public static void main(String[] args) {
Point p1 = new Point(1,2);
Point p2 = new Point(12,-2);
Point p3 = new Point(-14,-2);
System.out.println("3 Points are: ");
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println();
System.out.println("Distance between "+p1+" and "+p2+" is: "+p1.DistFromPoint(p2));
System.out.println("Distance between "+p1+" and origin is: "+p1.distFromOrg());
System.out.println("Slope of line from "+p1+" and "+p1+" is: "+p1.slope(p2));
System.out.println(p1+" is in quadrant "+p1.whatQuadrant());
System.out.println(p2+" is in quadrant "+p2.whatQuadrant());
System.out.println(p3+" is in quadrant "+p3.whatQuadrant());
}
}