-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineSegment.java
67 lines (60 loc) · 2.08 KB
/
LineSegment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*************************************************************************
* Compilation: javac LineSegment.java
* Execution: none
* Dependencies: Point.java
*
* An immutable data type for Line segments in the plane.
* For use on Coursera, Algorithms Part I programming assignment.
*
* DO NOT MODIFY THIS CODE.
*
*************************************************************************/
public class LineSegment {
private final Point p; // one endpoint of this line segment
private final Point q; // the other endpoint of this line segment
/**
* Initializes a new line segment.
*
* @param p one endpoint
* @param q the other endpoint
* @throws NullPointerException if either <tt>p</tt> or <tt>q</tt>
* is <tt>null</tt>
*/
public LineSegment(Point p, Point q) {
if (p == null || q == null) {
throw new IllegalArgumentException("argument to LineSegment constructor is null");
}
if (p.equals(q)) {
throw new IllegalArgumentException("both arguments to LineSegment constructor are the same point: " + p);
}
this.p = p;
this.q = q;
}
/**
* Draws this line segment to standard draw.
*/
public void draw() {
p.drawTo(q);
}
/**
* Returns a string representation of this line segment
* This method is provide for debugging;
* your program should not rely on the format of the string representation.
*
* @return a string representation of this line segment
*/
public String toString() {
return p + " -> " + q;
}
/**
* Throws an exception if called. The hashCode() method is not supported because
* hashing has not yet been introduced in this course. Moreover, hashing does not
* typically lead to good *worst-case* performance guarantees, as required on this
* assignment.
*
* @throws UnsupportedOperationException if called
*/
public int hashCode() {
throw new UnsupportedOperationException("hashCode() is not supported");
}
}