Advertisement
fosterbl

Point.java COMPLETE

Feb 13th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. public class Point{
  2.  
  3.    private int x, y;
  4.  
  5.    public Point(){
  6.       x = 0;
  7.       y = 0;
  8.    }
  9.  
  10.    public Point( int theX, int theY ){
  11.       x = theX;
  12.       y = theY;
  13.    }
  14.  
  15.    public int getX(){
  16.       return x;
  17.    }
  18.    
  19.    public int getY(){
  20.       return y;
  21.    }
  22.  
  23.    public void setX( int theX ){
  24.       x = theX;
  25.    }
  26.    
  27.    public void setY( int theY ){
  28.       y = theY;
  29.    }
  30.  
  31.    public String toString(){
  32.       return "(" + x + ", " + y + ")";
  33.    }
  34. /*Add the following method to the Point class:
  35.  
  36. public int quadrant()
  37.  
  38. Returns which quadrant of the x/y plane this Point object falls in. Quadrant 1 contains all points whose x and y values are both positive. Quadrant 2 contains all points with negative x but positive y. Quadrant 3 contains all points with negative x and y values. Quadrant 4 contains all points with positive x but negative y. If the point lies directly on the x and/or y axis, return 0.
  39. */
  40.    public int quadrant(){
  41.       if( y == 0 || x == 0 )
  42.          return 0;
  43.       else if( x > 0 && y > 0 )
  44.          return 1;
  45.       else if( x < 0 && y > 0 )
  46.          return 2;
  47.       else if( x < 0 && y < 0 )
  48.          return 3;
  49.       else
  50.          return 4;
  51.    }
  52.  
  53.    public void flip(){
  54.       int temp = x;
  55.       x = -y;
  56.       y = -temp;
  57.    }
  58.  
  59. /*Add the following method to your Point class:
  60.  
  61. public boolean isVertical(Point other)
  62.  
  63. Returns true if the given Point lines up vertically with this Point; that is, if their x coordinates are the same.
  64. */
  65.    public boolean isVertical(Point other){
  66.       if( x == other.getX() )
  67.          return true;
  68.       else
  69.          return false;
  70.    }
  71.  
  72. /*Add the following method to the Point class:
  73.  
  74. public double slope(Point other)
  75.  
  76. Returns the slope of the line drawn between this Point and the given other Point. Use the formula (y2 - y1) / (x2 - x1) to determine the slope between two points (x1, y1) and (x2, y2). Note that this formula fails for points with identical x-coordinates
  77. */
  78.    public double slope(Point other){
  79.       return (1.0 * y - other.getY())/(1.0 * x - other.getX() );
  80.    }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement