Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rectangle {
- private int x;
- private int y;
- private int width;
- private int height;
- // Constructs a new Rectangle with the given x,y,w,h.
- public Rectangle(int x, int y, int w, int h)
- {
- this.x = x;
- this.y = y;
- width = w;
- height = h;
- }
- // returns the field values
- public int getX()
- {
- return x;
- }
- public int getY()
- {
- return y;
- }
- public int getWidth()
- {
- return width;
- }
- public int getHeight()
- {
- return height;
- }
- // example: {(5,12), 4x8}
- public String toString()
- {
- return "("+5+","+y+"), "+width+"x"+height;
- }
- // your method would go here
- public int max(int[] nums)
- {
- int max = nums[0];
- for(int i : nums)
- {
- if(i > max)
- max = i;
- }
- return max;
- }
- public int min(int[] nums)
- {
- int min = nums[0];
- for(int i : nums)
- {
- if(i < min)
- min = i;
- }
- return min;
- }
- public void union(Rectangle r)
- {
- int a = x;
- int b = x + width;
- int c = y;
- int d = y + height;
- int e = r.getX();
- int f = r.getX() + r.getWidth();
- int g = r.getY();
- int h = r.getY() + r.getHeight();
- int[] rows = new int[4];
- rows[0] = a;
- rows[1] = b;
- rows[2] = e;
- rows[3] = f;
- int[] cols = new int[4];
- cols[0] = c;
- cols[1] = d;
- cols[2] = g;
- cols[3] = h;
- int w = max(rows) - min(rows);
- int h2 = max(cols) - min(cols);
- int newX = min(rows);
- int newY = min(cols);
- x = newX;
- y = newY;
- width = w;
- height = h2;
- }
- }
Add Comment
Please, Sign In to add comment