Due by 10am, Monday, February 13
In this prelab, you will be working on some of the non-programming isses related to the first full lab assignment. Please write up or type up your solutions and hand in a paper copy before 10am on Monday. Remember: no late prelabs allowed!
If you've not done so, I suggest you do Lab 0 first.
In this lab you'll be creating your first implementation of a data structure from the Java Collections Framework. Specifically, we'll be constructing an ArrayList. You should be very familar with using this data structure and its methods by now -- two different add methods, get, set, remove, isEmpty, size, and so forth.
ArrayList is just an implementation of the List interface that uses an array to store items that are put into it. (We'll see a different storage structure later when we look at LinkedList.) The List interface has over 20 methods that you need to implement!!! Fortunately, many of these methods can be handled without worrying about how the data is actually being stored. For example, isEmpty() might just check to see if (0==size()). Much of this work has already been done in the AbstractList class.
To keep our version of the data structure separate from the existing ones in Java, we'll be prefixing each with "My". In this case, you'll be creating MyArrayList which is a child of the AbstractList class.
As I'm sure you recall, an array is fixed in size while ArrayList dynamically expands itself as necessary. Now that we know that our MyArrayList will be using a regular array for storage, this means that the number of spaces in the array (capacity) might be different than the number of items in the MyArrayList (size). The capacity must always be greater than or equal to the size -- otherwise we must be losing data. (Consider one of the flippy CD holders -- you've got a certain number of slots where you could put CDs and then there is the number of CDs you've actually put in it.)
for( i=0; i < 5; i++ ) {
temp[i] = 4;
}
has 5 assignment statements. The loop
for( i=0; i < s; i++ ) {
temp[i] = 4;
}
has s assignment statements. How many assignment statements does
your loop from question 4 have?)
Hopefully by now you'll have realized that when you add something to MyArrayList, you might need to resize things. Otherwise, you risk not being able to fit all of your lovely data into your structure.
| data.length | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | ... | n-1 | n |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| calls resize? (Y/N) | Y | ... | Y | ||||||||||||||||||
| # assignment statements |
total number of assignments performed (in terms of n):
| data.length | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | ... | n-1 | n=2p |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| calls resize? (Y/N) | Y | ... | Y | ||||||||||||||||||
| # assignment statements |
total number of assignments performed (in terms of n):
An important part of developing a reusable ADT is being able to determine if you've correctly implemented everything. A portion of this lab will be writing some programs that will test out your MyArrayList implementation and allow you to spot if there are any problems in it.
A good programming technique is to "write a little, test a little". By being able to come up short test that you can use right after you create a method, you will have greater confidence and insight as to the location of any bug that appears later.
If you adhered to the honor code in this assignment, add the following statement to the top of your prelab.
I have adhered to the Honor Code in this assignment.
Write or type up your solutions and hand in a paper copy by the deadline listed at top. Late prelabs will not be accepted.