CS 161 Lab 3
This lab will explore the ArrayList data structure as well as the following aspects of OO 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. It means that items can be added and removed from the list. An Array on the other hand, has a fixed size: once we declared it to be a particular size, that size cannot be changed.
- Constructing objects
- Access modifiers (private, public) and accessing/modifying instance variables
- Method usage
- Client objects
- equals(Object other)
To set up an ArrayList, you first have to import the package from the java.util library:
You can then create a new ArrayList object:
ArrayList listTest = new ArrayList( );
The Java API has a list of all the methods provided by an ArrayList. See:
Java ArrayList API.
Please complete this lab individually. You are always welcome to bring any lab material and questions to office hours.
We will use Animal.java and GuinnessBook.java. Please download the following files:
Previously, you have used arrays to store and manipulate collections of primitives and objects. However, Java arrays can only represent collections of a fixed size.
This is a limitation as the programmer often does not know how many items will need to be stored in advance.
Many programming languages, including Java, provide programmers with array-like data structures that are resizable. In Java, this data structure is
ArrayList internally maintains an array of data that is resized to accomodate additional elements.
ArrayList also allows the programmer to add/remove items at arbitrary indices and even find the index of a particular item.
Complete the implementation of Animal.java, by adding the following methods: p>
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.
- Getters and setters for name and topSpeed. Add a check to make sure that the topSpeed doesn't go over 70 or below 0.
- A constructor that takes in a String for the name and an int for the topSpeed. Use the setters to set the instance variables.
- a toString() method that returns a string with that animal's information. EX: Name: elephant Top Speed: 25
- an equals(Object other) method that returns True if two animals have the same speed within 2mph, and False otherwise (recall that equals takes in an instance of Object rather than Animal, in order to override the default implementation of equals).
In GuinnessBook.java: p>
Complete the code for the constructor. You just need to add code that adds the animals to the landAnimals arrayList.
The toString() method. This can make use of Animal's toString()method. It should return a string representation all of the elements in the arrayList. EX:
Name: giraffe Top Speed: 32
Name: pronghorn Top Speed: 61
Name: reindeer Top Speed: 32
Test your code in the main method.
Follow the instructions in main and testGuinnessBook
This lab is worth a total of 2 points broken down as:
Demo/Attendance: 1 point
Effort: 1 point