3-13 The Dynamic Queue

At this point you should be comfortable with the proper design and implementation of abstract data types, including implementing an interface.  Time to prove it!

You Try!

  1. Using what you’ve learned from the DynamicStack we implemented in 3-12, implement a DynamicQueue and DynamicQueueTest class.  Don’t forget to implement the QueueADT interface we defined in 3-10, and be sure that the LinkedList class you use includes your addLast() method from 3-11 Q1.
  2. You may have played the game “Hot Potato” when you were younger. The game works like this: a ball (i.e., “hot potato”) is passed in a circle from player to player while music plays.  When the music stops the player holding the hot potato is out.  The game repeats until there is only one person left – the winner!  Use your DynamicQueue class to simulate a game of Hot Potato.  Start by prompting the user to enter the player names (or <Enter> by itself to stop entering names).  Each name should be enqueued in the order they are entered.

    Next, pick a random integer, n, between 1 and the number of players in the queue (inclusive).  Using a loop, dequeue the front player in the queue and then enqueue them at the end.  Repeat this dequeuing/enqueuing process n times.  Once this is finished, dequeue the player currently at the front of the queue (do not enqueue them again) – they’re out!  Pick a new random integer, between 1 and the number of players left in the queue (inclusive), and repeat the simulation loop. When there is only one player left in the queue, stop the game, and declare them as the winner. Try to include some meaningful output during the simulation so that we can see that everything is working correctly.