Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 6: Simulating Playoffs The Playoffs Class Objectives Call methods with param

ID: 3698942 • Letter: L

Question

Lab 6: Simulating Playoffs

The Playoffs Class

Objectives

Call methods with parameters and return values.

Use the Scanner and Random classes.

Use while statements

Task

Simulate a best-of-seven games playoff using information supplied by the user. Print information about the result of each simulated playoff and about the result of multiple simulations. Ensure that the user enters a valid value for the simulation.

Details

Suppose two teams are playing each other in a best-of-seven playoff (the two teams play each other until one team wins four games). Suppose one team is more likely to win each game (say 51%). What are the chances that the better team will win the playoff?

We can solve this problem mathematically (the answer is about 52.2%), but simulations are often needed for more complicated problems involving uncertainty. Multiple simulations can give us an idea of what the chances are. For example, if we simulate a playoff 2500 times, our results will probably be within 1% of the correct answer (oversimplifying statistics here).

How can you simulate a 51% chance? The standard technique is to generate pseudorandom numbers, a sequence of numbers that appears to be random, but is actually generated by a program. The Random class provides pseudorandom numbers in Java. Here is an example.

// you need import java.util.*; at the top of your program

Random rand = new Random( );

int num1 = rand.nextInt(100);

if (num1 < 51) {

    System.out.println("Team 1 won the first game");

} else {

    System.out.println("Team 2 won the first game");

}

int num2 = rand.nextInt(100);

if (num2 < 51) {

    System.out.println("Team 1 won the second game");

} else {

    System.out.println("Team 2 won the second game");

}

Here, the nextInt method of the Random class is used to generate a random int between 0 and 99. Change the 100 to a different value if you want a different range. Each integer between 0 and 99 is equally likely. 51 of these integers are less than 51 (from 0 to 50), so testing if the integer is less than 51 means that 51 integers make the test true, and that 49 integers make the test false, a 51% chance of being true! Change the 51 to a different value if you want a different percentage.

We will need to be able to simulate a single game. If we can simulate a single game, we can simulate multiple games until one team wins four games. If we can simulate a playoff, we can simulate any number of playoffs. The user will be asked for the chance that Team 1 will win a given game.

Methods

Write one method to play a single game. This method should have two parameters: the percent chance that Team 1 will win the game, and a Random object. This method should only generate one random number. This method should return one value if Team 1 wins and a different value if Team 1 loses. In your final submission, this method should not print anything (comment out any print statements).

Write another method to simulate a playoff, that is, to play games until one team wins four games. This method must use a while loop for flow of control. This method should have two parameters: the percent chance that Team 1 will win the game, and a Random object. This method should return different values depending on which team wins. In your final submission, this method should not print anything (comment out any print statements).

Write a third method to keep doing playoffs until one team has won 10 more playoffs than the other team. This method must use a while loop for flow of control. This method should have two parameters: the percent chance that Team 1 will win the game, and a Random object. This method should print a 1 every time team 1 wins, and print a 2 every time team 2 wins; this should all be on one line. This method should also print the final results.

The main method should ask the user for the percent chance that team 1 will a game. It should ensure that the user enters an integer between 0 and 100. This method must use a while loop to ensure that a valid value has been entered. After obtaining a valid value, the main method should construct a Random object. Finally, the main method should pass the two values (the percent chance and the Random object) to the third method.

Explanation / Answer

Below is your code:-

public class BestOfSeven {

// main method

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter an integer for the percent chance: ");

int percentChance = input.nextInt();

while (percentChance < 0 || percentChance > 100) {

System.out.print("Enter an integer between 0 and 100 as the percent chance: ");

percentChance = input.nextInt();

}

Random rand = new Random();

int result1 = playSingleGame(percentChance, rand);

if (result1 == 1)

System.out.println(" 1. Team 1 wins the first game.");

else

System.out.println(" 1. Team 2 wins the first game.");

int result2 = simulatePlayoff(percentChance, rand);

if (result2 == 1)

System.out.println(" 2. Team 1 wins the second game.");

else

System.out.println(" 2. Team 2 wins the second game.");

keepDoingPlayoffs(percentChance, rand);

}

// first method

public static int playSingleGame(int percentChance, Random rand) {

int num = rand.nextInt(100);

if (num < percentChance)

return 1;

else

return 2;

}

// second method

public static int simulatePlayoff(int percentChance, Random rand) {

int team1wins = 0;

int team2wins = 0;

while (team1wins != 4 && team2wins != 4) {

int num = rand.nextInt(100);

if (num < percentChance)

team1wins++;

else

team2wins++;

}

if (team1wins == 4)

return 1;

else

return 2;

}

// third method

public static void keepDoingPlayoffs(int percentChance, Random rand) {

int team1wins = 0;

int team2wins = 0;

System.out.println(" 3.");

while ((team1wins - team2wins != 10) && (team2wins - team1wins != 10)) {

int num = rand.nextInt(100);

if (num < percentChance) {

System.out.println("Team 1 wins");

team1wins++;

} else {

System.out.println("Team 2 wins");

team2wins++;

}

}

if (team1wins > team2wins)

System.out.println(" Team 1 wins the third game.");

else

System.out.println(" Team 2 wins the third game.");

System.out.println("Number of playoffs won by team1: " + team1wins);

System.out.println("Number of playoffs won by team2: " + team2wins);

System.out.println("Total number of playoffs for the game: " + (team1wins + team2wins));

}

}

output

Enter an integer for the percent chance: 50

1. Team 1 wins the first game.

2. Team 1 wins the second game.

3.
Team 1 wins
Team 2 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 2 wins
Team 1 wins
Team 1 wins
Team 1 wins

Team 1 wins the third game.
Number of playoffs won by team1: 40
Number of playoffs won by team2: 30
Total number of playoffs for the game: 70