Microsoft Word XXXXXXXXXXCopy.docx Programming Assignment #02 – King of the Stacks Due: Tuesday, October 20th at 5:00 PM You must upload a single ZIP file containing all of your JAVA source code files...

Assignment details on pdf. Use zip file as reference/base of program.


Microsoft Word - 02 - Copy.docx Programming Assignment #02 – King of the Stacks Due: Tuesday, October 20th at 5:00 PM You must upload a single ZIP file containing all of your JAVA source code files on the course Moodle site by the start of class on the due date. Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each disk will include some marker to denote to whom it belongs.  At the end of certain turns, spaced at regular intervals, the top disk is automatically popped from the Stacks. The pop timer is staggered so that disks are popped off from different Stacks at different times.  The game is played for a set number of turns (N), but there must be at least 20 turns.  After N turns have elapsed, the game is over. The player who has the most disks remaining on the three Stacks combined is the winner. Your task is to write a program that will implement and simulate the King of the Stacks game. Some requirements for your simulation:  Your program should ask how many turns should be played (N >= 20)  The game will be simulated according to the rules listed above  Your program should simulate the entire game without any user intervention after the number of turns is entered. Each player's turn will be simulated by having a random integer (0, 1, 2) generated. Based on the random integer drawn, the player will push her disk onto the appropriate Stack.  The pop timers should be staggered so that a disk is popped from each Stack at the end of every 3rd, 5th, and 7th turns, respectively. (In other words, Stack A will pop off a disk every 3 turns, Stack B will pop off a disk every 5 turns; and Stack C will pop off a disk every 7 turns).  Your simulation should include a way to handle a potential EmptyStackException that is thrown by attempting to pop an empty Stack. 2  Your program should include print statements stating which turn it is and describing the events of each turn (Player _ push disk onto Stack _; A disk was popped from Stack __; etc.)  At the end of the game, all of the disks should be popped off from each Stack and scores tallied for each player. The results should be announced via a print statement. Some ground rules:  Your simulation should work for any of the implementations of the Stack ADT. If one were to change the driver to use a different Stack implementation class, your simulation should still work.  Your program should be generalized where possible so that the simulation can easily be modified (e.g., number of turns played, pop timers for each stack, etc.)  Object-oriented principles must be followed; define classes to represent game objects as needed.  Functional decomposition must be followed and helper methods be used where appropriate.  Your program should be well organized and properly commented.  You must test your program to confirm that it works. Upload your source code files (.java files) to the course Moodle. KingStacksLab/ArrayStack.class public synchronized class ArrayStack implements StackInterface { private Object[] stack; private int topIndex; private boolean integrityOK; private static final int DEFAULT_CAPACITY = 50; private static final int MAX_CAPACITY = 10000; public void ArrayStack(); public void ArrayStack(int); private void checkCapacity(int); private void checkIntegrity(); public void push(Object); private void ensureCapacity(); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear(); } KingStacksLab/ArrayStack.ctxt #BlueJ class context comment0.target=ArrayStack comment1.params= comment1.target=ArrayStack() comment10.params= comment10.target=void\ clear() comment2.params=initialCapacity comment2.target=ArrayStack(int) comment3.params=capacity comment3.target=void\ checkCapacity(int) comment4.params= comment4.target=void\ checkIntegrity() comment5.params=newEntry comment5.target=void\ push(java.lang.Object) comment6.params= comment6.target=void\ ensureCapacity() comment7.params= comment7.target=java.lang.Object\ pop() comment8.params= comment8.target=java.lang.Object\ peek() comment9.params= comment9.target=boolean\ isEmpty() numComments=11 KingStacksLab/ArrayStack.java KingStacksLab/ArrayStack.java /**  * ArrayStack.java - array-based implementation of Stack ADT  */ import java.util.EmptyStackException; import java.util.Arrays; public class ArrayStack implements StackInterface {     private T[] stack;    // Array of stack entries     private int topIndex; // Index of top entry     private boolean integrityOK = false;     private static final int DEFAULT_CAPACITY = 50;     private static final int MAX_CAPACITY = 10000;     public ArrayStack()     {         this(DEFAULT_CAPACITY);     }      public ArrayStack(int initialCapacity)     {         integrityOK = false;         checkCapacity(initialCapacity);         T[] tempStack = (T[])new Object[initialCapacity];         stack = tempStack;         topIndex = -1;         integrityOK = true;     }      //helper method to ensure we are not trying to create an array that is too large     private void checkCapacity(int capacity)     {         if (capacity >= MAX_CAPACITY)             throw new IllegalStateException("Attempted to create bag whose capacity exceeds max allowed value.");     }     //checkIntegrity() - helper method to ensure bag is okay to work with     private void checkIntegrity()     {         if (!integrityOK)             throw new SecurityException("Data is corrupt.");     }     //push() - adds a new entry to the top of this stack     //  @param newEntry - the object to be added to the stack     public void push(T newEntry)     {         checkIntegrity();         ensureCapacity();         stack[topIndex + 1] = newEntry;         topIndex++;     }     //helper method to double size of array if it is full     private void ensureCapacity()     {         if (topIndex >= stack.length - 1) // If array is full, double its size         {             int newLength = 2 * stack.length;             checkCapacity(newLength);             stack = Arrays.copyOf(stack, newLength);         }      }      //pop() - removes and returns the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T pop()     {         checkIntegrity();         if (isEmpty())             throw new EmptyStackException();         else         {             T top = stack[topIndex];    //save the top item             stack[topIndex] = null;     //delete the spot in array             topIndex--;                 //topIndex is reduced by 1             return top;         }     }     //peek() - retrieves the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T peek()     {         checkIntegrity();         if (isEmpty())             throw new EmptyStackException();         else             return stack[topIndex];     }     //isEmpty() - detects whether this stack is empty     //  @return - TRUE if this stack is empty, FALSE otherwise     public boolean isEmpty()     {         return (topIndex<0);     }=""     //clear() - removes all entries from this stack=""     public void clear()=""     {=""         //delete each slot in array=""         for (int i =""><=topindex; i++)             stack[i] =" null;"             =""         topindex =" -1;"     }="" }="" kingstackslab/driver.class="" public="" synchronized="" class="" driver="" {="" public="" void="" driver();="" public="" static="" void="" main(string[]);="" public="" static="" void="" printtop(stackinterface);="" }="" kingstackslab/driver.ctxt="" #bluej="" class="" context="" comment0.target="Driver" comment0.text="\r\n\" driver.java\="" -\="" program\="" to\="" test\="" stack\="" adt\r\n\r\n="" comment1.params="args" comment1.target="void\" main(java.lang.string[])="" comment2.params="theStack" comment2.target="void\" printtop(stackinterface)="" numcomments="3" kingstackslab/driver.java="" kingstackslab/driver.java="" **=""  * driver.java - program to test stack adt=""  *=""  */="" public class driver="" {=""     public static void main(string[] args)=""     {=""         stackinterface stringstack ="">(2);                  stringStack.push("Jim");    //Jim         printTop(stringStack);                  stringStack.push("Jess");   //Jess -> Jim         printTop(stringStack);                  stringStack.push("Jill");   //Jill -> Jess -> Jim         printTop(stringStack);                  stringStack.push("Jane");   //Jane -> Jill -> Jess -> Jim         printTop(stringStack);                  stringStack.push("Joe");    //Joe -> Jane -> Jill -> Jess -> Jim         printTop(stringStack);                  stringStack.pop();    //Jane -> Jill -> Jess -> Jim         printTop(stringStack);                  stringStack.pop();    //Jill -> Jess -> Jim         printTop(stringStack);                  stringStack.push("Jules");    //Jules -> Jill -> Jess -> Jim         printTop(stringStack);     }          public static void printTop(StackInterface theStack)     {         System.out.println("The top item is: " + theStack.peek());     } } KingStacksLab/LinkedStack$1.class synchronized class LinkedStack$1 { } KingStacksLab/LinkedStack$Node.class synchronized class LinkedStack$Node { private Object data; private LinkedStack$Node next; private void LinkedStack$Node(LinkedStack, Object); } KingStacksLab/LinkedStack.class public synchronized class LinkedStack implements StackInterface { private LinkedStack$Node topNode; public void LinkedStack(); public void push(Object); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear(); } KingStacksLab/LinkedStack.ctxt #BlueJ class context comment0.target=LinkedStack comment1.params= comment1.target=LinkedStack() comment2.params=newEntry comment2.target=void\ push(java.lang.Object) comment3.params= comment3.target=java.lang.Object\ pop() comment4.params= comment4.target=java.lang.Object\ peek() comment5.params= comment5.target=boolean\ isEmpty() comment6.params= comment6.target=void\ clear() numComments=7 KingStacksLab/LinkedStack.java KingStacksLab/LinkedStack.java /**  * LinkedStack.java - linked list implementation of our Stack ADT  */ import java.util.EmptyStackException; public class LinkedStack implements StackInterface {     private Node topNode;          public LinkedStack()     {         topNode = null;     }          private class Node     {         private T data;     //the actual data Node holds         private Node next;  //pointer to next Node in list                  private Node(T theData)         {             data = theData;             next = null;         }     }          //push() - adds a new entry to the top of this stack     //  @param newEntry - the object to be added to the stack     public void push(T newEntry)     {         Node newNode = new Node(newEntry);                  newNode.next = topNode;                  topNode = newNode;      //this is exactly like LinkedBag's add() method!     }          //pop() - removes and returns the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T pop()     {         T topValue = peek();                  //we know not null b/c peek() didn't throw Exception         topNode = topNode.next;                  return topValue;     }          //peek() - retrieves the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T peek()     {         if (isEmpty())             throw new EmptyStackException();         else             return topNode.data;     }          //isEmpty() - detects whether this stack is empty     //  @return - TRUE if this stack is empty, FALSE otherwise     public boolean isEmpty()     {         return (topNode==null);     }          //clear() - removes all entries from this stack     public void clear()     {         topNode = null;     } } KingStacksLab/package.bluej #BlueJ package file dependency1.from=Driver dependency1.to=StackInterface dependency1.type=UsesDependency dependency2.from=Driver dependency2.to=VectorStack dependency2.type=UsesDependency editor.fx.0.height=1176 editor.fx.0.width=1936 editor.fx.0.x=-8 editor.fx.0.y=-8 objectbench.height=157 objectbench.width=760 package.divider.horizontal=0.6 package.divider.vertical=0.672 package.editor.height=329 package.editor.width=670 package.editor.x=120 package.editor.y=120 package.frame.height=600 package.frame.width=800 package.numDependencies=2 package.numTargets=5 package.showExtends=true package.showUses=true project.charset=UTF-8 readme.height=58 readme.name=@README readme.width=47 readme.x=10 readme.y=10 target1.height=50 target1.name=StackInterface target1.showInterface=false target1.type=InterfaceTarget target1.width=140 target1.x=200 target1.y=30 target2.height=50 target2.name=LinkedStack target2.showInterface=false target2.type=ClassTarget target2.width=130 target2.x=40 target2.y=200 target3.height=50 target3.name=VectorStack target3.showInterface=false target3.type=ClassTarget target3.width=130 target3.x=340 target3.y=200 target4.height=50 target4.name=Driver target4.showInterface=false target4.type=ClassTarget target4.width=80 target4.x=530 target4.y=100 target5.height=50 target5.name=ArrayStack target5.showInterface=false target5.type=ClassTarget target5.width=120 target5.x=200 target5.y=200 KingStacksLab/README.TXT ------------------------------------------------------------------------ This is the project README file. Here, you should describe your project. Tell the reader (someone who does not know anything about this project) all he/she needs to know. The comments should usually include at least: ------------------------------------------------------------------------ PROJECT TITLE: PURPOSE OF PROJECT: VERSION or DATE: HOW TO START THIS PROJECT: AUTHORS: USER INSTRUCTIONS: KingStacksLab/StackInterface.class public abstract interface StackInterface { public abstract void push(Object); public abstract Object pop(); public abstract Object peek(); public abstract boolean isEmpty(); public abstract void clear(); } KingStacksLab/StackInterface.ctxt #BlueJ class context comment0.target=StackInterface comment0.text=\r\n\ StackInterface.java\ -\ implementation\ of\ Stack\ ADT\r\n\r\n comment1.params=newEntry comment1.target=void\ push(java.lang.Object) comment2.params= comment2.target=java.lang.Object\ pop() comment3.params= comment3.target=java.lang.Object\ peek() comment4.params= comment4.target=boolean\ isEmpty() comment5.params= comment5.target=void\ clear() numComments=6 KingStacksLab/StackInterface.java KingStacksLab/StackInterface.java /**  * StackInterface.java - implementation of Stack ADT  *  */ public interface StackInterface {     //push() - adds a new entry to the top of this stack     //  @param newEntry - the object to be added to the stack     public void push(T newEntry);          //pop() - removes and returns the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T pop();          //peek() - retrieves the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T peek();          //isEmpty() - detects whether this stack is empty     //  @return - TRUE if this stack is empty, FALSE otherwise     public boolean isEmpty();          //clear() - removes all entries from this stack     public void clear();       } KingStacksLab/VectorStack.class public synchronized class VectorStack implements StackInterface { private java.util.Vector stack; private boolean integrityOK; private static final int DEFAULT_CAPACITY = 50; private static final int MAX_CAPACITY = 10000; public void VectorStack(); public void VectorStack(int); private void checkCapacity(int); private void checkIntegrity(); public void push(Object); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear(); } KingStacksLab/VectorStack.ctxt #BlueJ class context comment0.target=VectorStack comment1.params= comment1.target=VectorStack() comment2.params=initialCapacity comment2.target=VectorStack(int) comment3.params=capacity comment3.target=void\ checkCapacity(int) comment4.params= comment4.target=void\ checkIntegrity() comment5.params=newEntry comment5.target=void\ push(java.lang.Object) comment6.params= comment6.target=java.lang.Object\ pop() comment7.params= comment7.target=java.lang.Object\ peek() comment8.params= comment8.target=boolean\ isEmpty() comment9.params= comment9.target=void\ clear() numComments=10 KingStacksLab/VectorStack.java KingStacksLab/VectorStack.java /**  * VectorStack.java - Vector-based implement of Stack ADT  */ import java.util.Vector; import java.util.EmptyStackException; public class VectorStack implements StackInterface {     private Vector stack;     private boolean integrityOK;     private static final int DEFAULT_CAPACITY = 50;     private static final int MAX_CAPACITY = 10000;          public VectorStack()     {         this(DEFAULT_CAPACITY);     }          public VectorStack(int initialCapacity)     {         integrityOK = false;         checkCapacity(initialCapacity);         stack = new Vector(initialCapacity);         integrityOK = true;     }          private void checkCapacity(int capacity)     {         if (capacity > MAX_CAPACITY)             throw new IllegalStateException("Attempt to create a stack whose size exceeds max allowed value.");     }          private void checkIntegrity()     {         if (!integrityOK)             throw new SecurityException("Data is corrupt.");                  }          //push() - adds a new entry to the top of this stack     //  @param newEntry - the object to be added to the stack     public void push(T newEntry)     {         checkIntegrity();         stack.add(newEntry);     }          //pop() - removes and returns the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T pop()     {         checkIntegrity();         if(isEmpty())             throw new EmptyStackException();         else             return stack.remove(stack.size() - 1);  //remove entry in last spot of Vector and return it     }          //peek() - retrieves the top entry from this stack     //  @return - the object at the top of the stack     //  @throws - EmptyStackException if the stack is empty before the operation     public T peek()     {         checkIntegrity();         if(isEmpty())             throw new EmptyStackException();         else             return stack.lastElement();     }          //isEmpty() - detects whether this stack is empty     //  @return - TRUE if this stack is empty, FALSE otherwise     public boolean isEmpty()     {         checkIntegrity();         return stack.isEmpty();     }          //clear() - removes all entries from this stack     public void clear()     {         checkIntegrity();         stack.clear();     } }
Oct 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here