Skip to content

Commit 460c472

Browse files
committed
proper exceptions (50/100)
1 parent 7458295 commit 460c472

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

week5/balanced-search-trees/assignment-kd-trees/KdTree.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ private void rangeSearch(RectHV rect, Node node, ArrayList<Point2D> points) {
174174
}
175175
// // a nearest neighbor in the set to point p; null if the set is empty
176176
public Point2D nearest(Point2D p) {
177+
if (p == null) throw new IllegalArgumentException("nearest method called with a null argument");
177178
return nearestSearch(p, this.root, null, Double.POSITIVE_INFINITY);
178179
}
179180

week5/balanced-search-trees/assignment-kd-trees/PointSET.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public int size() {
2020
}
2121
// add the point to the set (if it is not already in the set)
2222
public void insert(Point2D p) {
23+
if (p == null) throw new IllegalArgumentException("insert method called with a null argument");
2324
tree.add(p);
2425
}
2526
// // does the set contain point p?
2627
public boolean contains(Point2D p) {
28+
if (p == null) throw new IllegalArgumentException("contains method called with a null argument");
2729
return tree.contains(p);
2830
}
2931
// draw all points to standard draw
@@ -34,6 +36,7 @@ public void draw() {
3436
}
3537
// all points that are inside the rectangle (or on the boundary)
3638
public Iterable<Point2D> range(RectHV rect) {
39+
if (rect == null) throw new IllegalArgumentException("range method called with a null argument");
3740
ArrayList<Point2D> points = new ArrayList<Point2D>();
3841
for (Point2D p : tree) {
3942
if (rect.contains(p)) {
@@ -44,6 +47,7 @@ public Iterable<Point2D> range(RectHV rect) {
4447
}
4548
// // a nearest neighbor in the set to point p; null if the set is empty
4649
public Point2D nearest(Point2D p) {
50+
if (p == null) throw new IllegalArgumentException("nearest method called with a null argument");
4751
Point2D nearest = null;
4852
double minDistance = Double.POSITIVE_INFINITY;
4953
for (Point2D point : tree) {

0 commit comments

Comments
 (0)