Guest User

Untitled

a guest
Apr 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. public class Rectangle {
  2.     private int x;
  3.     private int y;
  4.     private int width;
  5.     private int height;
  6.  
  7.     // Constructs a new Rectangle with the given x,y,w,h.
  8.     public Rectangle(int x, int y, int w, int h)
  9.     {
  10.         this.x = x;
  11.         this.y = y;
  12.         width = w;
  13.         height = h;
  14.     }
  15.  
  16.     // returns the field values
  17.     public int getX()
  18.     {
  19.         return x;
  20.     }
  21.     public int getY()
  22.     {
  23.         return y;
  24.     }
  25.     public int getWidth()
  26.     {
  27.         return width;
  28.     }
  29.     public int getHeight()
  30.     {
  31.         return height;
  32.     }
  33.  
  34.     // example: {(5,12), 4x8}
  35.     public String toString()
  36.     {
  37.         return "("+5+","+y+"), "+width+"x"+height;
  38.     }
  39.  
  40.     // your method would go here
  41.     public int max(int[] nums)
  42.     {
  43.         int max = nums[0];
  44.         for(int i : nums)
  45.         {
  46.             if(i > max)
  47.                 max = i;
  48.         }
  49.         return max;
  50.     }
  51.  
  52.     public int min(int[] nums)
  53.     {
  54.         int min = nums[0];
  55.         for(int i : nums)
  56.         {
  57.             if(i < min)
  58.                 min = i;
  59.         }
  60.         return min;
  61.     }
  62.  
  63.     public void union(Rectangle r)
  64.     {
  65.         int a = x;
  66.         int b = x + width;
  67.         int c = y;
  68.         int d = y + height;
  69.         int e = r.getX();
  70.         int f = r.getX() + r.getWidth();
  71.         int g = r.getY();
  72.         int h = r.getY() + r.getHeight();
  73.         int[] rows = new int[4];
  74.         rows[0] = a;
  75.         rows[1] = b;
  76.         rows[2] = e;
  77.         rows[3] = f;
  78.         int[] cols = new int[4];
  79.         cols[0] = c;
  80.         cols[1] = d;
  81.         cols[2] = g;
  82.         cols[3] = h;
  83.         int w = max(rows) - min(rows);
  84.         int h2 = max(cols) - min(cols);
  85.         int newX = min(rows);
  86.         int newY = min(cols);
  87.         x = newX;
  88.         y = newY;
  89.         width = w;
  90.         height = h2;
  91.     }
  92. }
Add Comment
Please, Sign In to add comment