Quick Index
Example #2


This example is a continuation of Object Structure Example #2.

Required Methods


This example requires that our DynamicArray must implement (satisfy) the DynamicList ADT.

This means that we need to code all of the listed methods on the ADT.

One technique is to "stub" all the required instance methods into the class with "TODO" tabs. Here is an example in Java...

NOTE WELL -- if there are methods known to be "core methods", start coding these first. They will get the object up and running and ready for testing.

For starters, let's choose the methods "size", "get", "add", and "addLast". They are certainly core methods and will allow us to get tests up and running quickly.

Coding 'size'


ADT Spec
Here is the ADT spec for the method "size".
size();
	/*
	Return number of elements in this list.
	*/
Code
Utilizing our ivar listing in our coded object structure we note that "_size" is an ivar. Thus all we need to do is return it.
size() {
	return this._size;
}


Coding 'get'


ADT Spec
Here is the ADT spec for the method "get".
get(index);
	/*
	Return element at given index.
	If passed arg "index" is invalid, throws exception:
		"(get) Index %d is out of bounds"
	*/
Code
Here is the code for "get".
get(index) {
	if (index < 0 || index >= this.size())
		throw '(get) Attempt to access element in empty list';
	return this.fixedArray[index];
}


Coding 'add'


ADT Spec
Here is the ADT spec for the method "add". We see that this one is trivial, we simply call "addLast".
add(newElem);
	/*
	Alias for "addLast" (same functionality).
	*/
Code
Doesn't get any easier than this. Just pass the logic to our friend "addLast".
add(newElem) {
	this.addLast(newElem);
}


Coding 'addLast'



Required Instance Method Header
Assume the problem requirements include this required method header.

We know the instance variables from the object structure coding.
addLast(newElem);
	/*
	Add (append) the passed arg "newElem" to end of list
	All existing elements are preserved and will precede (be before) "newElem".
	No return value
	Example:
		given "this" dynamic list is [10, 20, 30, 40]
		and "(newElem)" is (1002)
	Then, after "addLast" is finished, the result:
		"this" dynamic list is [10, 20, 30, 40, 1002]
	*/
Coding 'addLast'
Here is the code for "addLast". We are utilizing several helper methods (which we'll also need to code).
addLast(newElem) {
	//Add the passed arg "newElem" to end of list
	if (!this.hasCapacity())
	    this.grow();
	this.fixedArray[this.size()] = newElem;
	this._size++;
}
Coding 'hasCapacity'
Here is the code for "addLast". We are utilizing several helper methods (which we'll also need to code).
hasCapacity() {
	// Return true if our capacity is greater than our size
	// i.e. if we have excess capacity
	return this.getCapacity() > this.size();
}

getCapacity() {
	return this.fixedArray.length;
}
Coding 'grow'
Here is the code for "addLast". We are utilizing several helper methods (which we'll also need to code).
grow() {
	// Grow (increase) our capacity by factoring our
	//current capacity by the input param "growthFactor"
	// In this method var "fa" will be the new fixed array
	this.growCapacityTo((int)Math.round(getCapacity() * this.growthFactor));
}

growCapacityTo(newCapacity) {
	// Grow (increase) our capacity to "newCapacity"
	// Construct new fixed array
	// Copy existing elements into new fixed array
	let oldFixedArray = this.fixedArray;
	let newFixedArray = this.newFixedArray(newCapacity);
	//Copy
	for (let i = 0; i < this.size(); i++)
		newFixedArray[i] = oldFixedArray[i];
	this.fixedArray = newFixedArray;
}


Coding Step #4 -- Testing


The most important step is testing. Without it we have no idea if our code works.

The four core methods we have coded allow us to do a rich mix of tests. Here are some ideas to help get started:


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

Download Example Code



In downloaded code, see "README.txt"

Navigation