Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. public interface StackADT { public void push (T element); public T pop(); public T peek(); public boolean isEmpty(); public int size(); public String toString();

1. public interface StackADT{ public void push (T element); public T pop(); public T peek(); public boolean isEmpty(); public int size(); public String toString(); }

2.

public class ArrayStack implements StackADT { private final int DEFAULT_CAPACITY = 100; private int top; private T[] stack; public ArrayStack() { top = 1; // Do not change this! stack = (T[])(new Object[DEFAULT_CAPACITY]); } public ArrayStack (int initialCapacity) { top = 1; // Do not change this! stack = (T[])(new Object[initialCapacity]); } public void push (T element) { } public T pop() throws EmptyCollectionException { } public T peek() throws EmptyCollectionException { } public boolean isEmpty() { } public int size() { } public String toString() { } private void expandCapacity() { T[] larger = (T[])(new Object[stack.length*2]); for (int index=0; index  

3.

public class EmptyCollectionException extends RuntimeException {

public EmptyCollectionException (String collection) {

super ("The " + collection + " is empty.");

}

}

4.

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  

5.

public class HockeyTeam { private String[] players; private String team; private int shots; private int goals; private int passes; public HockeyTeam (String[] players, String team) { this.players = players; this.team = team; shots = 0; goals = 0; } public String getPlayer (int index) { return players[index]; } public String getTeam () { return team; } public void addShot() { this.shots++; } public void addGoal() { this.goals++; } public void addPass() { this.passes++; } public int getPasses() { return this.passes; } public int getShots() { return this.shots; } public int getGoals() { return this.goals; } }

6.

public class HockeyEvent { private String playerName; private String eventType; private String teamName; public HockeyEvent(String player_name, String teamName, String event_type) { this.playerName = player_name; this.teamName = teamName; this.eventType = event_type; } public String getPlayerName() { return this.playerName; } public String getEventType() { return this.eventType; } public String getTeam() { return this.teamName; } public String toString() { return playerName + " from " + teamName + " " + eventType; } }

7.

public class HockeyGame { private HockeyTeam toronto; private HockeyTeam montreal; private String[] torontoPlayers = new String[] { "Tavares", "Matthews", "Marner", "Rielly", "Ceci" }; private String[] montrealPlayers = new String[] { "Tatar", "Danault", "Lehkonen", "Chiarot", "Weber" }; private int goalsToWin = 3; private ArrayStack 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  

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Exercise 1 - Completing the Array Stack class 1. Open ArrayStack.java and TestStack.java and examine the code in both classes. 2. Note that ArrayStack is not complete; most of its methods are left empty. Without adding any additional instance variables or methods or changing any of the provided code, complete the six empty methods given the following instructions: a. push(T element) - if the array is full, call expandCapacity (which is provided). Add the given element to the top of the stack. b. pop() - if the stack is empty, throw an Empty CollectionException. If not, remove and return the element at the top of the stack. c. peek () - if the stack is empty, throw an EmptyCollectionException. If not, retrieve the element at the top of the stack and return it without removing it. d. i sEmpty() - return true if there are no elements, and false otherwise. e. size() - return the number of elements in the stack. f. tostring() - if the stack is empty, return "The stack is empty." Otherwise return a single-line string starting with "Stack: " and then containing all the elements of the stack from the top to the bottom. There must be a comma and space between elements but the last element must end with a period instead. i.e. Stack: 1, 2, 3, 4, 5. Hint: how can you determine when the loop has reached the last element? g. NOTE: In this implementation, the default value of top is 1 rather than 0. This is different than the example shown in lecture. Do not change this default value, but rather think about how to adjust these methods to work with this value of top. 3. Run the TestStack file. This will test that the ArrayStack methods were properly implemented. If any of the test failed, fix the corresponding methods in ArrayStack until all the tests pass. You may add print lines in ArrayStack to help with the debugging. Do not add print lines in TestStack or alter this file in any way. 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. Stage 2: Puck acquired or Pass Received o 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. Your program should print out: All actions taken and by which team, the number of shots taken by each team, o the number of passes made by each team, and o the final score. 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 1 - Completing the Array Stack class 1. Open ArrayStack.java and TestStack.java and examine the code in both classes. 2. Note that ArrayStack is not complete; most of its methods are left empty. Without adding any additional instance variables or methods or changing any of the provided code, complete the six empty methods given the following instructions: a. push(T element) - if the array is full, call expandCapacity (which is provided). Add the given element to the top of the stack. b. pop() - if the stack is empty, throw an Empty CollectionException. If not, remove and return the element at the top of the stack. c. peek () - if the stack is empty, throw an EmptyCollectionException. If not, retrieve the element at the top of the stack and return it without removing it. d. i sEmpty() - return true if there are no elements, and false otherwise. e. size() - return the number of elements in the stack. f. tostring() - if the stack is empty, return "The stack is empty." Otherwise return a single-line string starting with "Stack: " and then containing all the elements of the stack from the top to the bottom. There must be a comma and space between elements but the last element must end with a period instead. i.e. Stack: 1, 2, 3, 4, 5. Hint: how can you determine when the loop has reached the last element? g. NOTE: In this implementation, the default value of top is 1 rather than 0. This is different than the example shown in lecture. Do not change this default value, but rather think about how to adjust these methods to work with this value of top. 3. Run the TestStack file. This will test that the ArrayStack methods were properly implemented. If any of the test failed, fix the corresponding methods in ArrayStack until all the tests pass. You may add print lines in ArrayStack to help with the debugging. Do not add print lines in TestStack or alter this file in any way. 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. Stage 2: Puck acquired or Pass Received o 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. Your program should print out: All actions taken and by which team, the number of shots taken by each team, o the number of passes made by each team, and o the final score. 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

describe the main employment rights as stated in the law

Answered: 1 week ago