Quick Index
Index
Example #1 (Provided Information)


Problem Overview


Problem Description
This challenge is to code a simple rectangle as shown (with two properties "width" and "height").


Problem Requirements


The code will be shown as pseudocode and thus will apply to all coding languages.

Required Class Header
This is the required class header.

The class must be public.
class Rectangle
Required Constructor Headers
Three constructors must be coded with the specified constructor headers.

The constructors are static factory methods

The constructors must be public.
//Returns new Rectangle with width and height set to zero.
static newRectangle()

//Returns new Rectangle that is a square with width and height both
//set to method param "aSide"
static fromSide(aSide)

//Returns new Rectangle with width and height set to passed method params
static fromWidthHeight(aWidth, aHeight)


Provided Design


Object Diagram
The object design provides us with a wonderful object diagram.

The object diagram is like gold to a coder because it is so helpful to code the object structure.
Rectangle
  |
  +------- width ------- an integer
  |
  +------- height -- an integer

Example #1 Coding


Coding Step #1 -- Class Shell


Using the provided design, we code a simple class shell, to initiate our coding.
class Rectangle {


}


Coding Step #2 -- Instance Variables


Using the provided object diagram, we code ivar declarations or a code comment about the ivars.

Some coding languages (e.g., Java) will require declaration of instance variables (ivars), while other languages (e.g., JavaScript) will not.
class Rectangle {
	/*
	width - the width dimension of a rectangle (an integer)
	height - the height dimension of a rectangle (an integer)
	*/
}


Coding Step #3 -- Constructors


Using the provided constructor headers and the coded ivars, we now code the constructors.

Constructor Starts
We start off coding the constructors by simply copying in the required constructor headers and adding empty method bodies and "//TODO" tags.
//Returns new Rectangle with width and height set to zero.

static newRectangle() {
	//TODO
}

//Returns new Rectangle that is a square with width and height both
//set to method param "aSide"
static fromSide(aSide) {
	//TODO
}

//Returns new Rectangle with width and height set to passed method params
static fromWidthHeight(aWidth, aHeight) {
	//TODO
}
Constructor Code
Here is the constructor code. Note that we take advantage of code reuse by simply calling another constructor when possible.

Note that we add a "classic constructor". We will typically do this, and call it from the static factory constructor, so that we can code on the instance side with "this" in context.

The classic constructor method header(s) vary per the language, e.g., in Java it would look like
public Rectangle(aWidth, aHeight)

//Returns new Rectangle with width and height set to zero.
static newRectangle() {
	return Rectangle.fromWidthHeight(0, 0);
}

//Returns new Rectangle that is a square with width and height both
//set to method param "aSide"
static fromSide(aSide) {
	return Rectangle.fromWidthHeight(aSide, aSide);
}

//Returns new Rectangle with width and height set to passed method params
static fromWidthHeight(aWidth, aHeight) {
	return new Rectangle(aWidth, aHeight);
}

//--------------------------------------------------------------
//Private (Classic) Constructors

//Classic constructor that accepts width and height params
constructor(aWidth, aHeight) {
	this.width = aWidth;
	this.height = aHeight;
}


Coding Step #4 -- Testing


We're ready to write test code. This is the most important step and the key to success.

Your test code should be in a separate directory/package from your model code (the code being tested).

We will simply test that the constructors run without crashing (without throwing an exception). These are minimal but fundamental tests called smoke tests.


Example smoke tests are shown for JavaScript and Java. Refer to "Constructor Smoke Tests". We would add one smoke test for every public constructor.

Be sure to change example test code as needed to test Rectangle.

Download Example Code



In downloaded code, see "README.txt"

Navigation