Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java.. Homework 03 - Coin Flips How many coins would you need to flip in order to have a certain number of heads and

In Java..

image text in transcribed

image text in transcribedimage text in transcribed

Homework 03 - Coin Flips How many coins would you need to flip in order to have a certain number of heads and a certain number of tails? Your objective is to create a simulation that answers this question. Your program must continue running until the desired number of heads and the desired number of Lails are reached. Assume that for any particular coin flip, the chances of getting a head and the chances of getting a tail are both 50 percent. The program will take in from the user the number of sequnces to generate. Then it will prompt the user to enter the number of heads and the number of tails that the user wishes. Note, this value will be shared by all of the sequences in the current simulation. Once all of the sequences have been generated and printed with H representing a head and T representing a tail), the program should print out both the average and the maximum number of coin flips necessary to achieve the problem's constraint. After that, the user should be prompted whether they want to run another simulation and will continue in the same fashion until the user decides to quit. To run again, the user will input y. To stop, the user will input n. However, the user might not enter y or n-in that case, the program should continue prompting the user whether they want to run another simulation or not. A sample run might look like the following: Ready to run a coin flip simulation. Enter the number of sequences: 4 How many heads should each sequence have? 2 How many tails should each sequence have? 3 Simulating Sequences 1 - HHTHTT 2 - THHTT 3 - HTTHT 4 - TIHTTH The average number of flips vas 5.5 and maximum was 6 Would you like to run another simulation? (y): maybe Would you like to run another simulation? (y): y Ready to run a coin flip simulation. Enter the number of sequences: 3 How many heads should each sequence have? 1 How many tails should each sequence have? 1 Simulating Sequences 1 - HT 2 - TTH 3 - HHT The average number of flips was 2.67 and maximum was 3 Would you like to run another simulation? (y): n Notes and Hints Round the average number of flips to the nearest hundredth. You can assume that the user will only enter an integer for the number of sequences, number of heads, and the number of tails but an invalid int can be entered like - 1. Your program should handle situations where the user inputs an invalid int by going to the continue question, ie. Would you like to run another simulation? (y): and if the user chooses y, beginning another simulation. An example is below. Ready to run a coin flip simulation. Enter the number of sequences: 3 How many heads should each sequence have? -4 Would you like to run another simulation? (y): y $ java -jar checkstyle-8.28.jar yourFileName.java Starting audit... Audit done. The morange above menns there were no Checkstyle errors. If you had any errors, they would show up above this mariage, and the number at the end would be the points we would take off (limited by the checkstyle cap mentioned above). The Javn source files we provide contain no Checkstyle errors. In future homeworks we will be increasing this cap, so get into the habit of fixing these style errors early! Feature Restrictions There are a few features and methods in Java that overly simplify the concepts we are trying to teach or breaks our auto grader. For that renson, do not use any of the following in your final submission: var (the reserved keyword) System.exit Ready to run a coin flip simulation. Enter the number of sequences: 3 How many heads should each sequence have? 1 How many tails should each sequence have? 1 Simulating Sequences 1 - HT 2 - TH 3 - HT The average number of flips was 2 and maximum was 2 Would you like to run another simulation? (y): n Food for Thought A particularly interesting case is the simple one of getting one head and one tail. On average, how many coin flips do you think that will take? Try running simulations with more and more sequences of coin flips. How does the average change as there are more sequences? Is it converging to a value? How does the maximum change as well? We will not require you to turn in your observations. However, this highlights a pretty neat use of your CoinFlip.java filo, so give it a shot! Rubric . [100CoinFlip.java - 31) Input/Output Format 10 Prompts for 3 numbers in the following order: number of sequences, number of heads, number of tails [10] Outputs the correct number of HT sequence Strings 110 Correctly presents the option to run program again ( many times as the user wants) 1 Jumps to the Continue? (y) prompt if any negative number is input - 46 Functionality of simulation sequences Strings 30 Valid String format . [15] Has the minimum amount of heads and tails requested (output isn't too short) . 15| Stops after has the requested number of heads and tails (output isn't too long) . [16Strings match the random chances as described 18 Strings can randomly change in length and composition from sequence to sequence .18 There is an equal chance (50/50) of all or a T appearing as the next character in a sequence string - 120) Average and Maximum Correct and appear in the output after the last sequence string . [10Correct Average value (should be a floating-point value) 110 Correct Maximum Value (should be a whole number value) Allowed Imports To prevent trivialization of the assignment, you may only import java.util.Scanner and java.util. Random Checkstyle You must run checkstyle on your submission. The checkstyle cap for this assignment is 10 points. If you don't have checkstyle yet, download it from Canvas -> Files/Resources. Place it in the same folder as the files you want checkstyled. Run checkstyle on your code like so

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

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions

Question

Determine miller indices of plane A Z a/2 X a/2 a/2 Y

Answered: 1 week ago