Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

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

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

how effective networks can be designed and operated

Answered: 1 week ago