Quick Index
Video


A video on the full example is here....

Overview


For the full example we'll be playing with our favorite object: a Rectangle.

We may abbreviate it as "rec".

The following show a few features we'll look at.

Rectangle
Our rec has Four properties (ivars)

leftTop -- a point that signifies where the left-top corner of the rectangle is
width -- width of rectangle
height -- height of rectangle
Min and Max (Dimensions)
We'll be interested in knowing the min and max dimension of the rec.

Max is the larger of the two dimension width and height. Height is the smaller dimension.
Contains By Size
We'll determine if one rectangle can fit inside another.

We only consider rectangle size. We ignore rectangle position.
Contains By Size (Rotated)
We may rotate the smaller rectangle to determine if it fits in the larger.

We only consider rectangle size. We ignore rectangle position.


Problem Statement


Enhance Rectangle with some logic.

Add the following methods:

Notes:


Method Name
Description
isSquare
Return true if the rectangle is a square (width=height). Otherwise return false.
hasInitialdims
Return true if the rectangle has the initial dims (both dims equal to zero).
isWider
Return true if width > height
print
Receives boolean parameter "detailed". If "detailed" is true, print detailed rectangle info, else print just width and height. Detailed info should include at least three things e.g. hasInitialdims, isWider, isSquare.
doubleSmallestDimension
Double the smaller of the two rectangle dims
isPointAbove
Returns true if point argument is above this rectangle
containsBoth
Returns true if this rectangle contains both parameters (points)
canContainBySize
Receives "otherRec" as parameter. Return true if the target rectangle can contain "otherRec". The rectangle can be rotated. The dim should be greater than the otherRec's dim to contain. We only consider rectangle size. We ignore rectangle position.
canFitInsideBySize
Receives "otherRec" as parameter. Return true if the target rectangle can fit inside "otherRec". The rectangle can be rotated. The dim should be less than the otherRec's dim to fit. We only consider rectangle size. We ignore rectangle position.


Solution


Here are most of the methods. See "Source Code" at the bottom of the page to download the complete solution.

Basic Methods


isSquare
The operator "==" returns true if the values are equal, else false
	public boolean isSquare()  {
		return width == height;
	}
hasInitialDimensions
The logical operator "&&" means the left condition AND right condition need to be true
	public boolean hasInitialDimensions()  {
		/*&& is a logical operator for AND.
		It means the left expression
		AND the right expression must be true */
		return (width == 0) && (height == 0);
	}
isWiderTry1
Here we use an if-else statement to return true if width>height, else false
	public boolean isWiderTry1()  {
		//Return true if we are wider (w >= h)
		//width and height are our ivars
		if (width > height)
			return true;
		else
			return false;
	}
isWider
We improve on the previous by noting that the ">" operator already returns a boolean (no need for if-else)
	public boolean isWider()  {
		/* Improved. Because (width > height)
		produces a boolean -- so return it
		without the if/else (not needed) */
		return width > height;
	}
doubleSmallestDimension
If we are wider, we double height, otherwise width.
	public void doubleSmallestDimension()  {
		/*Note: if dims are equal, okay
		to double either*/
		if (isWider())
			height = 2 * height;
		else
			width = 2 * width;
	}
getMax
If we are wider, we double height, otherwise height.
	public int getMax()  {
		//return max dimension
		if (width > height)
			return width;
		else
			return height;
	}
getMin
If we are wider, we double height, otherwise height.
	public int getMin()  {
		//return min dimension
		if (width < height)
			return width;
		else
			return height;
	}


Rectangle-Point Methods


isPointAbove
This Rectangle has the method "isPointAbove" which receives a Point method parameter.
	/** Returns true if point argument is above of this rectangle */
	public boolean isPointAbove(Point p)  {
		return p.getY() < this.leftTop.getY();
	}
contains
This Rectangle has the method "contains" which receives a Point method parameter.
	/**
	 * Returns true if this rectangle contains "otherPoint"
	 * i.e., if "otherPoint" is inside this rectangle
	*/
	public boolean contains(Point otherPoint)  {
		int leftX, topY, rightX, bottomY, otherX, otherY;
		leftX = this.leftTop.getX();
		topY = this.leftTop.getY();
		rightX = leftX + this.width;
		bottomY = topY + this.height;
		otherX = otherPoint.getX();
		otherY = otherPoint.getY();
		return
			otherX >= leftX && otherX <= rightX
				&& otherY >= topY && otherY <= bottomY;
	}
containsBoth
This Rectangle has the method "containsBoth" which receives two Point method parameters.
	/**
	 * Returns true if this rectangle contains both
	 * Point parameters
	*/
	public boolean containsBoth(Point p1, Point p2)  {
		return this.contains(p1) && this.contains(p2);
	}


canContain (and Helpers)


canContainBySize
We need to ask "otherRec" a couple questions to make this logic happen

isMaxLargerThan
	public boolean isMaxLargerThan(int length)  {
		//Return true if our max dimension is larger than the param
		return this.getMax() > length;
	}
isMinLargerThan
	public boolean isMinLargerThan(int length)  {
		//Return true if our min dimension is larger than the param
		return this.getMin() > length;
	}
canFitInsideOfBySize
We reuse here by sending another method because if other can contain this, then this can fit inside other.



print


This is just a little method named "print" that shows a little logic to print various characteristics about the object.

print
If the param option is true, we do the detailed print, otherwise we just print the dims (helper methods are shown)
	public void print(boolean option)  {
		//if arg is true, print details, else basic
		if (option)
			printDetails();
		else
			printDimensions();
	}

	public void printDimensions()  {
		prn("Width: " + width);
		prn("Height: " + height);
	}

	public void printDetails()  {

		//First print the basics
		printDimensions();

		//Now print a rich mix of info about this rec
		if (width == 0 && height == 0)
			prn("Dims are initial (condition 1)");

		if (hasInitialDimensions())
			prn("Dims are initial (condition 2)");

		if (width > 10)
			prn("Width is greater than 10");

		if ((width + height) > 20)
			prn("Dim sum is greater than 20");

		if (isWider())
			prn("Greater width than height");
		else
			prn("Not greater width than height");
	}


Source Code


Download the code here...