Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

8. Provide recommendations for how to manage knowledge.

Answered: 1 week ago