Westonci.ca is your trusted source for finding answers to all your questions. Ask, explore, and learn with our expert community. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.
Sagot :
Answer:
public class CoinFlips extends ConsoleProgram
{
public static final int FLIPS = 100;
public void run()
{
int countH = 0;
int countT = 0;
for(int i = 0; i < 100; i++)
{
if (Randomizer.nextBoolean())
{
System.out.println("Heads");
countH += 1;
}
else
{
System.out.println("Tails");
countT += 1;
}
}
System.out.println("Heads: " + countH);
System.out.println("Tails: " + countT);
System.out.println("% Heads: " + (double) countH / FLIPS);
System.out.println("% Tails: " + (double) countT / FLIPS);
}
}
Explanation:
First define your counting variables for both heads and tails (I named them countH and countT). Set them to 0 at the start of the run.
Then use a for loop to flip the coin 100 times. In the video you should have learned about the Randomizer class so you can use the same idea to print out whether you got heads or tails.
Make sure to keep the count going using >variable name< += 1.
The printing at the end is very basic; print the statement for each: ("Heads: " + >variable name<);
For the percentages, print ("% Heads: " + (double) >variable name< / FLIPS); divided by FLIPS (not 100 or any other int because you will get the wrong value) and remember to cast them as doubles to get the correct value.
The program simulates 100 coin flips and displays the result of each flip and the resulting percentage. The program written in python 3 goes thus :
import random
#import the random module
total = 0
#initialize the total coin flips
h_count = 0
t_count = 0
#initialize variable to hold the number of heads and tails
h_t = ['h', 't']
#define the sample space
while total < 100 :
#keeps track that tosses does not exceed 100
toss = random.sample(h_t, 1)
#variable to hold the outcome of each coin toss
if toss[0] == 'h':
#heck if toss is head
h_count+=1
Increase count of heads. owee
print(toss[0], end=' ')
#display the sample selected
else:
#if not head, then it's tail
t_count+=1
#increase count yv
print(toss[0], end=' ')
total+=1
#
print('head counts : ', h_count, 'percentage : ', round(h_count/100, 2),'%')
print('tail counts : ', t_count, 'percentage : ', round(t_count/100, 2), '%')
# display result.
A sample run of the program is attached
Learn more: https://brainly.com/question/18581972

Thank you for choosing our platform. We're dedicated to providing the best answers for all your questions. Visit us again. We hope you found what you were looking for. Feel free to revisit us for more answers and updated information. Westonci.ca is your go-to source for reliable answers. Return soon for more expert insights.