shape5

Rect.java  [download]

/*
 * Definition of class Rect
 */

class Rect {

	private int x, y, wid, ht;

	public Rect (int xin, int yin, int widin, int htin) { //*1 Constructor
		x = xin; y = yin; //*1
		wid = widin; ht = htin; //*1
	}

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

	public float area() {
		return (float) (wid * ht);
	}
}

Circle.java  [download]

/*
 * Definition of class Circle
 */

class Circle {

	private int x, y, rad;

	public Circle (int xin, int yin, int radin) { //*1 Constructor
		x = xin; y = yin; //*1
		rad = radin; //*1
	}

	public void draw() {
		System.out.println ("Drawing circle at " + x + ", " + y
			+ ", radius = " + rad);
	}

	public float area() {
		return (float) (rad * rad * 3.14159);
	}
}

Main.java  [download]

/*
 * Just a main program for testing our objects
 */

class Main {

	// The main program
	public static void main (String args[]) {

		/*
		 * Data definitions
		 */
		Rect r1, r2;
		Circle c1;

		/*
		 * Instantiate and initialize the objects
		 * (could have done in defs above)
		 */
		r1 = new Rect (10, 10, 50, 50); //*1 Use constructors
		r2 = new Rect (80, 10, 10, 100); //*1
		c1 = new Circle (35, 90, 10); //*1

		/*
		 * "Draw" them
		 */
		r1.draw();
		r2.draw();
		c1.draw();

		/*
		 * Compute and print total area
		 */
		System.out.println ("Total area = " +
			(r1.area() + r2.area() + c1.area()));
	}
}