Question

We want to design our die class (to be named aDie) such that we can have...

We want to design our die class (to be named aDie) such that we can have statements such as:


aDie D; // To instantiate a Die object

int rolled = D; // To get the value rolled by D

rolled = D + D; // To get the value of the sum of 2 rolls of the die

aDie d1, d2; // to instantiate 2 dice

rolled = d1 + d2; // To get the value of the sum of the roll of 2 dice


In essence, we are creating a new class that behaves like a random number whenever it is used in an expression whose result is an int.


As in project 1, we will want to visualize the histograms for different experiments.


We want to see the histograms for the faces of 1 die.

We want to see the histograms for the faces of the sum of 2 dice.


We want to see the histograms for the faces of the product of 2 dice


Design a coin class (to be called aCoin) that simulates a coin. You want to design it so that you can do this:


// Instantiate a coin

aCoin coin;

// Get the result of a “toss” of the coin this way:

String z = coin;

The variable z should only be “H” or “T” for Heads and Tail.


Then show the number of H’s and of T’s obtained by displaying their counts

Then display the histogram of the toss results. As before, the maximum count should be represented with 60 X’s


Finally we want to simulate drawing cards from a deck,with or without replacement. With replacement means that the card is placed back in the deck after having been drawn.


You will want to design a class that represents a deck of card (52 cards. 13 cards: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)


The class is to be called “aDeckOfCards”. And it should generate a random card drawn out of the deck.


The card should be printable with a string of the form: “Brand-Suit” where the brands are A,2,3,4,5,6,7,8,9,T,J,Q,K and the suits are “S,H,D,C for Spades, Heart, Diamond and Clubs)


You should be able to have statements such as:

// Declare a deck of cards

aDeckOfCards deck;

// Get a random card from the deck

aCard c = deck;// Draw a hand of N cards

Vector<aCard> hand = deck.draw(N);


Using your aDeckOfCards class, simulate the result of drawing 4 hands of 5 cards each (as in playing poker) and show each hand by printing the 5 cards of each hand. You should have a print method for a card so that you can print each card of the hand as in 4-S, K-H, K-D, 3-C, T-H for 4 of SPADE, King of HEART, KING of Diamond,3 of Club and TEN of Heart



Each of your classes should have its own “.h” file and “.cpp” files.


The aDie file for example should be implemented with “aDie.h” and “aDie.cpp”.




Code in C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

// File Name: aDie.h
#ifndef ADIE_H
#define ADIE_H
#include <cstdlib> // srand, rand
#include <ctime> // time
using namespace std;

class aDie
{
// To store die value
int val;
public:
// Default constructor to generate random value between 1 and 6
aDie();

// Overloading + operator
// Returns the added result
friend int operator+(const aDie &c1, const aDie &c2);

// Overloading function to convert object type to integer
// Returns the value
operator int() const;
};// End of class

#endif // End of ADIE_H

-----------------------------------------------------------------------------

#include <iostream>
#include "aDie.h"
using namespace std;

// Default constructor to generate random value between 1 and 6
aDie::aDie()
{
val = rand() % 6 + 1;
}// Default constructor

// Overloading + operator
// Returns the added result
int operator+(const aDie& c1, const aDie& c2)
{
// Adds the value of both the objects
return c1.val + c2.val;
}// End of function

// Overloading function to convert object to integer
// Returns the value
aDie::operator int() const
{
return val;
}// End of function

// main function definition
int main()
{
aDie D; // To instantiate a Die object

int rolled = D; // To get the value rolled by D
cout<<"\n Rolled value: "<<rolled;
rolled = D + D; // To get the value of the sum of 2 rolls of the die
cout<<"\n Sum of rolled value: "<<rolled;
aDie d1, d2; // to instantiate 2 dice

rolled = d1 + d2; // To get the value of the sum of the roll of 2 dice
cout<<"\n Sum of rolled die 1 and die 2: "<<rolled;
}// End of main function

Sample Output:

Rolled value: 6
Sum of rolled value: 12
Sum of rolled die 1 and die 2: 11

