public class Point{
//these are instance variables; they are private and unaccessible by others
private double x;
private double y;
//this is a default constructor
//the job of a constructor is to initialize the private instance variables
//they do not have a return type
//constructor body is executed when new object is created with the word new
public Point(){
x = 0;
y = 0;
}
public Point(double xValue, double yValue){
x = xValue;
y = yValue;
}
//these are accessor methods
//accessor methods provide access to private attributes of a class
public double getX(){
return x;
}
//these are accessor methods
//accessor methods provide access to private attributes of a class
public double getY(){
return y;
}
public double findDistanceFromOrigin(){
double result = Math.sqrt(x*x + y*y);
return result;
}
public String toString(){
String s = "(" + x + "," + y + ")";
return s;
}
//when this method is called, it is essentially saying
//"find the midpoint from me to pt"
public Point findMidPoint(Point pt){
double midX = (x + pt.x)/2;
double midY = (y + pt.y)/2;
Point midPoint = new Point(midX,midY);
return midPoint;
}
public double findDistanceBetween(Point pt){
double xDiff = (x - pt.x);
double yDiff = (y - pt.y);
double result = Math.sqrt((xDiff*xDiff) + (yDiff*yDiff));
return result;
}
public double findSlope(Point pt){
double xDiff = (x - pt.x);
double yDiff = (y - pt.y);
double slope = yDiff/xDiff;
return slope;
}
}