CS 231 Lab 4

Abstract Classes

The goal of this lab period is to get you started on the current assignment, give you an introduction to an Abstract class and go over how to remove an item from a Linked List.

Here are the links to the API's for the most recent versions of Java:


Tasks

  1. Update your Linked List class to have a remove method.

    public boolean remove(Object obj)

    It should remove the first occurrence of obj from the list, if obj is present, and then the method returns true. If this list does not contain the element, the list is unchanged and the method returns false. It uses the obj's equals method to determine if the obj is present. (And, if you want to do a really great job, then you will add this functionality: If obj is null, it removes the first occurrence of null in the list.)

    Put a test of your remove method in the main function of your LinkedList class.

  2. How would you write the remove method for your LLIterator? (The goal of an iterator's remove method is to remove from the LinkedList the last item returned by the next() method.) What information would need to be stored in the LLIterator class?
  3. Create an abstract class Animal.

    public abstract class Animal

    It should have a field for the animal's name and accessors to get and set the value. Give the class an abstract method Speak that returns a string representation of the sound the animal makes.

    public abstract String speak()

  4. Create several derived classes of the Animal parent class that each implement the speak method. In the derived classes, the speak method will not be abstract.
  5. Create a test class with a main function that creates an ArrayList<Animal>. Then add different types of animals to the list. Finally, go through the list and have each animal speak.

Once you are comfortable with the above tasks, go on to the project.