CS 148: Lab 11 – ArrayList and Object
Welcome to lab 11!
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. In Eclipse, create a lab11 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:
carbonite banana apple carbonite banana apple carbonite banana
Include the following:
- A default constructor that initializes the list to be empty. Use an ordinary
ArrayListofStrings to hold the cycleable elements. - A
sizemethod that returns the number of elements in the list. - An
addmethod that accepts aStringto add to the list. When added, the name should be inserted such that it will be the next name chosen. For example:Hint: keep the list organized from oldest to newest. When adding alist.add("Florence"); list.add("Bologna"); list.add("Turin"); System.out.println(list.get()); // prints Turin
String, place it at the “new” end. - A
getmethod that returns as aStringthe 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 toget, 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.
Checkpoint 2
Person B types. Create a bare-bones text-based adventure game, in which a player moves through a network of rooms, achieving some objective—like collecting items or solving puzzles.
Create a class named Room with the following:
- A constructor that accepts as a parameter a description of the room. It sets up the room to initially have no connections to other rooms.
- Setter methods
setNorth,setEast,setSouth, andsetWest. Each accepts anotherRoomas its sole parameter. - Getter methods
getNorth,getEast,getSouth, andgetWest. Each returns theRoomin the given direction. If there is no room in the given direction, returnnull. - Any other methods you might need.
Create a class named TextAdventure with a main method that creates a network of at least four rooms. Choose one of the rooms to be the player’s starting room. Then continuously prompt the user for a text command and respond accordingly. Respond to commands n, e, s, and w by attempting to move the player to the given room. When a room is entered, display its description.
For example, consider this interaction with just two rooms:
You look around. The room is completely black. Suddenly, a flame lights up. It ignites the end of cigarette, which is perched in the mouth of a clown. He is not smiling. > n Three clowns sit around a table playing cards. They nod as you enter and return to their game. > e There is no room to the east. > s You look around. The room is completely black. Suddenly, a flame lights up. It ignites the end of cigarette, which is perched in the mouth of a clown. He is not smiling.
Support at least one other meaningful command of your choosing that allows the player to interact with a room. For example, maybe a room contains items that can be taken and added to the player’s inventory. Maybe the doors are locked and need to be unlocked in some way. You decide. Add methods to Room as needed.
Consider adding a win condition appropriate for your interaction.