Shape programs, from C to C++ to Java

File: shape/shape1.c

/* 
 * First example: shapes
 * 1. Bad old way, in plain C
 *
 * RunStuff:: gcc THISFILE
 *
 * Picture looks roughly like this:
 *
 *   *************************          *****
 *   *                       *          *   *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *        r1             *          *r2 *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *************************          *   *
 *                                      *   *
 *                                      *   *
 *            *  *                      *   *
 *          *      *                    *   *
 *         *        *                   *   *
 *         *   c1   *                   *   *
 *          *      *                    *   *
 *            *  *                      *   *
 *                                      *****
 */

#include <stdio.h>

/*
 * Data definitions
 */
int r1x, r1y, r1wid, r1ht;
int r2x, r2y, r2wid, r2ht;
int c1x, c1y, c1rad;

/*
 * Procedure definitions
 */
void draw_rect (int x, int y, int wid, int ht) {
	printf ("Drawing rectangle at %d, %d, %d, %d\n", x, y, wid, ht);
}

void draw_circle (int x, int y, int rad) {
	printf ("Drawing circle at %d, %d, radius = %d\n", x, y, rad);
}

float area_rect (int x, int y, int wid, int ht) {
	return (float) (wid * ht);
}

float area_circle (int x, int y, int rad) {
	return (float) (rad * rad * 3.14159);
}

/*
 * Main program
 */
main () {
	/*
	 * Set the locations of the 3 objects
	 */
	r1x = 10; r1y = 10;
	r1wid = 50; r1ht = 50;

	r2x = 80; r2y = 10;
	r2wid = 10; r2ht = 100;

	c1x = 35; c1y = 90;
	c1rad = 10;

	/*
	 * Draw them
	 */
	draw_rect (r1x, r1y, r1wid, r1ht);
	draw_rect (r2x, r2y, r2wid, r2ht);
	draw_circle (c1x, c1y, c1rad);

	/*
	 * Compute and print total area
	 */
	printf ("Total area = %f\n",
		area_rect (r1x, r1y, r1wid, r1ht)
		+ area_rect (r2x, r2y, r2wid, r2ht)
		+ area_circle (c1x, c1y, c1rad));
}



File: shape/shape2.c

/* 
 * First example: shapes
 * 2. Modularized, uses typedefs, but still in C
 *
 * RunStuff:: gcc THISFILE
 */

#include <stdio.h>

/*
 * Type definitions
 */
typedef struct {
	int x, y, wid, ht;
} Rect;

typedef struct {
	int x, y, rad;
} Circle;

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

/* 
 * Procedure definitions
 */
void drawRect (Rect r) {
	printf ("Drawing rectangle at %d, %d, %d, %d\n", r.x, r.y, r.wid, r.ht);
}

void drawCircle (Circle c) {
	printf ("Drawing circle at %d, %d, radius = %d\n", c.x, c.y, c.rad);
}

float areaRect (Rect r) {
	return (float) (r.wid * r.ht);
}

float areaCircle (Circle c) {
	return (float) (c.rad * c.rad * 3.14159);
}

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

	r2.x = 80; r2.y = 10;
	r2.wid = 10; r2.ht = 100;

	c1.x = 35; c1.y = 90;
	c1.rad = 10;

	/*
	 * Draw them
	 */
	drawRect (r1);
	drawRect (r2);
	drawCircle (c1);

	/*
	 * Compute and print total area
	 */
	printf ("Total area = %f\n",
		areaRect (r1) + areaRect (r2) + areaCircle (c1));
}


File: shape/shape3.cc

/* 
 * First example: shapes
 * OOP and C++
 * Note how the main program calls to draw and area become cleaner
 *
 * RunStuff:: g++ THISFILE Rect.cc Circle.cc
 */

#include <stdio.h>

/*
 * Definition of class Rect
 */
class Rect {
public:
	int x, y, wid, ht;
	void draw();
	float area();
};

/*
 * Definition of class Circle
 */
class Circle {
public:
	int x, y, rad;
	void draw();
	float area();
};

main () {
	/*
	 * Data definitions
	 */
	Rect r1, r2;
	Circle c1;

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

	r2.x = 80; r2.y = 10;
	r2.wid = 10; r2.ht = 100;

	c1.x = 35; c1.y = 90;
	c1.rad = 10;

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

	/*
	 * Compute and print total area
	 */
	printf ("Total area = %f\n", r1.area() + r2.area() + c1.area());
}

/*
 * Methods for class Rect
 */

void Rect::draw () {
	printf ("Drawing rectangle at %d, %d, %d, %d\n", x, y, wid, ht);
}

float Rect::area () {
	return (float) (wid * ht);
}

/*
 * Methods for class Circle
 */

void Circle::draw () {
	printf ("Drawing circle at %d, %d, radius = %d\n", x, y, rad);
}

