public class Point { private double x, y; public Point() { this.x = 0; this.y =
ID: 3763884 • Letter: P
Question
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;
}
}
in the main method(in the Point class) how can include the following:
Creates 3 Point Objects, values of x and y coordinates can be defined 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.
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());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.