Question
Generics, Lists, and Queues. Thank you. Basic Procedures You must: Have a style (indentation, good variable names, etc.) Comment your code well in JavaDoc style
Generics, Lists, and Queues. Thank you.
Basic Procedures
You must:
Have a style (indentation, good variable names, etc.)
Comment your code well in JavaDoc style (no need to overdo it, just do it well)
Have code that compiles with the command: javac *.java in your user directory
Have code that runs with the command: java PA1
You may:
Add additional methods and variables, however these methods must be private.
You may not:
Make your program part of a package.
Add additional public methods or variables
Use any built in Java Collections Framework classes anywhere in your program (e.g. no ArrayList, LinkedList,
HashSet, etc.)
Use any arrays anywhere in your program (except in the Air class toArray() method)
Alter any method signatures defined in this document of the template code.
Change any code in Air.java or Juggler.java below the DO NOT CHANGE ANYTHING BELOW THIS LINE
line. You still need to add comments to the methods and instance variables in Juggler.java, but not Air.java.
Add any additional libraries/packages which require downloading from the internet.
Setup
Stephen the Juggler
Step 0: Overview
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. An outline has been provided for these classes in the zip file with this document.
Step 1: JavaDocs
You are required to complete the JavaDoc comments for the provided classes. You need to add comments to the methods and instance variables in Juggler.java below the DO NOT CHANGE ANYTHING line.
Step 2: Understanding the Code
The Ball Class (see Ball.java) Start here... this is the easiest class to complete. Required methods:
public Ball(int number)
creates a ball with a given number
public String toString()
should return a ball that looks like: (#)
e.g. (1) for ball with number 1 or (9) for a ball with number 9
public int getNumber()
returns this balls number The Juggler Class (see Juggler.java)
This class is done for you, but the inner class Hand is not, see below.
The Hand Class (see Juggler.java) This class will work like a single item list. There are three methods you need to complete:
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 because the
hand is already full
public Ball throwBall()
this works as a remove() method
should throw a RuntimeException (or your own if you'd like) if it can't throw a ball because there is no ball
to throw
public boolean hasBall()
this works similarly to an isNotEmpty() method
public String toString()
if the hand is holding a ball, this should return the string representation of the ball, otherwise return three spaces
The ListItem Class (see Air.java) This class will work as the building block for a linked list (i.e. a node). It needs to contain some private class variables to get it working. Use accessors and mutators appropriately.
The Air Class (see Air.java) This class, as outlined, is a linked list queue. It needs to implement the Queue interface:
http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html http://docs.oracle.com/javase/tuto rial/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) are still required. Below are some notes on the methods which you may find useful along with the REQUIRED Big-O runtime of each method (n = size of list):
public boolean add(T item)
O(1)
Remember to throw appropriate exceptions, see javadocs
public boolean offer(T item)
O(1)
Hint: use add() to implement offer()
public T remove()
O(1)
Hint: keep track of the end of the list to get O(1)
Remember to throw appropriate exceptions, see javadocs
public T poll()
O(n)
Hint: use remove() to implement poll()
public T element()
O(1)
Remember to throw appropriate exceptions, see javadocs
public T peek()
O(1)
Hint: use element() to implement peek()
public String toString()
O(n)
use the T's toString() method
public void clear()
O(1) public boolean isEmpty()
O(1)
public int size()
O(1)
Hint: don't iterate through the list every time, keep track while you add/remove items
public Object[] toArray()
O(n)
The PA1 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 the same output as the Example Run. Remember to handle invalid user input as shown in the Example Run.
public static void main(String[] arg)
this is the main code for your program, it should loop, prompting the user and responding appropriately
DO NOT create an instance of Air, Ball, Hand, or ListItem here, you only need a Juggler
use a try/catch block to handle runtime exceptions
public static int doMenu(Scanner in)
prints the prompt
alter this to get and return a choice from the user using the provided Scanner
CODE NEEDED IS BELOW
AIR
/** * @author Your Name Here * * */
import java.util.*; //for Queue interface
/** * YOUR COMMENTS HERE * * For the Queue interface, see http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html * and http://docs.oracle.com/javase/tuto rial/collections/interfaces/queue.html */ class Air implements Queue { /** * Wikipedia claims max solo record is 13 * see http://en.wikipedia.org/wiki/Juggling_world_records#Solo_records * Allowing room for growth... */ public static final int MAX_CAPACITY = 15;
/** * */ private class ListItem { //your code here } //you code here - variables, required methods, etc. /*-------------- DO NOT CHANGE ANYTHING BELOW THIS LINE --------------*/ /** * DO NOT CHANGE THIS, no comment needed */ public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public boolean contains(Object o) { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public boolean containsAll(Collection c) { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public boolean equals(Object o) { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public int hashCode() { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public Iterator iterator() { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public boolean remove(Object o) { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } /** * DO NOT CHANGE THIS, no comment needed */ public E[] toArray(E[] a) { throw new UnsupportedOperationException(); } }
BALL
/** * @author Your Name Here * * */
/** * */ class Ball { //you code here - variables, required methods, etc. }
JUGGLER
/** * @author Your Name Here * * */ import java.util.*;
/** * */ class Juggler { /** * */ private class Hand { //you code here - variables, required methods, etc. } /*-------------- DO NOT CHANGE ANYTHING BELOW THIS LINE --------------*/ /** * DO NOT CHANGE THIS, but replace this comment */ private Air air = new Air<>(); /** * DO NOT CHANGE THIS, but replace this comment */ private Hand[] hands = new Hand[2]; /** * DO NOT CHANGE THIS, but replace this comment */ private int numUnthrownBalls; /** * DO NOT CHANGE THIS, but replace this comment */ private int totalBalls; /** * DO NOT CHANGE THIS, but replace this comment */ public Juggler() { hands[0] = new Hand(); hands[1] = new Hand(); this.totalBalls = this.numUnthrownBalls = 5; //((new Random()).nextInt(5))+3; } /** * DO NOT CHANGE THIS, but replace this comment */ public void passBall() { hands[0].catchBall(hands[1].throwBall()); } /** * DO NOT CHANGE THIS, but replace this comment */ public void throwBall() { if(!hands[0].hasBall() && this.numUnthrownBalls > 0) air.add(new Ball(this.numUnthrownBalls--)); else air.add(hands[0].throwBall()); } /** * DO NOT CHANGE THIS, but replace this comment */ public void catchBall() { hands[1].catchBall(air.remove()); } /** * DO NOT CHANGE THIS, but replace this comment * @return */ public int getNumUnthrownBalls() { return this.numUnthrownBalls; } /** * DO NOT CHANGE THIS, but replace this comment * @return */ public String toString() { String spacing = ""; if(this.totalBalls < 7) spacing += " "; if(this.totalBalls < 5) spacing += " "; return spacing + air.toString() + " " + hands[0].toString() + "( )" + hands[1].toString() + " " + " \\__|__/ " + ((this.numUnthrownBalls > 6) ? "(7)" : " ") + ((this.numUnthrownBalls > 5) ? "(6)" : " ") + " | " + ((this.numUnthrownBalls > 4) ? "(5)" : " ") + ((this.numUnthrownBalls > 3) ? "(4)" : " ") + " | " + ((this.numUnthrownBalls > 2) ? "(3)" : " ") + ((this.numUnthrownBalls > 1) ? "(2)" : " ") + "/ \\ " + ((this.numUnthrownBalls > 0) ? "(1)" : " ") + " / \\ "; } }
PA1
/** * @author Your Name Here * * */ import java.util.*;
/** * */ class PA1 { /** * @param */ public static void main(String[] args) { Juggler stephen = new Juggler(); System.out.println(" Stephen, the juggler, is learning to do a shower trick..."); System.out.println("He has " + stephen.getNumUnthrownBalls() + " balls"); //your code here //the following messages may be useful as well: //System.out.println("Stephen dropped everything."); //System.out.println("Stephen wants to try again... He has " + stephen.getNumUnthrownBalls() + " balls"); } /** * @param * @return */ public static int doMenu(Scanner in) { System.out.println(" Stephen can:"); System.out.println("1) Throw a ball into the air"); System.out.println("2) Pass a ball between hands"); System.out.println("3) Catch a ball from the air"); System.out.println("4) Quit"); System.out.print(" What should he do? "); //your code here return 0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started