float Circle::area () {
	return (float) (rad * rad * 3.14159);
}


File: shape/shape4/Rect.java

/*
 * Definition of class Rect
 */

class Rect {

	public int x, y, wid, ht;

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

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

File: shape/shape4/Circle.java

/*
 * Definition of class Circle
 */

class Circle {

	public int x, y, rad;

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

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

File: shape/shape4/Main.java

/*
 * 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 the objects (could have done in defs above)
		 */
		r1 = new Rect ();
		r2 = new Rect ();
		c1 = new Circle ();

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

		r2.x = 80; r2.y = 10;
		r2.wid = 10; r2.ht = 100;

		c1.x = 35; c1.y = 90;
		c1.rad = 10;

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

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

File: shape/shape5/Rect.java

/*
 * Definition of class Rect
 */

class Rect {

	private int x, y, wid, ht;

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

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

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

File: shape/shape5/Circle.java

/*
 * Definition of class Circle
 */

class Circle {

	private int x, y, rad;

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

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

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

File: shape/shape5/Main.java

/*
 * 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);
		r2 = new Rect (80, 10, 10, 100);
		c1 = new Circle (35, 90, 10);

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

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

File: shape/shape6/Rect.java

/*
 * Definition of class Rect
 */

class Rect {

	private int x, y, wid, ht;

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

	public void setPos (int xin, int yin) {
		x = xin; y = yin;
	}

	public void setSize (int widin, int htin) {
		wid = widin; ht = htin;
	}

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

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

File: shape/shape6/Circle.java

/*
 * Definition of class Circle
 */

class Circle {

	private int x, y, rad;

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

	public void setPos (int xin, int yin) {
		x = xin; y = yin;
	}

	public void setSize (int radin) {
		rad = radin;
	}

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

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

File: shape/shape6/Main.java

/*
 * 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);
		r2 = new Rect (80, 10, 10, 100);
		c1 = new Circle (35, 90, 10);

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

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

File: shape/shape7/Shape.java

/*
 * Definition of superclass Shape
 */

abstract class Shape {
	// protected, not private, cause is used by our subclasses
	protected int x, y;

	public void setPos (int xin, int yin) {
		x = xin; y = yin;
	}

	abstract public void draw();

	abstract public float area();
}

File: shape/shape7/Rect.java

/*
 * Definition of subclass Rect
 */

class Rect extends Shape {

	private int wid, ht;

	public Rect (int xin, int yin, int widin, int htin) {
		// Note that "x" and "y" are inherited from Shape
		x = xin; y = yin;
		// but wid and ht are defined by Rect
		wid = widin; ht = htin;
	}

	public void setSize (int widin, int htin) {
		wid = widin; ht = htin;
	}

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

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

File: shape/shape7/Circle.java

/*
 * Definition of class circle
 */

class Circle extends Shape {

	private int rad;

	// N.B. This shows how you can use same var name in ivar and arg
	public Circle (int x, int y, int rad) {
		this.x = x; this.y = y;
		this.rad = rad;
	}

	public void setSize (int rad) {
		this.rad = rad;
	}

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

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

File: shape/shape7/Main.java

/*
 * 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 the objects (could have done in defs above)
		 */
		r1 = new Rect (10, 10, 50, 50);
		r2 = new Rect (80, 10, 10, 100);
		c1 = new Circle (35, 90, 10);

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

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

File: shape/shape8/Circle.java

/*
 * Now, let's change implementation of circle
 *
 * Note that the other *.java files don't change at all
 */

class Circle extends Shape {

	private float diam;

	public Circle (int x, int y, int rad) {
		this.x = x; this.y = y;
		diam = (float) (2.0 * rad);
	}

	public void setSize (int rad) {
		diam = (float) (2.0 * rad);
	}

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

	public float area() {
		return (float) ((diam/2.) * (diam/2.) * 3.14159);
	}
}

File: shape/shape8/Rect.java

/*
 * Definition of subclass Rect
 */

class Rect extends Shape {

	private int wid;
	private float aspect;

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

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

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

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

File: shape/shape9/Main.java

/*
 * Just a main program for testing our objects
 * this time exploiting polymorphism
 * Note that the other *.java files don't change
 */

class Main {

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

		/*
		 * Data definitions
		 */
		Shape s1, s2, s3;

		/*
		 * And now, instantiate the objects
		 */
		s1 = new Rect (10, 10, 50, 50);
		s2 = new Rect (80, 10, 10, 100);
		s3 = new Circle (35, 90, 10);

		/*
		 * "Draw" them
		 */
		s1.draw ();
		s2.draw ();
		s3.draw ();

		/*
		 * Compute and print total area
		 */
		System.out.println ("Total area = " + 
			(s1.area() + s2.area() + s3.area()));
	}
}