15-200 Lecture 3 (Monday, January 23, 2006)

On Friday, we began working with a simple collection class, aptly named SimpleCollection. This class stores a list of Objects in an array, and supports basic add and contains methods. It also must contains a private grow method to increase the size of the array if the number of objects added exceeds its capacity.

public class SimpleCollection
{
  //instance variables
  private Object[] items;
  private int numberOfItems;

  //default constants
  private static final int DEFAULT_CAPACITY = 100;
  private static final double DEFAULT_GROWTH_FACTOR = 2.0;

  //Constructor creates a new Object array and sets numberOfItems to zero
  public SimpleCollection()
  {
    items = new Object[DEFAULT_CAPACITY];
    numberOfItems = 0;
  }

  public SimpleCollection(int initialCapacity) 
  {
    items = new Object[initialCapacity];
    numberOfItems = 0;
  }

  //Adds object to the end of the array. Must check if the array is full and increase its size if necessary.
  public void add (Object o)
  {
    if(numberOfItems = items.length())
      grow();

    items[numberOfItems] = o;
    numberOfItems++;
  }

  //Searches the array and checks for a given object
  public boolean contains(Object o)
  { 
    for(int i = 0; i < items.length(); i++)
    {
       if(o.equals(items[i]))
         return true;
    }
    return false;
  }

  //Increases the size of the array
  private Object[] grow()
  {
    //Declare temporary array with new size.
    Object[] newBiggerArray = new Object[(int)(items.length() * DEFAULT_GROWTH_FACTOR)];
    
    //Copy old array into the new, larger array.
    for(int i = 0; i < items.length(); i++)
    {
      newBiggerArray[i] = items[i];
    }
    
    //Change items to point to the new array.
    items = newBiggerArray;
  }
}

Note the use of static constants. Now, if you later decide to change the default capacity or growth factor, you don't have to hunt through your code looking for every time you use these values. By simply changing the constants, you can change the values everywhere they are used in your method.

In this example, the growth factor is stored as a double, instead of an int. This will allow you to use a value like 1.5, however, array lengths and indices must be integers. It doesn't make sense to talk about an array of length 7.5, or the value at array index 4.7. To fix this, you just have to cast the double as an integer, which will round down to the nearest integer value.


Handing Assignments In

We will be turning assignments in to the course space in the Andrew File System also known as afs.

First of all Macs do not play nicely with the afs system, so while you can find the space using Finder it does not let you write to the AFS space. Instead we have to use the Terminal program that is in the dock of the Mac. It is the icon that is a screen with a >_ on it.

The procedure for handing assignments in is first to enter the terminal program. Since most of you will have learned the afs commands already today's lecture was brief. Here is a more thorough explanation for the freshmen. Also it is very important if you are having trouble to get help and get it early. The last thing you want to do is get to the tests where we have to turn it in using the terminal and be unable to turn in your test. The best idea is to get so used to using the afs space and the commands that you can do it in your sleep.

First of all the basic commands you will need will be:

The basic order of how to hand stuff in is:

First to navigate to the folder:

cd /afs/andrew.cmu.edu/course/15/200/handin/lab2 (or the other lab number, but your first real lab will be lab2)
Second create a directory for you to turn your work into:
mkdir gkesden (or what ever your andrew id is)

Third, enter into the folder that you just created.

cd gkesden (andrew id)

If you type ls and enter then the folder should come up as empty.

Fourth put the assignments into the folder, if the assignment is on the desktop the coding is this:

cp ~/Desktop/SimpleCollection.java .

the ~ represents /afs/andrew.cmu.edu/user#/andrew_id
the . represents the directory you are currently in.

Fifth, If you make a change to an assignment after you already turned the assignment in, Then add a number at the end example gkesden1, gkesden2. The highest number folder is the one that will be graded.

Hotel Lab
Speaking of handing in labs, Lab 2, your first real lab was assigned today. The lab will make use of the built in ArrayList class to store data about Hotel Reservations. Check out the lab section (or just click here) to read the description and grab the starter code. Read the comments in the starter code for more detailed specs on each method. Also, don't forget to make use of the Java API for details about the ArrayList class. Finally, if you have any problems, drop by the cluster during office hours for help.