Question: public class Rect { private final int xmin; private final int ymin; private final int xmax; private final int ymax; Rect(int xmin, int ymin, int
| public class Rect { | |
| private final int xmin; | |
| private final int ymin; | |
| private final int xmax; | |
| private final int ymax; | |
| Rect(int xmin, int ymin, int xmax, int ymax) { | |
| this.xmin = xmin; | |
| this.ymin = ymin; | |
| this.xmax = xmax; | |
| this.ymax = ymax; | |
| } | |
| /** | |
| * Returns true if the point (x,y) is inside the | |
| * current rectangle. Touching the sides of the rectangle | |
| * counts as being inside it. | |
| */ | |
| boolean contains(int x, int y) { | |
| return x >= xmin && x <= xmax && | |
| y >= ymin && y <= ymax; | |
| } | |
| /** | |
| * Returns true if the given rectangle r intersects the | |
| * current one. If the two rectangles touch in even | |
| * one point, that counts as an intersection. | |
| */ | |
| boolean intersect(Rect r) { | |
| return false; // TODO | |
| } | |
| public String toString() { | |
| return String.format("R[(%d,%d)--(%d,%d)]", xmin, ymin, xmax, ymax); | |
| } | |
| } |
| import org.junit.jupiter.api.Test; | |
| import static org.junit.jupiter.api.Assertions.*; | |
| class RectTest { | |
| /** | |
| * This is a very minimal version of the method you need to write. | |
| * Your test cases should attempt to cover all patterns of | |
| * intersecting rectangles. | |
| * | |
| * We will run your testing method against several implementations | |
| * of "intersect" that might be incorrect in subtle ways. We expect | |
| * your test cases to pass when the implementation of "intersect" | |
| * is correct and to fail when the implementation of "intersect" | |
| * has errors. | |
| */ | |
| @Test | |
| void intersect() { | |
| Rect r1 = new Rect(0, 0, 10, 10); | |
| Rect r2 = new Rect(20, 20, 30, 30); | |
| Rect r3 = new Rect(5, 5, 15, 15); | |
| Rect r4 = new Rect(10, 10, 15, 15); | |
| Rect r5 = new Rect(0,0,0,0); | |
| Rect r6 = new Rect(1,1,1,1); | |
| Rect r7 = new Rect(-1,-1,1,1); | |
| assertFalse(r1.intersect(r2)); | |
| assertTrue(r1.intersect(r3)); | |
| assertTrue(r1.intersect(r4)); | |
| assertTrue(r1.intersect(r5)); | |
| assertFalse(r6.intersect(r5)); | |
| assertTrue(r5.intersect(r7)); | |
| assertTrue(r7.intersect(r5)); | |
| } | |
| } |
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
