CS 145: Lab 10 – Objects and ArrayList

Welcome to lab 10!

If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.

In this lab you will create our own custom data types. We will model objects’ state and behaviors using classes in Java.

Checkpoint 1

Person A types.

Solve the following problems on Practice-It!:

Checkpoint 2

Person B types. In Eclipse, create a lab10 package.

Write a class RoundRobinList that can be used to repeatedly and fairly cycle through a list of people or things. For example, if you create a list and add to it "apple", "banana", and "carbonite", you’ll generate the following sequence when you call get 8 times:


Include the following:

  • A default constructor that initializes the list to be empty. Use an ordinary ArrayList of Strings to hold the cycleable elements.
  • A size method that returns the number of elements in the list.
  • An add method that accepts a String to add to the list. When added, the name should be inserted such that it will be the next name chosen. For example:
    System.out.println(list.get()); // prints Turin
    Hint: keep the list organized from oldest to newest. When adding a String, place it at the “new” end.
  • A get method that returns as a String the next (or newest) element in the cycle. But that’s not all. Also make sure to set things up such that the second newest element will be drawn on the next call to get, and that the element you return won’t be retrieved again until the next cycle. In other words, maintain your organization from oldest to newest. The newest item becomes the oldest.

In class RoundRobinTest, write a main method that constructs an instance of RoundRobinList, adds some elements, and make several complete cycles through it. Print each element as you retrieve it.


Leave a Reply

Your email address will not be published. Required fields are marked *