Question: How would I approach this example exercise using arrays? PROBLEM 1 Arrays Write a Java application to simulate coin tosses and compute the fraction of

How would I approach this example exercise using arrays?

How would I approach this example exercise using arrays? PROBLEM 1 Arrays

PROBLEM 1 Arrays Write a Java application to simulate coin tosses and compute the fraction of heads and fraction of tails. As a challenge, determine the longest run of one outcome; that is, the most consecutive heads or consecutive tails (whichever is longer). To simulate the toss of a coin, use the random method in the Math utility class, which generates a number greater than or equal to 0 and less than 1 . 0. The notation for this range is [0 , l . 0), where the square bracket means to include the 0 in the range and the rounded parenthesis means to exclude the 1.0 from the range. Since there are two outcomes of equal probability in the toss of a \"fair" coin, divide the range of numbers between [0 , l . 0) into two roughly equal size sub-ranges. Assume any number less than 0 . 5 will be considered as a tail while any number greaterthan or equal to 0 . 5 as a head. Program Organization Requirements: CoinToss . java 0 A parameterized constructor that accepts an integer parameter for the number of tosses. This value will be used to set the size of an array that will hold the coin toss results. I A loop is used to ll an array with the characters 'h' (head) and 't' (tail). As described above, use a random number to determine if each \"coin toss\" is a head or a tail. This array initialization is accomplished in the constructor (HINT: you can keep a count of the number of heads and tails as attributes as you fill the array). 0 Create methods that return the number of heads and the number of tails in the coin toss array (these are accessors). o Create a method that returns the total number of coin tosses (this is the length of the array). 0 Create a method that computes the longest (consecutive) number of heads or tails in the coin tosses array. This method stores this information in two attributes: one for the length of the longest run, and one for the outcome of that longest run (head or tail). Initialize the length to 0. To compute the longest run, keep two MORE values (not attributes, but local variables: one for the length of the current run of identical outcomes, and one for the outcome associated with the run (head or tail). 0 Initialize the outcome for the current run to neither a head nor a tail (perhaps an 'x') and the length of the run to 0' o In a loop. compare each toss in the array to the outcome of the current run. I If they match, the current run is now recognized as one longer (increment its length). I If they don't match, then the current run has ended. Compare the length of this run to the longest run that has been previously seen (the longest run so far). 0 If this is current run is longer, record its length and outcome in the two attributes (above). I Whether or not this is a new longest run, reset the current outcome to the one just examined (the mismatch) and reset the length of the current run to 1. Think about ifany special processing needs to happen when the loop terminates. 0 Define accessor and mutator methods for the length and outcome of the longest run. 0 Define any additional methods as needed. TestCoinToss . java I Prompt the user for the number of coin tosses. 0 You will repeat the run until the user enters any value less than 1. TESTING The results should be similar in nature to those shown in the sample run window below. Because we are using a random number generator, the results will change each time the application is executed. Use Scanner and a loop to get the number of coin tosses. ---jGRASP exec: java -ea -ea Tastcoin'l'oss Enter integer number ( >= 2) coin tosses or 0 to Exit: 29 Number of Coin Testes = 29 Fraction of Heads 0.517 Fraction of Tails 0.493 Longest run is 5 tails Enter integer number ( >= 2) coin tosses or 0 to Exit: 725 Number of Coin 10550: = 725 Fraction of Heads = 0.520 Fraction o Tails 0.490 Longest run is 9 heads Enter integer number ( >'= 2) coin tosses or 0 to Exit: 0 End of Program ----jGRASP: operation complete

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!