Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package A1;
- public class Window {
- protected double left;
- protected double right;
- protected double bottom;
- protected double top;
- public Window(double left, double right, double bottom, double top) {
- this.left = left;
- this.right = right;
- this.bottom = bottom;
- this.top = top;
- setLeft(left);
- setRight(right);
- setBottom(bottom);
- setTop(top);
- }
- public double getLeft() {
- return left;
- }
- public void setLeft(double left) {
- if (left >= right) {
- throw new InvalidWindowException();
- }
- this.left = left;
- }
- public double getRight() {
- return right;
- }
- public void setRight(double right) {
- if (right <= left) {
- throw new InvalidWindowException();
- }
- this.right = right;
- }
- public double getTop() {
- return top;
- }
- public void setTop(double top) {
- if (top <= bottom) {
- throw new InvalidWindowException();
- }
- this.top = top;
- }
- public double getBottom() {
- return bottom;
- }
- public void setBottom(double bottom) {
- if (bottom >= top) {
- throw new InvalidWindowException();
- }
- this.bottom = bottom;
- }
- public boolean encloses(Window w) {
- return w.top < this.top && w.left > this.left && w.bottom > this.bottom && w.right < this.right;
- }
- public boolean overlaps(Window w) {
- return !(w.right < this.left || // left
- w.top < this.bottom || // bottom
- w.left > this.right || // right
- w.bottom > this.top); // top
- }
- // disclaimer: assuming the word "pair" in the assignment description to be an unordered pair
- // ie. (a, b) = (b, a)
- public static int overlapCount(Window[] windows) {
- int res = 0;
- for (int i = 0; i < windows.length; i++) {
- Window a = windows[i];
- for (int j = i + 1; j < windows.length; j++) { // symmetric relationship, so we already checked windows
- Window b = windows[j];
- if (a.overlaps(b)) {
- res += 1;
- }
- }
- }
- return res;
- }
- // disclaimer: assuming the word "pair" in the assignment description to be an unordered pair
- // ie. (a, b) = (b, a)
- public static int enclosureCount(Window[] windows) {
- int res = 0;
- for (int i = 0; i < windows.length; i++) {
- Window a = windows[i];
- for (int j = 0; j < windows.length; j++) { // non-symmetric relationship, so we still need to check for every n^2 pairs
- Window b = windows[j];
- if (a.encloses(b)) {
- res += 1;
- }
- }
- }
- return res;
- }
- public static void main(String[] args) {
- // TODO: runs some interesting test cases of the above methods
- Window w1 = new Window(0, 10, 0, 10);
- Window w2 = new Window(1, 5, 1, 5);
- TestHelper.verify(w1.overlaps(w2), "Wrong: they should overlap!!! No marshmallow.");
- TestHelper.verify(w2.overlaps(w1), "Wrong: they should overlap!!! No marshmallow.");
- TestHelper.verify(w1.encloses(w2), "Wrong: they should enclose!!! No marshmallow.");
- TestHelper.verify(!w2.encloses(w1), "Wrong: they should not enclose!!! No marshmallow.");
- Window[] windows = new Window[]{w1, w2};
- TestHelper.verify(overlapCount(windows) == 1, "Wrong: they should have exactly 1 overlap!!! No marshmallow.");
- TestHelper.verify(enclosureCount(windows) == 1, "Wrong: they should have exactly 1 enclosure!!! No marshmallow.");
- }
- public class InvalidWindowException extends IllegalArgumentException {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement