You are going to build a small game using a linked list queue. You will have a juggler (Stephen) who is using two data structures to juggle (his hands and the air). His hands will be modeled with a simple data structure that holds a single juggling ball at a time, and the air will be modeled as a queue data structure which can hold a several items. The outline has been provided for these classes.

Setup

You have been provided with several classes already. Note that if you have completed Part 1, than understanding these classes and the interactions between them should be much easier.

  • Ball
    • Start here, it should be the easiest thing to finish and the methods that need your code should be self explanatory.
  • Juggler -> Hand
    • This inner class of Juggler models a juggler's hand. Hands can hold a single ball at a time. When the juggler catches a ball, the hand will keep track of that ball (see catchBall()) and when the juggler throws the ball the hand should no longer keep a reference to the ball (see throwBall()). See additional details below.
    • Note that you do not need to worry about how the Juggler uses this information, just concentrate on how the hand works.
  • Juggler
    • This class is completely written for you. You do not need to change any part of this except to add comments.
  • Air -> ListItem
    • This class should be the building block for a linked list. See details below.
  • Air
    • This class, as outlined, is a linked list queue. It needs to implement the Queue interface and information about what each of the methods in the class does can be found in the API documentation (http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html). See additional details below.
  • Assignment6
    • This is mostly written for you, there is a method to get a menu and some print statements, but you'll need to call appropriate methods for the juggler Stephen based on the user's input. See details below.

Additional Details

The Hand Class (see Juggler.java)

This is basically a single item list with three methods. Below are some notes on the methods which you may find useful:

public void catchBall(Ball ball)

  • this works like an add() method
  • should throw a RuntimeException (or your own if you'd like) if it can't catch the ball given

public Ball throwBall()

  • this works like a remove() method
  • should throw a RuntimeException (or your own if you'd like) if it can't throw a ball

public boolean hasBall()

  • this works like an isNotEmpty() method

The ListItem< T> Class (see Air.java)

This class should be the building block for a linked list. It needs to contain some private class variables to get it working. Use the getters and setters appropriately.

The Air< T> Class (see Air.java)

This class, as outlined, is a linked list queue. It needs to implement the Queue< T> interface and information about what each of the methods in the class does can be found in the API documentation (http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html). A tutorial on the queue interface can be found here: http://docs.oracle.com/javase/tutorial/collections/interfaces/queue.html. Several methods from this interface have already been written for you (and may not be changed). Eleven other methods (6 required by the Queue interface, and 5 required by the Collection interface) have been outlined for you and need to be implemented. Below are some notes on the methods which you may find useful:

Note: For your purposes, O(1) means that this can be done without a loop, and O(n) means you'll need to loop through all the elements in the list.

public boolean add(T item)

  • This can be done in O(1)
  • Remember to throw appropriate exceptions, see javadocs

public boolean offer(T item)

  • This can be done in O(1)
  • Hint: use add() to implement offer()

public T remove()

  • Hint: keep track of the end of the list to get O(1)
  • Remember to throw appropriate exceptions, see javadocs

public T poll()

  • Hint: keep track of the end of the list to get O(1)
  • Hint: use remove() to implement poll()

public T element()

  • This can be done in O(1)
  • Remember to throw appropriate exceptions, see javadocs

public T peek()

  • This can be done in O(1)
  • Hint: use element() to implement peek()

public String toString()

  • This can be done in O(n)
  • Use the T's toString() method

public void clear()

  • This can be done in O(1)

public boolean isEmpty()

  • This can be done in O(1)

public int size()

  • This can be done in O(1) if you don't iterate through the list every time, but rather keep track of the size while you add/remove items

public Object[] toArray()

  • This can be done in O(n)

The Assignment6 Class

This class contains the main code to interact with the juggler. You will need to prompt for input and perform the appropriate actions to produce both the same output as the samples below (as well as any other details covered in class). If you have any questions as to what should be output in specific situations: ASK!

public static void main(String[] arg)

  • This is the main code for your program, it should loop, prompting the user, and responding accordingly
  • Do not create an instance of Air, Ball, Hand, or ListItem here, you only need Juggler
  • Use a try/catch block to handle runtime exceptions

Testing

Unit tests have been provided for this portion of your code (Assignment6SampleTests.java). Additionally, below is some sample output for main:

> java Assignment6 Stephen, the juggler, is learning to do a shower trick...

He has 4 balls
( )
__|__/
|
(4)
|
(3)(2)
/
(1)
/
Stephen can:
1) Throw a ball into the air
2) Pass a ball between hands
3) Catch a ball from the air
4) Quit
Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.