Page 726 - AP Computer Science A, 7th edition
P. 726
3. (a) public Player requestSlot(String playerName) {
for (int i = 0; i < slots.length; i++) {
if (slots[i] == null) {
Player p = new Player(playerName, i); slots[i] = p;
return p;
} }
waitingList.add(playerName);
return null; }
(b) public Player cancelAndReassignSlot(Player p) {
int i = p.getPlayerNumber(); if (waitingList.size() != 0) {
slots[i] = new Player(waitingList.get(0), i);
waitingList.remove(0);
} else {
slots[i] = null; }
return slots[i]; }
NOTE
• In part (a), the last two lines of the method will be executed only if you are still in the method, namely no available slot was found.
• In part (b), the final line will return either a new player, or null if the waiting list was empty.