11:59.59pm, Sunday, February 17
In this lab you'll be creating your first implementation of a data structure from the Java Collections Framework. You'll also be learning how to use Generics and getting some practice writing test cases for you program.
If you've not done so, I suggest you do Lab 0 first.
We'll be constructing an implementation of the ArrayList data structure. We'll be using the prefix "My" in our class names so that you don't accidentally use the standard ArrayList in your testing. However, we will be matching its behavior, so when in doubt, you can refer back to the ArrayList documentation for clarification and/or use an ArrayList as a working reference solution.
MyArrayList implements the Java List interface. It contains more that 20 methods, however, we don't have to implement them all if we extend the existing class AbstractList.
We'll be using generics for our implementation, and taking advantage of the fact that List and AbstractList are similarly parameterized. Just declare your class like the following:
public class MyArrayList<AnyType> extends AbstractList<AnyType>
AbstractList gives you all methods except for get(int index) and size(), however, you'll need to override many other methods to get this working.
You should be using Javadoc style comments in your programs.
javadoc -d docs -link http://cs.oberlin.edu/~kuperman/csci151/jdk1.5/docs/api *.java
As you'd expect, the backing storage for an ArrayList is an array. However, you'll need to keep track of the number of items that are currently in the array you may not have something in every slot. Also, you need to keep the data in the array packed to the front so there are no gaps.
When the array is full, and a user tries to add something in, you should double the size of the array. You'll need to copy the previous items into the same slots in the new array.
You may get a compiler warning about allocating an array of a generic type. This can be solved by using casting and a special marker in the source code to suppress the warning. Allocate in a manner similar to:
AnyType[] data=(AnyType [])new Object[size];
and have a line
@SuppressWarnings("unchecked")
between your JavaDoc comments and the method header. This doesn't remove the warning in 1.5, but does in 1.6, and it will get rid of the error message in Eclipse.
You'll probably want a private method to handle the array resizing. I'd suggest printing yourself a message when it is called at first indicating what size it is and what size it becomes. Just be sure to remove it after doing some testing.
Also, take advantage of your existing methods and don't duplicate logic. You've got two constructors and two add() methods. Only one needs to do the real work, the second should just figure out a way to call the first.
You should include a meaningful message when you construct the IndexOutOfBoundsException.
Next you'll be creating a set of programs that exercise your MyArrayList class. Each should be a separate file named Test1.java, Test2.java, etc.
Create a class that tests out the exceptions you should be generating by using try/catch blocks. You should focus on the border cases. For example, try to add to position 0, then try position -1.
Keep your output short and tidy, maybe just passed/failed messages. You can just use the exception's toString method if you want to see the type of exception you caught.
Trying to add to position 0: passed. Trying to add to position -1: passed! java.lang.IndexOutOfBoundsException: Index: -1, Size: 1
java -Xmx500m Test7
Also, pay attention to the rate at which it prints out numbers. Do you notice any changes? Run it again. Does it behave the same? Why do you think this is happening?
Look through your programs and make sure you've included your name at the top of all of them. If you know that there is a problem in one of your programs, document that at the top. If you adhered to the honor code in this assignment, add the following statement as a comment at the top of your MyArrayList.java file:
I affirm that I have adhered to the Honor Code in this assignment.
Go through and delete any *.class files and backup files.
You now just need to electronically handin all your files.
% cd # changes to your home directory
% cd cs151 # goes to your cs151 folder
% handin # starts the handin program
# class is 151
# assignment is 1
# file/directory is lab1
% lshand # should show that you've handed in something
You can also specify the options to handin from the command line
% cd ~/cs151 # goes to your cs151 folder
% handin -c 151 -a 1 lab1
% lshand # should show that you've handed in something
This lab is intended to: