Advertisement
jules0707

LineSegment.java

Nov 24th, 2020
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac LineSegment.java
  3.  *  Execution:    none
  4.  *  Dependencies: Point.java
  5.  *
  6.  *  An immutable data type for Line segments in the plane.
  7.  *  For use on Coursera, Algorithms Part I programming assignment.
  8.  *
  9.  *  DO NOT MODIFY THIS CODE.
  10.  *
  11.  *************************************************************************/
  12.  
  13. public class LineSegment {
  14.     private final Point p;   // one endpoint of this line segment
  15.     private final Point q;   // the other endpoint of this line segment
  16.  
  17.     /**
  18.      * Initializes a new line segment.
  19.      *
  20.      * @param  p one endpoint
  21.      * @param  q the other endpoint
  22.      * @throws NullPointerException if either <tt>p</tt> or <tt>q</tt>
  23.      *         is <tt>null</tt>
  24.      */
  25.     public LineSegment(Point p, Point q) {
  26.         if (p == null || q == null) {
  27.             throw new NullPointerException("argument is null");
  28.         }
  29.         this.p = p;
  30.         this.q = q;
  31.     }
  32.  
  33.  
  34.     /**
  35.      * Draws this line segment to standard draw.
  36.      */
  37.     public void draw() {
  38.         p.drawTo(q);
  39.     }
  40.  
  41.     /**
  42.      * Returns a string representation of this line segment
  43.      * This method is provide for debugging;
  44.      * your program should not rely on the format of the string representation.
  45.      *
  46.      * @return a string representation of this line segment
  47.      */
  48.     public String toString() {
  49.         return p + " -> " + q;
  50.     }
  51.  
  52.     /**
  53.      * Throws an exception if called. The hashCode() method is not supported because
  54.      * hashing has not yet been introduced in this course. Moreover, hashing does not
  55.      * typically lead to good *worst-case* performance guarantees, as required on this
  56.      * assignment.
  57.      *
  58.      * @throws UnsupportedOperationException if called
  59.      */
  60.     public int hashCode() {
  61.         throw new UnsupportedOperationException();
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement