// Arup Guha
// 7/20/04
// Solution to 2004 BHCSI Programming Assignment Point.java
// This class has several methods that operate on Point objects and has
// a main included to test those methods out.


public class Point {

  // Instance variables.
  private int x;
  private int y;

  // Default constructor, sets object to (0,0).
  public Point() {
    x = 0;
    y = 0;
  }

  // Sets object to (xval, yval).
  public Point(int xval, int yval) {
    x = xval;
    y = yval;
  }

  // Returns the sum of the coordinates of the current object.
  public int sumCoordinates() {
    return x+y;
  }

  // Returns the distance of the current object from the origin.
  public double getDistance() {
    return Math.sqrt(x*x+y*y);
  }

  // Returns the String form of the object as an ordered pair.
  public String toString() {
    return "("+x+","+y+")";
  }

  // Reflects the current object over the X-axis.
  public void reflectXaxis() {
    y = -y;
  }

  // Reflects the current object over the Y-axis.
  public void reflectYaxis() {
    x = -x;
  }

  // Reflects the current object over the origin.
  public void reflectOrigin() {
    x = -x;
    y = -y;
  }

  // Returns true if and only if the current object and p2 represent the
  // same point.
  public boolean equals(Point p2) {
    if (x == p2.x && y == p2.y)
      return true;
    else
      return false;
  }

  // Sets the x coordinate of the current object to xval.
  public void setX(int xval) {
    x = xval;
  }

  // Sets the y coordinate of the current object to yval.
  public void setY(int yval) {
    y = yval;
  }

  // Multiplies the current object by the scalar c.
  public void multC(int c) {
    x = c*x;
    y = c*y;
  }

  // Translates the current object by p2.
  public void translate(Point p2) {
    x = x+p2.x;
    y = y+p2.y;
  }

  // Returns true if and only if the current object and p2 form a vertical
  // line.
  public boolean vertical(Point p2) {
    if (x == p2.x)
      return true;
    else
      return false;
  }

  // Returns true if and only if the current object and p2 form a 
  // horizontal line.
  public boolean horizontal(Point p2) {
    if (y == p2.y)
      return true;
    else
      return false;
  }

  // Returns the slope between the current object and p2.
  public double slope(Point p2) {

    int deltax = p2.x-x;
    int deltay = p2.y-y;
    double s = 1.0*deltay/deltax;
    return s;

  }

  public static void main(String[] args) {

    Point p1 = new Point();
    Point p2 = new Point(7, 10);
    Point p3 = new Point(-4, -4);

    // Test sumCoordinates and getDistance.
    System.out.println("Sum of p3's coordinates is "+p3.sumCoordinates());
    System.out.println("The distance of p2 to the origin is "+p2.getDistance());    

    // Test reflections.
    p2.reflectXaxis();
    System.out.println("After reflect X-axis, p2 is now at "+p2);

    p3.reflectXaxis();
    System.out.println("After reflect Y-axis, p3 is now at "+p3);

    Point p4 = new Point(-3,5);
    p4.reflectOrigin();
    System.out.println("After reflect origin, p4 is now at "+p4);

    // Test equals, should test other way too.
    if (!p3.equals(p4))
      System.out.println("p3 and p4 are not equal.");

    // Test translation - notice how the constructor is called here.
    p3.translate(new Point(7,-9));
    if (p3.equals(p4))
      System.out.println("After translation, p3 and p4 are equal!");

    // Test mutators.
    p4.setX(1);
    p4.setY(-5);
    System.out.println("p4 is now at "+p4);

    // Test multC.
    p2.multC(2);
    System.out.println("p2 is now multiplied and is now at "+p2);

    // Test vertical and slope methods.
    Point p5 = new Point(1,3);
    if (p4.vertical(p5))
      System.out.println("p4 and p5 form a vertical line.");

    if (!p2.vertical(p5)) {
      System.out.println("p2 and p5 do not form a vertical line.");
      System.out.println("The slope between them is "+p2.slope(p5));
    }

    // Test horizontal method.
    Point p6 = new Point(4,-5);
    if (p4.horizontal(p6))
      System.out.println("p4 and p6 form a horizontal line.");
    if (!p2.horizontal(p6)) 
      System.out.println("p2 and p6 do not form a vertical line.");
     
  }
}
