Question

12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following member variable: • A string named13. Tossing Coins for a Dollar For this assignment, you will create a game program using the Coin class from Programming Chal

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

Its a c++ problem

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

Note: I can only answer the first question as I need to follow the HOMEWORKLIB RULES. The answer to the first question is given below.

Program:

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

class Coin

{

    string sideUp; //  member variable sideUp

public:

    // constructor that randomly determines the side of the coin

    // that is facing up

    Coin()

    {

        if (rand() % 2 == 0)

        {

            sideUp = "heads";

        }

        else

        {

            sideUp = "tails";

        }

    }

    // void member function that simulates the tossing of the coin

    // it randomly determines the side of the coin that is facing up

    void toss()

    {

        if (rand() % 2 == 0)

        {

            sideUp = "heads";

        }

        else

        {

            sideUp = "tails";

        }

    }

    // it returns the value of the sideUp member variable

    string getSideUp()

    {

        return sideUp;

    }

};

// Driver program

int main()

{

    srand(time(0));

    Coin coinObject;

    int numberOfHeads = 0, numberOfTails = 0;

    // display the side that is initially facing up

    cout << "The side that is initially facing up: " << coinObject.getSideUp() << endl;

    // loop to toss the coin 20 times

    for (int i = 1; i <= 20; i++)

    {

        coinObject.toss();

        // each time the coin is tossed, display the side that is facing up

        cout << "The side that is facing up after " << i << " toss: " << coinObject.getSideUp() << endl;

        if (coinObject.getSideUp() == "tails")

        {

            numberOfTails++;

        }

        else

        {

            numberOfHeads++;

        }

    }

    // display the number of times heads is facing up

    cout << "Number of times heads is facing up: " << numberOfHeads << endl;

    // display the number of times tails is facing up

    cout << "Number of times tails is facing up: " << numberOfTails << endl;

    return 0;

}

Note: Please refer to the screenshot of the code to understand the indentation of the code.

Screenshot of the code:

Output:

Add a comment
Know the answer?
Add Answer to:
implement coinclass and driver program within one source file, i.e. do not separate class specifications with...
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...

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

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

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

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

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2: Design (pseudocode) and implement (source code) a class called Counter. It should have one private instance variable representing the value of the counter. It should have two instance methods: increment() which adds on to the counter value and getValue() which returns the current value. After creating the Counter class, create a program that simulates tossing a coin 100 times using two Counter objects (Head...

  • Can someone help me with this HW problem. It's a C# assignment. Two-Up (10 points) Two-up is an old gambling game u...

    Can someone help me with this HW problem. It's a C# assignment. Two-Up (10 points) Two-up is an old gambling game using two coins. Before the coins are tossed, the player would guess whether the toss would result in two heads (obverse), two tails (reverse) or one head and one tail (ewan). The "spinner" would spin (or toss) the two coins and the player would see if they won their bet. Write an application for the game Two-up with the...

  • A high-school teacher takes an afternoon to teach their class some basic ideas about probability. They do this by gettin...

    A high-school teacher takes an afternoon to teach their class some basic ideas about probability. They do this by getting the students to toss coins (head or tails) and set up a tournament where the 32 students split into pairs and have a contest: the students in each pair toss a coin ten times and the winner is the student who tosses the most heads. Then the 16 students who were winners in the first round are again split into...

  • lab #5 understanding radioactivity Experimental Procedure: Note: Samale.salculations were done by the instructor, Please see below....

    lab #5 understanding radioactivity Experimental Procedure: Note: Samale.salculations were done by the instructor, Please see below. Simply do what the instructor has done! This is a simple and fun home project. We will call it the Decay Game. Get 100 pennies. Think of them as a new radioactive isotope. We will call it Coinium isotope. One coin decays” when it comes up tails. Start with all your 100 coins by tossing them on the floor or a large table. Remove...

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