Description Create a Java program called Point. This project will have Point.jav
ID: 3763633 • Letter: D
Question
Description
Create a Java program called Point. This project will have Point.java as its file. The Point class will represent a point in 2-Dimensional space.
– (6 points) The Point class will have two instance variables: x and y. Both instance variables will have type double. Both instance variables will be private.
– (10 points) You will create two constructors: a default no argument constructor and a constructor that takes two integers as arguments(representing x and y coordinates).
– (5 points) You will also create accessors(getters) and mutators(setters) methods for each instance variable.
– (24 Points @ 6 points each) 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:
– (5 points) Creates 3 Point Objects, values of x and y coordinates can be defined by YOU.
– (5 points) Print the three Point objects to the console showing their x and y coordinates.
– (5 points) Compute the distance between two Point objects. Which two Point objects is up to you. Print the distance to the console.
– (5 points) Compute the distance between a Point object and the origin. Which Point object you used is up to you. Print the distance to the console.
– (5 points) Compute the slop of a line passing through two point Objects. Which two Point objects is up to you. Print the slop the console.
– (5 points) Determine which quadrant all 3 Point objects are in. Print the quadrants to the console.
– (10 points) 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
/**
* @author Srinivas Palli point is in 2-Dimensional space
*
*/
public class Point {
/**
* x and y represents a (x,y) coordinates in 2-Dimensional space
*/
private double x, y;
/**
* default constructor initiallizes x and y to 0
*/
public Point() {
this.x = 0;
this.y = 0;
}
/**
* @param x
* @param y
* default constructor initiallizes x and y to parameters x and y
*/
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;
}
/**
* @return distance
*
* Distance from Origin, Assumed to be (0,0)
*/
public double distFromOrg() {
return Math.sqrt(x * x + y * y);
}
/**
* @param P
* @return distance Distance between two Points
*/
public double DistFromPoint(Point P) {
return Math.sqrt((P.x - x) * (P.x - x) + (P.y - y) * (P.y - y));
}
/**
* @return quadrant value What Quadrant is the point object in public
*/
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;
}
/**
* @param P
* contains x,y coordinates
* @return slope of point Slope between two points
*/
public double slope(Point P) {
return (P.y - y) / (P.x - x);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
return "x-coord: " + x + " y-coord: " + y;
}
/**
* @param args
*/
public static void main(String args[]) {
// instantiating 3 Point Objects
Point p1 = new Point(3.00, 2.00);
Point p2 = new Point(2.00, -5.00);
Point p3 = new Point(-1.00, 2.00);
// displaying Point Objects
System.out.println("Point 1 " + p1);
System.out.println("Point 2 " + p2);
System.out.println("Point 3 " + p3);
// calling and displaying instance methods
System.out.println("Distance between Point 1 and Point 2 :"
+ p1.DistFromPoint(p2));
System.out.println("Distance between Point 3 and the origin(0,0):"
+ p3.distFromOrg());
System.out
.println("Slope of a line passing through Point 1 and Point 2:"
+ p1.slope(p2));
System.out.println("Point 1 is in quadrant " + p1.whatQuadrant());
System.out.println("Point 2 is in quadrant " + p2.whatQuadrant());
System.out.println("Point 3 is in quadrant " + p3.whatQuadrant());
}
}
OUTPUT:
Point 1 x-coord: 3.0 y-coord: 2.0
Point 2 x-coord: 2.0 y-coord: -5.0
Point 3 x-coord: -1.0 y-coord: 2.0
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