This example is a continuation of Object Structure Example #1.
Required Instance Method Header
Assume the problem requirements give "getPerimeter" as a required method header.
//Compute and return the rectangle perimeter//perimeter = 2 * (width + height)getPerimeter()
Coding Instance Method
Using the instance variable information from our object structure work, and using the calculation given in the requirement method description, we can code the method, as shown.
//Compute and return the rectangle perimeter//perimeter = 2 * (width + height)getPerimeter() {
return 2 * (this.width + this.height);
}
We want to write test code in parallel with our model code. This is the most important step and the key to success.
Test code should be in a separate directory/package from your model code (the code being tested).
We write functional tests, i.e., tests that assure that the objects function correctly. Generally these tests compare "actual results" vs "expected results".
Be sure to change example test code as needed to test Rectangle.