Question: /** * @author Lewis and Chase * * Represents an array implementation of a stack. */ public class ArrayStack implements StackADT { /** * constant

 /** * @author Lewis and Chase * * Represents an array

/** * @author Lewis and Chase * 

* Represents an array implementation of a stack. */ public class ArrayStack implements StackADT { /** * constant to represent the default capacity of the array */ private final int DEFAULT_CAPACITY = 100; /** * int that represents both the number of elements and the next * available position in the array */ private int top; /** * array of generic elements to represent the stack */ private T[] stack; /** * Creates an empty stack using the default capacity. */ public ArrayStack() { top = 0; stack = (T[]) (new Object[DEFAULT_CAPACITY]); } /** * Creates an empty stack using the specified capacity. * * @param initialCapacity represents the specified capacity */ public ArrayStack(int initialCapacity) { top = 0; stack = (T[]) (new Object[initialCapacity]); } /** * Adds the specified element to the top of this stack, expanding * the capacity of the stack array if necessary. * * @param element generic element to be pushed onto stack */ public void push(T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; } /** * Removes the element at the top of this stack and returns a * reference to it. Throws an EmptyCollectionException if the stack * is empty. * * @return T element removed from top of stack * @throws EmptyCollectionException if a pop is attempted on empty stack */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("Stack"); top--; T result = stack[top]; stack[top] = null; return result; } /** * Returns a reference to the element at the top of this stack. * The element is not removed from the stack. Throws an * EmptyCollectionException if the stack is empty. * * @return T element on top of stack * @throws EmptyCollectionException if a peek is attempted on empty stack */ public T peek() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("Stack"); return stack[top - 1]; } /** * Returns true if this stack is empty and false otherwise. * * @return boolean true if this stack is empty, false otherwise */ public boolean isEmpty() { return (top == 0); } /** * Returns the number of elements in this stack. * * @return int the number of elements in this stack */ public int size() { return top; } /** * Returns a string representation of this stack. * * @return String representation of this stack */ public String toString() { if (isEmpty()) { return "The stack is empty."; } else { String result = "Stack: "; int scan = top; while (scan > 1) { scan--; result = result + stack[scan].toString() + ", "; } scan--; result = result + "one."; return result; } } /** * Creates a new array to store the contents of this stack with * twice the capacity of the old one. */ private void expandCapacity() { T[] larger = (T[]) (new Object[stack.length * 2]); for (int index = 0; index eventLog; public HockeyGame () { eventLog = new ArrayStack(); } public void emptyEventLog() { while (eventLog.isEmpty() == false) { System.out.println(eventLog.pop().toString()); } } public void simulate () { toronto = new HockeyTeam(torontoPlayers,"Toronto"); montreal = new HockeyTeam(montrealPlayers,"Montreal"); boolean torontoPossession = true; boolean intercept = false, pass = false; //Simulation code goes inside while (toronto.getGoals() = 50) { torontoPossession = true; eventLog.push(new HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()],"Toronto", "retrieved the puck")); } else { torontoPossession = false; eventLog.push(new HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()],"Montreal", "retrieved the puck")); } } else if (intercept) { intercept = false; torontoPossession = !torontoPossession; } //Any previous pass flags are reset as we move into the simulation behaviour. pass = false; // This is the code block for the Toronto team's events. if(torontoPossession) { int randomEvent = HockeyGame.rollDice(); //System.out.println(randomEvent); if (randomEvent { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element); /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T element on top of the stack */ public T peek(); /** Returns true if this stack contains no elements. * @return boolean whether or not this stack is empty */ public boolean isEmpty(); /** Returns the number of elements in this stack. * @return int number of elements in this stack */ public int size(); /** Returns a string representation of this stack. * @return String representation of this stack */ public String toString(); } public class TestStack { public static void main(String[] args) { ArrayStack S = new ArrayStack(4); if (S.isEmpty()) { System.out.println("Test 1 passed - isEmpty()"); } else { System.out.println("Test 1 failed - isEmpty()"); } S.push("one"); S.push("two"); S.push("three"); S.push("four"); S.push("five"); S.push("six"); S.push("seven"); S.push("eight"); S.push("nine"); if (S.size() == 9) { System.out.println("Test 2 passed - size()"); } else { System.out.println("Test 2 failed - size()"); } String firstElem = S.peek(); if (firstElem.equals("nine")) { System.out.println("Test 3 passed - first()"); } else { System.out.println("Test 3 failed - first()"); } // Pop several elements. for (int i = 0; i Exercise 2 - Simulating Hockey When trying to model a sport, it's important to consider numerous metrics or measures of performance. By studying these, coaches, fans, statisticians and more try to figure out what can be improved to get the largest changes in importance. One approach for seeing what changes make is to change parameters in a simulation. In this lab, we will focus on a simplistic simulation of a hockey game. We have made a method for you in HockeyGame.java called rollDice(), which will return an int between 1 and 100. We will use this to simulate two teams playing against each other. In Hockey, players can pass the puck between each other, shoot, or lose the puck to the other team. Here, you will print out the actions of the game as they are simulated You will need to write code that implements the following scenarios when HockeyGame.java is run. An example of all events to be pushed onto the team's stack are included with the lab code: Stage 1: Loose Puck o Call rollDice(), if result is 1-50, team 1 (stack 1) gets the puck. Else (51-100) team 2 (stack 2) gets the puck. For the appropriate team's stack, push a node onto the stack. o O . o Stage 2: Puck acquired or Pass Received Call rollDice() and then do the following: 1-10, return to stage 1 (Team loses puck, Empty stack) 11-20, other team intercepts puck, return to Stage 2 with other team. Empty stack. 21-80, Team passes successfully, return to Stage 2 for same team. Record a pass. 81-90, Team shoots but doesn't score. Proceed to Stage 1 & empty stack. Record a shot. 91-100, Team shoots and scores! Add a goal for Team. Proceed to Stage 1 & empty stack. Record a shot. . o Your program should print out: All actions taken and by which team, the number of shots taken by each team, the number of passes made by each team, and the final score. o CS1027 LAB 5 Computer Science Fundamentals II Your final line should look something like (with different numbers most likely): FINAL SCORE TORONTO with 3 goals and 5 shots and 9 passes VS MONTREAL with 2 goals and 2 shots and 1 pass Questions: 1. Consider the possibility of using a Linked Stack instead of the Array Stack in this algorithm. Would the simulation's results be impacted by switching to a different Stack implementation? Which of these classes/methods would you have to modify if you were going to use the other Stack implementation? 2. Which methods in HockeyGame.java are static and which are non-static? Explain why the static methods are static, and why other methods are not. Could HockeyTeam.java and HockeyEvent.java call static methods from HockeyGame? Optional: A more developed simulation would include things like individual player objects, which may have different percentages for pass success, shot accuracy, etcetera. List 5 changes that would help make your simulation more helpful for a sports team. Exercise 2 - Simulating Hockey When trying to model a sport, it's important to consider numerous metrics or measures of performance. By studying these, coaches, fans, statisticians and more try to figure out what can be improved to get the largest changes in importance. One approach for seeing what changes make is to change parameters in a simulation. In this lab, we will focus on a simplistic simulation of a hockey game. We have made a method for you in HockeyGame.java called rollDice(), which will return an int between 1 and 100. We will use this to simulate two teams playing against each other. In Hockey, players can pass the puck between each other, shoot, or lose the puck to the other team. Here, you will print out the actions of the game as they are simulated You will need to write code that implements the following scenarios when HockeyGame.java is run. An example of all events to be pushed onto the team's stack are included with the lab code: Stage 1: Loose Puck o Call rollDice(), if result is 1-50, team 1 (stack 1) gets the puck. Else (51-100) team 2 (stack 2) gets the puck. For the appropriate team's stack, push a node onto the stack. o O . o Stage 2: Puck acquired or Pass Received Call rollDice() and then do the following: 1-10, return to stage 1 (Team loses puck, Empty stack) 11-20, other team intercepts puck, return to Stage 2 with other team. Empty stack. 21-80, Team passes successfully, return to Stage 2 for same team. Record a pass. 81-90, Team shoots but doesn't score. Proceed to Stage 1 & empty stack. Record a shot. 91-100, Team shoots and scores! Add a goal for Team. Proceed to Stage 1 & empty stack. Record a shot. . o Your program should print out: All actions taken and by which team, the number of shots taken by each team, the number of passes made by each team, and the final score. o CS1027 LAB 5 Computer Science Fundamentals II Your final line should look something like (with different numbers most likely): FINAL SCORE TORONTO with 3 goals and 5 shots and 9 passes VS MONTREAL with 2 goals and 2 shots and 1 pass Questions: 1. Consider the possibility of using a Linked Stack instead of the Array Stack in this algorithm. Would the simulation's results be impacted by switching to a different Stack implementation? Which of these classes/methods would you have to modify if you were going to use the other Stack implementation? 2. Which methods in HockeyGame.java are static and which are non-static? Explain why the static methods are static, and why other methods are not. Could HockeyTeam.java and HockeyEvent.java call static methods from HockeyGame? Optional: A more developed simulation would include things like individual player objects, which may have different percentages for pass success, shot accuracy, etcetera. List 5 changes that would help make your simulation more helpful for a sports team

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!