CS 368 Program 1: List Class
What to Write
First, complete the List class partially defined below by:
- Writing the body of the AddToEnd function, which should add the given
Object to the end of the list.
Note that the "items" array might be full.
In that case, you should allocate a new array that is twice the size
of the current one and copy the old Objects into the new array before
adding the new Object.
- Writing the body of the Print function, which should print (to the
standard output) a left square bracket followed by each Object in
the list, separated by spaces, followed by a right square bracket.
(If the list is empty, just print "[]".)
- Writing a main function to test your List class (make the main
function part of the List class).
Be sure to test "boundary" cases (e.g., calling the List methods
when the List is empty) as well as other cases.
Next, add additional fields and methods to allow a client of the List
class to iterate through the List, accessing each item in the List, as follows:
- Add a "currentObject" field to the List class that is (conceptually)
a pointer to
the current object. (In fact, you should implement this field using
an integer whose value is the array index of the current object.)
- Add method firstElement, which makes the first object on the list
be the current object (i.e., set the currentObject field to zero).
- Add method nextElement, which returns the current object, and also
updates the currentObject field to "point to" the next object in
the list. Note that
it doesn't make sense to call this method when the current-object
"pointer" has "fallen off" the end of the list or when there are no
elements in the list. For now, you can just
assume that the method will not be called in those cases; you will learn
how to handle those cases using an exception later.
- Add method hasMoreElements, which returns true iff the list is not
empty and the current-object
"pointer" hasn't "fallen off" the end of the list (i.e., if there are
still more items in the list that haven't been accessed yet). Note
that this method should return true when the current-object "pointer"
is pointing to the last object in the list -- it should only return
false when the list is empty or the current-object pointer has
"fallen off" the end of the list.
- Update your main function to test the three new methods.
Note that neither adding a new item to the end of a list
nor printing a list should change the value of the current-object pointer.
To help you understand these methods better, look at the following main
function, which first creates a list of strings, then prints each string:
public static void main(String[] args) {
// create a list of strings
List L = new List();
for (int k = 0; k < args.length; k++) {
L.AddToEnd(args[k]);
}
// now iterate through the list, printing each item
L.firstElement();
while (L.hasMoreElements()) {
String tmp = (String)L.nextElement();
System.out.println(tmp);
}
}
What to Hand In
Put all of your code in a single file named List.java, and
hand in your code through student files and do not copy any files other than List.java (for example,
do not copy List.class).
Grading Criteria
Your grade will depend on:
- how well you follow the "What to Hand In" instructions
- the correctness of your code
- your coding style (i.e., is it easy to read and understand)
- how well your main function tests your code
The List Class
/*
* List class
*
* A List is an ordered collection of any kind of Object.
*
* Operations:
* AddToEnd Add a given object to the end of the list.
* Print Print (to standard output) the objects in the list in order,
* enclosed in square brackets, separated by spaces.
*/
class List {
private static final int INIT_LEN = 10;
private Object [] items; // the actual items
private int numItems; // the number of items currently in the list
/*
* constructor: initialize the list to be empty
*/
public List()
{
items = new Object [INIT_LEN];
numItems = 0;
}
/*
* AddToEnd
*
* Given: Object ob
* Do: Add ob to the end of the list.
*/
public void AddToEnd(Object ob)
{
}
/*
* Print
*
* Print (to standard output) the objects in the list in order, enclosed in
* square brackets, separated by spaces.
*/
public void Print()
{
}
}