shape8

Circle.java  [download]

/*
 * Now, let's change implementation of circle
 * to use bounding box, starting at top left
 *
 * Note that Main.java doesn't change at all
 */

class Circle extends Shape {

	private int width, height; //*1 Change internal representation

	public Circle (int x, int y, int rad) { //*2 But not public API
		this.x = x - rad; //*3 Convert from API to internal
		this.y = y - rad; //*3
		width = 2 * rad; //*3
		height = 2 * rad; //*3
	}

	public void setSize (int rad) { //*2
		width = 2 * rad; //*3
		height = 2 * rad; //*3
	}

	public void draw() {
		System.out.println ("Drawing circle at "
			+ (x + width/2) + ", " + (y + height/2) //*3
			+ ", radius = " + width/2); //*3
	}

	public float area() {
		return (float) ((width/2) * (width/2) * 3.14159); //*3
	}
}

Rect.java  [download]

/*
 * Definition of subclass Rect
 */

class Rect extends Shape {

	private int wid; //*1 Change internal representation
	private float aspect; //*1

	public Rect (int xin, int yin, int widin, int htin) { //*2 But not public API
		x = xin; y = yin;
		// convert from given inputs to our internal data
		wid = widin;
		aspect = (float)widin/htin; //*3 Convert from API to internal
	}

	public void setSize (int widin, int htin) { //*2
		wid = widin; //*3
		aspect = (float)widin/htin; //*3
	}

	public void draw() {
		System.out.println ("Drawing rectangle at " 
				    + x + ", " + y + ", " + wid + ", " + wid/aspect); //*3
	}

	public float area() {
		return (float) (wid * wid / aspect); //*3
	}
}