shape4

Rect.java  [download]

/*
 * Definition of class Rect
 */

class Rect { //*1 Class definition

	public int x, y, wid, ht; //*2 Instance variables

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

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

Circle.java  [download]

/*
 * Definition of class Circle
 */

class Circle { //*1 Class definition

	public int x, y, rad; //*2 Instance variables

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

	public float area() { //*3
		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[]) { //*6 Main program

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

		/*
		 * Instantiate the objects (could have done in defs above)
		 */
		r1 = new Rect (); //*2 Instantiate
		r2 = new Rect (); //*2
		c1 = new Circle (); //*2

		/*
		 * Set the locations of the 3 objects
		 */
		r1.x = 10; r1.y = 10; //*3 Set locations
		r1.wid = 50; r1.ht = 50; //*3

		r2.x = 80; r2.y = 10; //*3
		r2.wid = 10; r2.ht = 100; //*3

		c1.x = 35; c1.y = 90; //*3
		c1.rad = 10; //*3

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

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