Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package A1;
  2.  
  3. public class Window {
  4.     protected double left;
  5.     protected double right;
  6.     protected double bottom;
  7.     protected double top;
  8.  
  9.     public Window(double left, double right, double bottom, double top) {
  10.         this.left = left;
  11.         this.right = right;
  12.         this.bottom = bottom;
  13.         this.top = top;
  14.  
  15.         setLeft(left);
  16.         setRight(right);
  17.         setBottom(bottom);
  18.         setTop(top);
  19.     }
  20.  
  21.     public double getLeft() {
  22.         return left;
  23.     }
  24.  
  25.     public void setLeft(double left) {
  26.         if (left >= right) {
  27.             throw new InvalidWindowException();
  28.         }
  29.         this.left = left;
  30.     }
  31.  
  32.     public double getRight() {
  33.         return right;
  34.     }
  35.  
  36.     public void setRight(double right) {
  37.         if (right <= left) {
  38.             throw new InvalidWindowException();
  39.         }
  40.         this.right = right;
  41.     }
  42.  
  43.     public double getTop() {
  44.         return top;
  45.     }
  46.  
  47.     public void setTop(double top) {
  48.         if (top <= bottom) {
  49.             throw new InvalidWindowException();
  50.         }
  51.         this.top = top;
  52.     }
  53.  
  54.     public double getBottom() {
  55.         return bottom;
  56.     }
  57.  
  58.     public void setBottom(double bottom) {
  59.         if (bottom >= top) {
  60.             throw new InvalidWindowException();
  61.         }
  62.         this.bottom = bottom;
  63.     }
  64.  
  65.     public boolean encloses(Window w) {
  66.         return w.top < this.top && w.left > this.left && w.bottom > this.bottom && w.right < this.right;
  67.     }
  68.  
  69.     public boolean overlaps(Window w) {
  70.         return !(w.right < this.left ||   // left
  71.                 w.top < this.bottom ||   // bottom
  72.                 w.left > this.right ||   // right
  73.                 w.bottom > this.top);    // top
  74.     }
  75.  
  76.     // disclaimer: assuming the word "pair" in the assignment description to be an unordered pair
  77.     // ie. (a, b) = (b, a)
  78.     public static int overlapCount(Window[] windows) {
  79.         int res = 0;
  80.         for (int i = 0; i < windows.length; i++) {
  81.             Window a = windows[i];
  82.             for (int j = i + 1; j < windows.length; j++) { // symmetric relationship, so we already checked windows
  83.                 Window b = windows[j];
  84.                 if (a.overlaps(b)) {
  85.                     res += 1;
  86.                 }
  87.             }
  88.         }
  89.  
  90.         return res;
  91.     }
  92.  
  93.     // disclaimer: assuming the word "pair" in the assignment description to be an unordered pair
  94.     // ie. (a, b) = (b, a)
  95.     public static int enclosureCount(Window[] windows) {
  96.         int res = 0;
  97.         for (int i = 0; i < windows.length; i++) {
  98.             Window a = windows[i];
  99.             for (int j = 0; j < windows.length; j++) { // non-symmetric relationship, so we still need to check for every n^2 pairs
  100.                 Window b = windows[j];
  101.                 if (a.encloses(b)) {
  102.                     res += 1;
  103.                 }
  104.             }
  105.         }
  106.  
  107.         return res;
  108.     }
  109.  
  110.     public static void main(String[] args) {
  111.         // TODO: runs some interesting test cases of the above methods
  112.        
  113.         Window w1 = new Window(0, 10, 0, 10);
  114.         Window w2 = new Window(1, 5, 1, 5);
  115.        
  116.         TestHelper.verify(w1.overlaps(w2), "Wrong: they should overlap!!!  No marshmallow.");
  117.         TestHelper.verify(w2.overlaps(w1), "Wrong: they should overlap!!!  No marshmallow.");
  118.         TestHelper.verify(w1.encloses(w2), "Wrong: they should enclose!!!  No marshmallow.");
  119.         TestHelper.verify(!w2.encloses(w1), "Wrong: they should not enclose!!!  No marshmallow.");
  120.  
  121.         Window[] windows = new Window[]{w1, w2};
  122.  
  123.         TestHelper.verify(overlapCount(windows) == 1, "Wrong: they should have exactly 1 overlap!!!  No marshmallow.");
  124.         TestHelper.verify(enclosureCount(windows) == 1, "Wrong: they should have exactly 1 enclosure!!!  No marshmallow.");
  125.     }
  126.  
  127.     public class InvalidWindowException extends IllegalArgumentException {
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement