Write a program that implements the List API (“List.java”, provided with documentation)
as a dynamic array as covered in lecture. A code template DynamicArray.java has been
provided for you – place your code in the appropriate methods. If a method indicates an
exception for some condition, be sure to check and throw this when the condition occurs.
The methods defined in List.java already have javadoc – use them (copy/paste is fine)
in your own implementation for the method documentation. However, documentation rules
still apply for methods you add and the code in the methods that you add.
The constructor should initialize the internal array length to the constant
MIN_CAPACITY. Your dynamic array must grow as needed (as items are added/inserted)
and it must also shrink as needed (items are removed). The grow and shrink must use the
approach shown in lecture. For shrink, you must ensure that the capacity never goes below
the constant MIN_CAPACITY.
The toString and main method have already been implemented for you, do not change.
The main method takes in the file “operations.txt” for unit testing your code. Additionally, a
“Stdio.java” file is provided and used in the main, do not modify. You can only add methods –
you cannot change the signature of the class, constructor, the constant, or existing methods.
Below are notes concerning each method (typically the parameters passed by the caller are
checked for “preconditions” that must be satisfied when calling the method).
You only need to work on DynamicArray.java. If you program is working correctly, you
will see the same output in the last page of this assignment when you run your code.
public DynamicArray() // constructor
– allocate initial memory using constant
public Type get(int i);
– check i for range
public void set(int i, Type item);
– check i for range, make sure item is not null
public void add(Type item);
– make sure item is not null
– may have to grow to increase capacity
public Type remove(int i);
– check i for range
– may have to shrink to reduce capacity
public void insert(int i, Type item);
– check i for range, make sure item is not null, make sure capacity, shift right
public int size();
– keep a variable and update in other methods, do not try to derive
public int capacity();
– note: not part of the List interface (because not all Lists use arrays)
– return the length of the internal array
• Use the template provided for you
• Use the generic type in method signatures and your code
• Have a style (indentation, good variable names, etc.)
• Comment your code well (no need to over do it, just do it well)
• Add additional private utility methods you need (not necessary but makes easier).
You may not:
• Make your program part of a package.
• Use code from anywhere except your own brain.
• Use code from the Java Collections library, including ArrayList.
• Cast items to avoid using the generic type (exception: array creation in the constructor)
• Name a folder with your gmu username
• Put your java files in the folder (but not your .class)
• Zip the folder (not just the files) and name the zip “username-pa2.zip”
• Submit to blackboard
• Non-submitted assignments
• Late assignments
• Non-compiling assignments
• Non-independent work
1pt Submission Format
1pt Style and Comments
2pts add method with grow
2pts remove method with shrink
2pts insert method
2pts set, get, and size methods
> java DynamicArray operations.txt
set 1 to graduate13
Final list=[student12, graduate13, student14]