Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you will write a program that simulates a tennis match. To win a match, the players play until one of them wins

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
For this assignment, you will write a program that simulates a tennis match. To win a match, the players play until one of them wins two or three sets [two for women's singles, three for men's singles]. To win a set, a player must win at least Er games and must win two more games than the other player. For example, if player 1 wins a 6th game and player 2 has only won 4 games, player 1 has won the set. However if player 1 wins a 6th game and player 2 has won 5 games, the set must continue until one player has won at least 2 more games than the other. Between games, players alternate who serves the ball. You will need to create a class called Tennis-Match that keeps track of all ofthe data needed to simulate a tennis match. In addition you must create a main class called You rla st nameAssignE .java [with your actual last name] that uses the TennisMatch class to run simulations until the user wants to stop. The TennisMatch class should have private elds representing the following values: 0 The number of sets required to win a match. '- The probability that player 1 wins a game when serving. 0 The probability that player 2 wins a game when serving. 0 Which player is currently serving. 0 The number of games in the current set that player 1 has won. 0 The number of games in the current set that player 2 has won. 0 The number of sets in the match that player 1 has won. 0 The number of sets in the match that player 2 has won. 0 A string used to keep track of the results of each set. There should be one constructor for the Tennis-Match that three arguments, one to initialize each of the three rst fields listed above. It should initialize the remaining fields as follows: 0 The initial server should he player 1. i The number of games and sets won by each player should initially be zero. 0 The string representing the results should initially be The class should contain one public method called playMatch that does the following: 0 Repeatedly plays a set [using the playSet method] until a player has won the match [determined using the matchOver' method.) 0 Displays the winner ofthe match along with the results of each set. You will also need to write the following private methods: private void p1aySet() does the following: - Resets the number of games won for each player to zero. 0 Repeatedly plays a game [using the playGame method] until a player has won the set [determined using the setDver method] 0 Increments the number of sets won for whichever player won the game. - Adds the results of the set to the string eld described above. For example, if player 1 won 3 games and player 2 won 6 games, results of the set are summarized as 3 5. private void p1ayame() does the following: 0 Uses Math . random(} to generate a random number between D and 1. 0 Compare the random number to the current server's win probability. If the number is less than or equal to that probability, the server won the game. Othelwise, the server's opponent has won the game. - Increment the number of games won by whoever won the game. 0 Switch the current server. private boolean matchDyer()Iturnstrueifoneofthetayershasyvontherequned number of sets to win, and False otherwise. private boolean setOver{) returns true ifone of the players has won at least 6 games and that player has won 2 more games than their opponent. Example Input and Output Here is an example run of the program [note that the numbers may vary as you are using a random number generator]: welcome to Nicholas Coleman's tennis match simulator Please enter the number of sets needed to win a match: 3 Please enter the probability of player 1 winning: 3.5 Please enter the probability of player 2 winning: 6.6 Player 1 wins: 64 46 5? T3 ?5 would you like to run another simulation? (YIN): Y Please enter the number of sets needed to win a match: 2 Please enter the probability of player 1 winning: 9.?5 Please enter the probability of player 2 winning: 3.5 Player 1 wins: 64 37 64 would you like to run another simulation? (YIN): N Thank you for using the tennis match simulator

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions