Question

# 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 class should have an Instance variable 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 following methods:

A void method named toss, which simulates the tossing of a coin. When the toss method is called, it randomly determines the side of the coin that is facing up ("heads" or "tails") and sets the sideUp field accordingly.

A no-arg constructor, which randomly determines the side of the coin, that is facing up ("heads" or "tails") and initializes the sideUp field accordingly.

A method named getSideUp that returns the value of the sideUp field.

Create a toss method that uses loop to toss the coin 20 times. Each time the coin is tossed, display the side that is facing up. The method should keep count of the number of times heads or tails is facing up and display those values after the loop finishes.

Write the test program, which has main method and demonstrates the Coin class. Submit a class diagram, test runs and code (.java file) with your submission.

HINTS:

Design and Test

Let's decide what classes, methods and variables will be required in this task and their significance:
java.util.Random to determine heads/tails
Write a class called Coin, Driver and Simulation.
- Coin() - sideup - constructor (random decision to use head or tails)
- decide on any other method needed.
- void toss()


//there has to be another object that tosses coin.
Simulation
properties ..heads, and tails.
methods
..Simulation()
..void tossforsimulation(Coin n, int count) //loop in which coin is tossed and stats tracked.
..printstats(Coin n) //print can call toss method 20 times in a loop and then print # of times head or tails.

********************Possible framework for lab 2 part 2**************************


public class Coin {
   private String sideUp;

   public Coin() {
       // use a random approach (pl. use google or stackoverflow
       // for tossing the coin.
   }

   public void toss() {
       // sets the value of sideup
       //using the same logic in the Coin() default constructor.
   }
  

   public String getsideUp() {

       return sideUp;
   }
}


public class Simulation {
   public void toss(Coin c){
       //Create a loop to toss the coin 20 times
       //and count how many time we get heads or tails.
       // - use the coin toss() and getSideUp().
       //print output showing # of heads or tails
   }
}


public class Driver {

   public static void main(String[] args) {
       //Create a Coin object.
       //Create a Simulation Object
       //Call the toss method in Simulation
       //Write 2 to 3 test cases for this
       //Create a quarter, dime and nickel object
       //toss each coin 20 times.
       Coin quarter = new Coin();
       Coin dime = new Coin();
       Coin nickel = new Coin();
       Simulation s1 = new Simulation();
       s1.toss(quarter);
       s1.toss(dime);
       s1.toss(nickel);
   }

}

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

//Solution 1

//Java code

import java.util.Random;

public class Coin {
    // sideUp field will hold either "heads" or "tails"
    private String sideUp;
    Random random = new Random();
    public Coin() {
        // use a random approach (pl. use google or stackoverflow
        // for tossing the coin.

        int face = random.nextInt(2)+1;
        if(face == 1)
            sideUp = "head";
        if(face==2)
            sideUp ="tail";
    }

    /**
     * toss method that uses loop to toss the coin 20
     * times. Each time the coin is tossed, display
     * the side that is facing up. The method should keep
     * count of the number of times heads or tails is
     * facing up
     * and display those values after the loop finishes.
     */
    public void toss() {
        // sets the value of sideup
        //using the same logic in the Coin() default constructor.
        int countTail=0,countHead =0;
        for (int i = 0; i <20; i++) {
            int face = random.nextInt(2)+1;
            if(face == 1) {
                sideUp = "head";
                countHead++;
            }
            if(face==2) {
                sideUp = "tail";
                countTail++;
            }
            System.out.println("Side Up Value: "+sideUp);
        }
        System.out.println("Tail occurs: "+countTail+" times");
        System.out.println("Head occurs: "+countHead+" times");
    }


    public String getsideUp() {

        return sideUp;
    }
}

//=============================

public class Simulation {
public static void main(String[] args)
{
    Coin quarter = new Coin();
    Coin dime = new Coin();
    Coin nickel = new Coin();
  quarter.toss();
  System.out.println("=================================");
   dime.toss();
    System.out.println("=================================");
    nickel.toss();

}
}

//Output

//Second Solution:)

import java.util.Random;

public class Coin {
    // sideUp field will hold either "heads" or "tails"
    private String sideUp;
    Random random = new Random();
    public Coin() {
        // use a random approach (pl. use google or stackoverflow
        // for tossing the coin.

        int face = random.nextInt(2)+1;
        if(face == 1)
            sideUp = "head";
        if(face==2)
            sideUp ="tail";
    }

    /**
     * toss method that uses loop to toss the coin 20
     * times. Each time the coin is tossed, display
     * the side that is facing up. The method should keep
     * count of the number of times heads or tails is
     * facing up
     * and display those values after the loop finishes.
     */
    public void toss() {
        // sets the value of sideup
        //using the same logic in the Coin() default constructor.
        int countTail=0,countHead =0;
        for (int i = 0; i <20; i++) {
            int face = random.nextInt(2)+1;
            if(face == 1) {
                sideUp = "head";
                countHead++;
            }
            if(face==2) {
                sideUp = "tail";
                countTail++;
            }
            System.out.println("Side Up Value: "+sideUp);
        }
        System.out.println("Tail occurs: "+countTail+" times");
        System.out.println("Head occurs: "+countHead+" times");
    }


    public String getsideUp() {

        return sideUp;
    }
}

//========================================

public class Simulation {

    public void toss(Coin n)
    {
        n.toss();
    }
}

//=======================================

public class Driver {
public static void main(String[] args)
{
    Coin quarter = new Coin();
    Coin dime = new Coin();
    Coin nickel = new Coin();
    Simulation s1 = new Simulation();
    s1.toss(quarter);
    System.out.println("=======================");
    s1.toss(dime);
    System.out.println("=======================");
    s1.toss(nickel);

}
}

//Output

//If you need any help regarding this solution ........ please leave a comment ......... thanks

Add a comment
Know the answer?
Add Answer to:
# JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss...
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
  • c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

    c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...

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

  • Please write this C# code- Your main program will use a class named Coin, which has...

    Please write this C# code- Your main program will use a class named Coin, which has the following field: • A private 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 has 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 SideUp field accordingly. • A void method...

  • implement coinclass and driver program within one source file, i.e. do not separate class specifications with...

    implement coinclass and driver program within one source file, i.e. do not separate class specifications with implementation Its a c++ problem 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following member variable: • A string named sideUp. The sideUp member variable will hold either "heads" or "tails" indicating the side of the coin that is facing up. The Coin class should have the following member functions: • A default constructor that randomly determines...

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

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

  • Money Lab using arraylists in Java Using the Coin class below create a program with the...

    Money Lab using arraylists in Java Using the Coin class below create a program with the following requirements Create an arraylist that holds the money you have in your wallet. You will add a variety of coins(coin class) and bills (ones, fives, tens *also using the coin class) to your wallet arraylist. Program must include a loop that asks you to purchase items using the coins in your wallet. When purchasing items, the program will remove coins from the arraylist...

  • The Coin Tossing simulation Coin.java The Coin class must be the enum class, defining the instances...

    The Coin Tossing simulation Coin.java The Coin class must be the enum class, defining the instances HEADS and TAILS. CoinTossing.java The CoinTossing class should be a simple class (with a main method) that uses the values provided by a Coin class, and performs a simulation of the tossing of a coin. There must be a separate (possibly static) flip method that takes no arguments and returns a value of the Coin class. The result returned from the flip method is...

  • Tosssing Coins for a Dollar (C++) For this assigment, you will create a game program using...

    Tosssing Coins for a Dollar (C++) For this assigment, you will create a game program using the Coin class from Programming Challenge 12 (Coin Toss Simuator). The prgram should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the...

  • Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a...

    Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a game program using the Coin class from Programming Challenge 12. The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of 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