2)

// File Name: aCoin.h
#ifndef ACOIN_H
#define ACOIN_H
#include <cstdlib> // srand, rand
#include <ctime> // time
using namespace std;

class aCoin
{
// To store coin head or tail
string val;
public:
// Default constructor to generate random value between 'H' and 'T'
aCoin();

// Overloading function to convert object type to string
// Returns the value
operator std::string() const;
};// End of class

#endif // End of ACOIN_H

----------------------------------------------------------------------------------

#include <iostream>
#include "aCoin.h"
using namespace std;

// Default constructor to generate random value between 1 and 6
aCoin::aCoin()
{
int res = rand() % 1 + 0;
if(res == 0)
val = "T";
else
val = "H";
}// Default constructor

// Overloading function to convert object to integer
// Returns the value
aCoin::operator std::string() const
{
return val;
}// End of function

// main function definition
int main()
{
// Instantiate a coin
aCoin coin;
// Get the result of a "toss" of the coin this way:
string z = coin;
cout<<"\n Coin tossed: "<<z;
}// End of main function

Sample Output:

Coin tossed: T

Add a comment
Know the answer?
Add Answer to:
We want to design our die class (to be named aDie) such that we can have...
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
  • Specifications: C++ each class should have a header file all files must create a console application...

    Specifications: C++ each class should have a header file all files must create a console application in Visual Studio We want to design our die class (to be named aDie) such that we can have statements such as: aDie D; // To instantiate a Die object int rolled = D; // To get the value rolled by D rolled = D + D; // To get the value of the sum of 2 rolls of the die aDie d1, d2;...

  • Create A Header file and A CPP File for this we want to simulate drawing cards...

    Create A Header file and A CPP File for this we want to simulate drawing cards from a deck,with or without replacement. With replacement means that the card is placed back in the deck after having been drawn. You will want to design a class that represents a deck of card (52 cards. 13 cards: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) The class is to be called “aDeckOfCards”. And it should generate a...

  • 1. The probabilities that two students show up for class are 0.75 and 0.80 respectively Find...

    1. The probabilities that two students show up for class are 0.75 and 0.80 respectively Find the probability that: (a) Both show up for class? (b) Neither show up for class? (c) Exactly one shows up for class? (d) At least one shows up for class? 2. I rolled a pair of dice. What is the probability that I rolled: (a) A sum of 6 or a sum of 11? (b)A sum of 7 or doubles? 3. Consider a standard...

  • We flip a coin. If it is heads we roll a four sided die with sides...

    We flip a coin. If it is heads we roll a four sided die with sides numbered from 1 to 4. If it is tails, we roll a six sided die with sides numbered from 1 to 6. We let X be the number rolled. (a) What is the expectation of X? (b) What is the variance of X? (c) What is the standard deviation of X? We draw cards one by one and with replacement from a standard deck...

  • Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

    Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...

  • 1. Find the 20th term in the sequence 4, 6, 8, 10, … 2.  What term of...

    1. Find the 20th term in the sequence 4, 6, 8, 10, … 2.  What term of the sequence 2, 5, 8, … is 74? 3. Find the sum of the series 2 + 5 + 8 + … + 74. (see question 2) 4.  Find the 19th term of the sequence 1, 2 , 2, … 5. What is the sum of the first 16 terms of the sequence 2, 4, 8, …? 6.  A coin is flipped, and a four sided...

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

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • can someone help? I have my answers just want double check. thanks 10. Two fair dice...

    can someone help? I have my answers just want double check. thanks 10. Two fair dice are rolled and the sum of the numbers on their faces is observed. Find the probability that the sum is: a. Less that 2 b. Greater than 9 c. 7 d. Less than 5 11. Probability Experiments are described below. Describe the sample space for each of the following. 1. Tossing a coin. 2. Guessing the last digit of a telephone number. 3. Tossing...

  • *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card...

    *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card class, create several methods: a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards. b.shuffleDeck – input an unshuffled deck array and return a shuffled one. + Create a swap method to help shuffle the deck c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of...

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