Prelab 1

Identity Crisis
Due by 10am, Monday 10 Sep 2012

In this prelab, you will tackle some of the non-programming issues related to the first full lab assignment. Please write or type up your solutions, and hand in a paper copy before 10am on Monday. Late prelabs are not accepted.

Part 1 - MyArrayList

If you've not yet done so, please complete lab 0 before proceeding.

In this week's lab, you will construct your very own implementation of the ArrayList data structure, which we will oh-so-inventively name MyArrayList. You should be very familiar with using this data structure and its methods by now -- two different add methods, get, set, remove, isEmpty, size, and so forth.

MyArrayList implements the Java List interface. That is, you have to provide implementation for all of the (abstract) methods contained therein. There are more than 20 such methods, however, and that could take you awhile to get through. Moreover, some of the methods are "redundant" in the sense that they can be implemented using the other methods (for example, you can implement isEmpty() by returning (size()==0)). Fortunately, the folks at Java have provided a lovely abstract class AbstractList that provides some very basic and default behaviour for a List. Some of the methods still aren't implemented (that is, they are abstract) and some of them may have inefficient implementation (that is, you'll want to override them), but it's useful nonetheless. Thus your MyArrayList class should extend AbstractList in order to reap the benefits; because AbstractList implements the List interface, you will implicitly be required to do so as well (but do not have to declare your intention to implement explicitly).

  1. You'll be using generics for your implementation, and taking advantage of the fact that List and AbstractList are similarly parameterized. Given the information above, give the declaration (i.e. "public class...") for MyArrayList. Remember to properly indicate the parent and that we'll be using Generics to handle the storage type. (Refer to Weiss 4.7 for information on generics.)

  2. Skimming through the documentation for AbstractList, what methods must you implement to create a concrete child class and what methods do you need to override to be able to do add/removes succesfully?

Backing Storage

As you might expect, the underlying storage for an ArrayList is an array. That is, an ArrayList is really just an array with the extra functionality of dynamic resizing. Thus, the number of spaces in the array (its capacity) might be different than the number of items in the MyArrayList (its size). The capacity must always be greater than or equal to the size -- otherwise we're not storing everything.

  1. Which of the methods that you identified in question 2 might require that you resize the array in order to be sure all the data can fit?

  2. Write a private resize method that increases the length of the data array by one. You will need to make a new array of larger size, copy the data from the old array into the new array, and change the reference to point to your new array. Let's assume that our storage array is declared AnyType data[].

  3. Suppose that data.length is currently s. How many assignment statements does your resize method need to perform, in terms of s? (You may ignore loop counter assignment statements, such as i++.) If you're having a hard time coming up with the formula, try counting the number of assignments when s=2, s=3, etc., and then try to find a pattern.
    (For example, the loop
        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 have?)

Resizing Options

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.


  1. Write the implementation for the add(int index, AnyType element) method. Don't forget to throw the specified exception and call resize if necessary. (See ArrayList's add method for details.)

  2. Now do the same for the set(int index, AnyType element) method. Pay close attention to what it is supposed to return and throw.

  3. Suppose you start with an arraylist of size 1 (and capacity, i.e., data.length=1), and you add n elements one-by-one to the arraylist. How many calls to resize will you make, in terms of n? What is data.length for these successive calls? Therefore, in total, how many assignment statements are performed for all these resizes? I will accept any answer in an acceptable range, but be as accurate as you can. The following may (or, may not) help organize your thoughts. Your final answer would be the sum of the entries in the bottom row. It may be useful to remember that 1+2+...+n = n(n+1)/2

  4.  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):


  5. Now suppose you change your resize method to double the length of the data array, instead of only incrementing by one. Answer the previous question again and comment on the result. If it helps, you may assume that n is a power of 2. It may be useful to remember that 1+2+...+2p= 2p+1-1 = 2*2p-1.

  6.  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, so get rid of all p's):




Part 2 - Testing

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 test programs to test your MyArrayList implementation, to see if you can spot any problems with it.

A good programming technique is to "write a little, test a little." By being able to come up with short tests 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.

  1. Come up with three short tests that will test an aspect of one of the methods you are planning on implementing. Describe (in pseudocode) what actions you will do in the test and what the expected outcome should be. For example, I may want to test the constructor. I would call the constructor and then print out the value given by size() as well as looking at the array's length to see if it was what I expected. Put some thought into where your programming mistakes are likely to be, and write tests that will suss those out. Frequent sources of errors come from adding/removing things, repeated operations, and dealing with edge cases (is that supposed to be less-than or less-than-or-equal?)
  2.  Test #1  




     Test #2  




     Test #3  





handin

If you adhered to the honor code in this assignment, add the following statement at the top of your prelab.

I have adhered to the Honor Code in this assignment.

Write or type your solutions and hand in a paper copy by the deadline listed at the top of this prelab. Late prelabs will not be accepted.


Last Modified: July 11, 2012 - Alexa SharpVI Powered