CS 161 Lab 3
Overview
This lab will explore the ArrayList data structure as well as a few other helpful methods for classes:
- Constructing objects
- Accessing and modifying instance variables
- Method usage
- Client objects
- Resizable arrays
- toString()
- equals()
Demo
This lab focuses on experimenting with the ArrayList data structure and features a brief review of basic object oriented programming. We will use Animal.java and GuinnessBook.java. Please download the following files:
Lab Assignment
Please complete this lab individually. You are always welcome to bring any lab material and questions to office hours.
Assignment Overview
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 an unfortunate limitation as the programmer often does not know how many items will be added in advance.
Many programming languages, including Java, provide programmers with data structures that are resizable as well as random access and contiguous. In Java, this data structure is ArrayList
. 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.
In Animal.java, please add: p>
- Getters and setters for name and topSpeed. Add a check to make sure that the topSpeed doens'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 correctly.
- a toString() method that returns a string with that animals information. EX: Name: elephant Top Speed: 25
- a sameSpeed(Animal other) method that accepts another Animal and returns true if that Animal's top speed is within 2 mph of the animal calling the method and the Animals do not have the same name.
In GuinnessBook's main method, we are creating two GuinnessBook objects, one will have an arrayList that just imports the animals as the are in the file. The other will insert animals in their appropriate places, sorted in increasing order according to their topSpeed. Don't worry about sorting by their names right now. Also, you don't need to add any code to the main method.
In GuinnessBook.java, please add code for: p>
The constructors - although the code for reading in the file is already written. You just need to add code that adds the animals to the landAnimals arrayList, unsorted in the first constructor, sorted in the second.
The toString() method. This can make use of Animal's toString()method. Just print out all of the elements in the arrayList. EX:
Name: giraffe Top Speed: 32
Name: pronghorn Top Speed: 61
Name: reindeer Top Speed: 32
The numAnimalsInRange(Animal other) method. This will return an int with the number of animals that have a top speed within 2 mph of the animal passed to this method, not including that same animal.
This recitation is worth two points