SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Classes
Exercise:
Exercises
Question:17 | ISBN:9780136091813 | Edition: 2

Question

Add the following method to your Rectangle class:
public Rectangle intersection(Rectangle rect)
Return a new Rectangle that represents the largest rectangular region completely contained within both this Rectangle and the given other Rectangle. If the Rectangles do not intersect at all, returns a Rectangle with width and height both equal to 0.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

// package classes;

public class Rectangle {

	int x;
	int y;
	int width;
	int height;
	Point point;

//initialize the constructor with point, width and height
	public Rectangle(Point p, int width, int height) {

		this.point = p;
		this.width = width;
		this.height = height;
	}

	public Rectangle(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;

	}

     // this method returns the intersection of two rectangles
    public Rectangle intersection(Rectangle rect) {
		if (this.x + this.width < rect.x || rect.x + rect.width < this.x
				|| this.y + this.height < rect.y || rect.y + rect.height < this.y)
			return null;

		int x1 = Math.max(this.x, rect.x);
		int y1 = Math.max(this.y, rect.y);
		int x2 = Math.min(this.x + this.width, rect.x + rect.width);
		int y2 = Math.min(this.y + this.height, rect.y + rect.height);

		return new Rectangle(x1, y1, x2 - x1, y2 - y1);
	}

   public boolean contains(int x, int y) {
		return this.x <= x && x <= this.x + width && this.y <= y && y <= this.y + height;
	}

	public boolean contains(Point p) {
		return contains(p.getX(), p.getY());
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	@Override
	public String toString() {

		return "[Rectangle x = " + this.x + ", y = " + this.y + ", width = " + width + ", height =  " + height + "]";

	}

	public static void main(String[] args) {

		Rectangle rec = new Rectangle(4, 6, 8, 5);
		Rectangle rec1 = new Rectangle(8, 5, 7, 5);

		System.out.println("New Rectangle formed: " + rec.intersection(rec1));
	

	}

}
Output:

New Rectangle formed: [Rectangle x = 8, y = 6, width = 4, height =  4]

 

0 0

Discussions

Post the discussion to improve the above solution.