Question

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 USING JAVA ECLIPSE AND MAKE SURE IT IS SENT IN THE EXACT FORM AS I WILL BE COPYING AND PASTING IT INTO JAVA ECLIPSE, MAKE SURE TO STATE WHAT IS BEING DONE IN THE CODE. WILL GIVE A THUMBS UP.

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


import java.util.Random;

class Coin {
   private String sideUp;

   public Coin() {
       toss();
   }

   public void toss() {
       // generating random number, if it even return Heads else Tails
       if (new Random().nextInt(2) == 0)
           sideUp = "heads";
       else
           sideUp = "tails";
   }

   public String getSideUp() {
       return sideUp;
   }

}

public class CountFlips {
   public static void main(String[] args) {
       int headCount = 0, tailCount = 0;
       Coin cf = new Coin();
       //iterating 100 times
       for (int i = 0; i < 100; i++) {
           //tossing the coin
           cf.toss();
           //checking the side
           // increase count based on side
           if (cf.getSideUp().equals("heads"))
               headCount++;
           else
               tailCount++;
       }
       System.out.println("Tails count: " + tailCount);
       System.out.println("Heads count: " + headCount);

   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Write a program called CountFlips whose main method flips a coin 100 times and counts how...
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 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...

  • # 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...

  • 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...

  • 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...

  • 1: What do the following statements do? a) if (total != stock + warehouse) inventoryError =...

    1: What do the following statements do? a) if (total != stock + warehouse) inventoryError = true; if (found || !done) System.out.println("Ok"); b) count1 = 1; while (count1<=10) { count2 = 1; while (count2 < 20) { System.out.println("here"); count2++; } count1++; } Part 2: SEND AS A TEXT FILE USING JAVA ECLIPSE AND MAKE SURE IT IS SENT IN THE EXACT FORM AS I WILL BE COPYING AND PASTING IT INTO JAVA ECLIPSE, MAKE SURE TO STATE WHAT IS BEING...

  • Comment your code. At the top of the program include your name, a brief description of...

    Comment your code. At the top of the program include your name, a brief description of the program and what it does and the due date. The program must be written in Java and submitted via D2L. The code matches the class diagram given above. The code uses Inheritance. In the following, you are given code for two classes: Coin and TestCoin. You study these classes first and try to understand the meaning of every line. You can cut and...

  • Write a program where 3 separate threads simulate flipping a coin. The 3 threads should execute concurrently and each thread should do 1000 flips. Keep track of what is currently the record number of...

    Write a program where 3 separate threads simulate flipping a coin. The 3 threads should execute concurrently and each thread should do 1000 flips. Keep track of what is currently the record number of consecutive heads/tails (separately) and which thread produced that sequence. Every time a thread produces a longer sequence, display a message on the screen. Make sure other threads can’t interfere when updating the current record. N.B: This is a java object oriented program assignment.

  • Write a program called ShuffleDeck. This will simulate randomly shuffling a deck of cards. The cards...

    Write a program called ShuffleDeck. This will simulate randomly shuffling a deck of cards. The cards will be represented by strings found in the file deckofcards.txt. Begin by reading these strings into a queue of strings. Call a method with the signature: public static void shuffle(Queue deck) This method performs the following steps: Compute a value called the shuffle count that is the base 2 log of the size of the deck In a loop that executes shuffle count times:...

  • I need eclipse code for : Write a program that analyzes text written in the console...

    I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...

  • CAN SOMEONE HELP WITH THIS? Part 1: power() method We want to write a method that...

    CAN SOMEONE HELP WITH THIS? Part 1: power() method We want to write a method that will enable us to calculate a number to a given power. In other words, we are writing a method to mimic what the Math.pow() method we have seen before does. To write this method use the following requirements: The method should return an integer. It should have two parameters x and y both integers. The name of this method will be power(). For 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