Index
Overview


First try coding the class without these suggestions.

Then if and when you get stuck, peruse through the suggestions.

Suggestions


Class must use generics, and specifically use a generic type parameter specified on the class (suggested parameter name of "E")
Class must implement the "DynamicList" interface
See the "Design" sections/pages of the previous chapter on dynamic array logic. You should minimize the number of ivars you use (only use what is needed), becuse each ivar adds code complexity.

Suggested ivar count: four.

Notes:

  • Capacity does NOT need to be an ivar (it can be derived)
  • Determine a default growth factor from the notes
  • Determine a default initial capacity from the notes
Implement the interface and all the specified methods
A couple easy ones:
  • size -- simply return ivar
  • isEmpty -- use size to determine if empty
This pattern works for throwing runtime exceptions:

if (isEmpty())
	throw new RuntimeException("(first) Attempted to access element in empty list");
You may want to code a helper method, e.g., to check validity of indexes (passed into methods) e.g., a possible name might be "checkIndex" which would return true if the index is valid relative to the lists valid index range
Use helper methods liberally (the name helper method just means any method you code to help you solve the problem, so, e.g., say you are coding "get" and want to check index validity, you may code "checkIndex" as a helper method. You might then call "checkIndex" from other methods, as needed (this is code reuse).
For a class as challenging as the domain model DynamicArray it is suggested to write test code as you code the model (in parallel). Put your test code in another package (not the same package as the domain model class). This will help flush out more bugs.
As far as test data, it is okay for some of the tests to use primitive-like objects (integers, strings, ...) but you want to also write tests that use richer objects.

These "richer" object (types) are coder's choice (e.g. possible types are Employee, Person, Animal, Restaurant, etc)

The object type (i.e., the "class") should include equality methods.

Here is an example of a simple class with common methods such as equality methods, accessors (getters and setters), etc.
The challenging part of the dynamic array is growing the capacity as needed. See logic chapter. Test code will be your friend for this implementation.
Initially stub in required methods (so the code compiles), e.g.

public E get(int index) {
	//TODO
	return null;
}


Then, finish up //TODO tags one-at-a-time (you may or may not get to all of them). But always make sure code compiles cleanly when delivering code.
Then start to build up the code, i.e., knock off one "//TODO tag" at a time (and test as you go). Start with methods that can possibly be tested right away (e.g., the simpler methods). E.g., "size", "isEmpty" can immediataly be tested vs an empty list (expecting 0 and true). Then test them for a list of size 1 (expecting 1 and false). Follow that same pattern. E.g., "get" would be a good one to hit next.
"equals" and "hashCode" (handy for testing)
If implementing hashCode, a good algorithm is described in Java's List interface (under "hashCode")
You may (optionally) add a superclass for DynamicArray (coder's choice). If so, you should consider also using the superclass for LinkedList.
The method "iterator" is defined as optional in the DynamicList interface. Iterators are handy and fun to code, and can simplify the overall code through use/reuse (of the iterator). See chapter 307 on iterators
This is a snippet for initializing the fixed array ivar.

Do other initialization as desired.

Note: this is a rare case where we need to suppress a warning in Java using SuppressWarnings. We can suppress it safely because the ivar is private and all public methods will use the generic param "E" (not "Object").
@SuppressWarnings("unchecked")
private void yourMethod() {
	//...
	//TODO -- replace XXXX with the desired capacity
		@SuppressWarnings("unchecked")
		E[] fixed = (E[]) new Object[XXXX];
	//...
}