Question

Random Number Generation and Static Methods Write a program that simulates the flipping of a coin...

Random Number Generation and Static Methods

Write a program that simulates the flipping of a coin n-times to see how often it comes up heads or tails. Ask the user to type in the number of flips (n) and print out the number of times the coin lands on head and tail. Use the method ‘random()’ from the Math class to generate the random numbers, and assume that a number less than 0.5 represents a tail, and a number bigger than or equal to 0.5 represents a head. Demo this before moving on to the next part.

Next, you are going to simulate rolling a 6-sided die several times and computing the sum of all the landings. Follow the steps below:

a- Write a static method called ‘sum_of_rolls’ that takes the number of times (m) you want to roll the die as an argument and returns the sum of all the landings. Set-up a loop inside this method that iterates exactly m times, and each time through the loop generate a random integer between 1 and 6, print that number out and also add it to the running total. Return the running total when the loop ends. This method doesn’t print the running total.

b- Now, add a couple of lines of code at the bottom of your main method to do the following: prompt for and read in the number of rolls; call the method ‘sum_of_rolls’; and then print out the returned value.

Include a loop in the program to allow the user to repeat the above steps as often as (s)he wishes. Demo this before moving on to the next part.

Sample Input/Output:

Programmer: Name of the programmer (your name) Course: COSC 111

Due date: 4-3-2019

Lab#: 8, part #1

Enter the number of flips: 20

Number of heads: 13

Number of tails: 7

Enter the number of rolls: 10

3 4 2 5 1 1 5 2 5 4

Sum of rolls = 32

Do it again, (yes or no)? Yes

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

import java.util.Scanner;

public class Flipcoin {

static boolean flipCoin() {

if (Math.random() < 0.5)

return false;

else

return true;

}

static int sum_of_rolls(int n) {

Random r = new Random();

int sum = 0;

for (int i = 0; i < n; ++i) {

int roll = r.nextInt(6) + 1;

System.out.print(roll + " ");

sum = sum + roll;

}

System.out.println();

return sum;

}

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

int n, heads = 0, tails = 0;

String str;

do {

System.out.print("Enter the number of flips: ");

n = keyboard.nextInt();

for (int i = 0; i < n; ++i) {

if (flipCoin())

++heads;

else

++tails;

}

System.out.println("Number of heads: " + heads);

System.out.println("Number of tails: " + tails);

System.out.print("Enter the number of rolls: ");

n = keyboard.nextInt();

System.out.println("Sum of rolls = " + sum_of_rolls(n));

System.out.println("Do it again, (yes or no)? ");

keyboard.nextLine();

str = keyboard.nextLine();

heads = 0; tails = 0;

} while (str.toLowerCase().equals("yes"));

}

}

===========================================
SEE OUTPUT
19 20 21 for Cint i-0; i< n; ++i)i <terminated> Flipcoin [Java Application] /Library/Java/JavaVirtualMac Enter the number of

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Random Number Generation and Static Methods Write a program that simulates the flipping of a coin...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that simulates the toss of a coin. Whenever a coin is tossed the...

    Write a program that simulates the toss of a coin. Whenever a coin is tossed the result will be either a head or tail. Prompt the user as shown by entering an ‘H’ for heads or ‘T’ for tails. Use a loop for input validation of an ‘h’ or ‘t’. Make sure that both an upper case and lower case will be accepted. Have the computer generate a random number. Assign a char variable a (‘h’ ^ ’t’) head or...

  • Write a function that flips a coin; it returns heads or tails. Use that function to...

    Write a function that flips a coin; it returns heads or tails. Use that function to write a GetHeadsInARow functions that accepts the number of heads to roll in a row before you return with total rolls it took to get that number of heads in a row. Please Code in Python3 my attempt below will not print out the answer and I do not know why def HeadTails(): from random import randrange """Will simulate the flip of a coin."""...

  • Write a program called CountFlips whose main method flips a coin 100 times and counts how...

    Write a program called CountFlips whose main method flips a coin 100 times and counts how many times each side comes up. Make sure to include comments of what is being done in the code. Verify whether the head comes up or not by calling isHeads method of Coin class. Increment the heads count if head comes up. Increment the tails count if tail comes up. Display the head and tails counts. Print the results SEND AS A TEXT FILE...

  • LabVIEW Homework (On NI LabVIEW Program) Write a program to simulate an experiment of flipping a...

    LabVIEW Homework (On NI LabVIEW Program) Write a program to simulate an experiment of flipping a coin a number of times (for example 100 times), and show the number of times getting heads and tails respectively. You can use random numbers to simulate the experiment. If the random number is >0.5, you can assume it is one side (for example Head), otherwise is the other side (for example Tail).

  • Write a C++ program that simulates tossing a coin. Allow the user to enter the number...

    Write a C++ program that simulates tossing a coin. Allow the user to enter the number of tosses. Print the number of tosses that yielded heads and the number of tosses that yielded tails. use function

  • # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss...

    # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...

  • Write a C++ program that simulates coin tossing. For each toss of the coin the program...

    Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.   The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • Using c++ Write a function named coinToss that simulates tossing a coin. When you call the...

    Using c++ Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2. 1 represents heads and 2 represents tails. Exercise 1 (40 points) Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2.1 represents heads and 2 represents tails Use the function...

  • Shandelle rolls a pair of fair dice and sums the number of spots that appear on...

    Shandelle rolls a pair of fair dice and sums the number of spots that appear on the up faces. She then flips a fair coin the number times associated with the sum of the spots. For example, if she rolled a 3 and a 4, then she flips the fair coin 7 times. If the coin flipping part of the random experiment yielded an equal number of heads and tails, find the probability that she rolled an 8 on the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT