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

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

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

  • Project 2 Tasks: Create a Coin.java class that includes the following:             Takes in a coin...

    Project 2 Tasks: Create a Coin.java class that includes the following:             Takes in a coin name as part of the constructor and stores it in a private string             Has a method that returns the coins name             Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:             A constructor that passes the coins name to the parent             A private variable that defines the...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

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

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