CS161 Lab Week 4: Classes and ArrayList

Introduction

This lab will explore the ArrayList data structure as well as the following aspects of object-oriented programming in Java:

The ArrayList structure in java is a way to create an array that can grow or shrink in size during the execution of a program. As we have seen, a primitive array has a fixed size: once we declare it to be a particular size, that size cannot be changed. In our first two assignments, we have learned how to grow and shrink primitive arrays. The ArrayList class uses a primitive array to store objects and includes code much like you have written to grow and shrink the array.

To set up an ArrayList, you first have to import the package from the java.util library.

import java.util.ArrayList;

You can then create a new ArrayList for storing any object.

ArrayList things = new ArrayList();

As discussed in lecture, you can create an ArrayList for holding specific types of objects, like Pet objects.

ArrayList<Pet> things = new ArrayList<Pet>();

The Java API has a list of all the methods provided by an ArrayList. See: Java ArrayList API.

What You Must Do

Download the following files.

Complete the implementation of Animal.java, by adding the following methods.

When implementing constructors, getters/setters, toString or equals methods you can use Eclipse to help you write those methods. Under the "Source" tab of Eclipse there are options for generating stubs for those methods.

In GuinnessBook.java complete these steps.

Name: giraffe, Top Speed: 32
Name: pronghorn, Top Speed: 61
Name: reindeer, Top Speed: 32