Quick Index
Index
Example #2 (Provided Information)


Problem Overview


Problem Description
This challenge is to code a dynamic array that implements the DynamicList ADT.

The dynamic array should perform indexed access as efficiently as a fixed array, but also add "dynamic capabilities" like adding, inserting and removing elements.


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 DynamicArray
Required Constructor Header
A constructor must be coded with the specified constructor header.

The constructor is a static factory method

The constructor must be public.
//Returns a new empty DynamicArray
//with defaults initCapacity=10 and growthFactor=2
static newEmpty()

//Returns a new empty DynamicArray using passed parameters
static fromCapacityGrowthFactor(anInitialCapacity, aGrowthFactor)


Provided Design


Object Diagram
The object design provides us with an object diagram.

Note the underscore in "_size" (to avoid name collision with method "size")

Note that the "fixedArray" ivar will contain elements plus "blank slots" (nulls).
DynamicArray
  |
  +------- fixedArray ------- a fixed array
  |
  +------- growthFactor -- a floating point number
  |
  +------- _size -- an integer
  |
  +------- initialCapacity -- an integer

Example #2 Coding


Coding Step #1 -- Class Shell


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

}


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 DynamicArray {
	/*
	fixedArray - a fixed array
	growthFactor - a floating point number
	size - an integer
	initialCapacity - 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 constructor(s) by simply copying in the required constructor headers and adding empty method bodies and "//TODO" tags.
//Returns a new empty DynamicArray
static newEmpty() {
	//TODO
}

static fromCapacityGrowthFactor(anInitCapacity, aGrowthFactor) {
	//TODO
}


Constructor Code
Below is the constructor code. We have also added a "classic constructor" and helper methods to make our code simple.

The classic constructor method headers vary per the language. See "Java Note - Classic Constructor" below.
//--------------------------------------------------------------
//Public Constructors

//Returns a new empty DynamicArray
//with defaults initCapacity=10 and growthFactor=2
static newEmpty() {
	//Use defaults
	return new DynamicArray(10, 2);
}

static fromCapacityGrowthFactor(anInitialCapacity, aGrowthFactor) {
	return new DynamicArray(anInitialCapacity, aGrowthFactor);
}

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

constructor(anInitialCapacity, aGrowthFactor) {
	this.initialCapacity = anInitialCapacity;
	this.growthFactor = aGrowthFactor;
	this.reset();
}

//--------------------------------------------------------------
//Helpers
reset() {
	this._size = 0;
	this.fixedArray = this.newFixedArray(this.initialCapacity);
}

newFixedArray(aSize) {
	//Varies per coding language (see next section for examples)
	//Construct fixed array with null elements, and with size "aSize"
	//Null elements are "blank" spots where "actual" elements will be placed
	//TODO
}



Java Note - Private (Classic) Constructors
Here is the classic constructor header in Java.
private DynamicArray(int initCapacity, double aGrowthFactor)
JavaScript Note - newFixedArray
This is how we could construct a fixed array in JavaScript.
newFixedArray(aSize) {
	//Construct fixed array with null elements, and with size "aSize"
	//Null elements are "blank" (available) spots where "real" elements will be placed
	let fixedArray = Array(aSize);
	// Fill with nulls
	fixedArray.fill(null);
	// Assure it is fixed
	Object.seal(fixedArray);
	return fixedArray;
}
Java Note - newFixedArray
This is how we could construct a fixed array in Java.
@SuppressWarnings("unchecked")
private E[] newFixedArray(int aSize) {
	return (E[]) new Object[aSize];
}


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 (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.

Navigation