Question

C+++ Program Part A Create a class called die (the singular of dice). The die class...

C+++ Program

Part A

Create a class called die (the singular of dice). The die class should have a single data member called value. The die constructor should initialize the die’s value to 1. Create a member function called roll() that gives the die a random value between 1 and 6. Create a member function called show() that displays value. In main, demonstrate the creation of a die object to validate it is working.

Part B

Revise main to count the number of occurrences of each die value. The best result is maximum number of any particular value. If you have the same number of two values, the higher value determines the best result.

Sample Output:

Roll: 23541

Best result: You have one 5

Roll: 12346

Best result: You have one 6

Roll: 41321

Best result: Two of a kind: 1

Roll: 44364

Best result: Three of a kind: 4

Roll: 65533

Best result: Two of a kind: 5

(Note: even though there are two three's, the two five's make up the better result.)

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

Note: Variable names, function names, and class names are used in such a way for a better understanding. These can be changed according to the user's choice. Please refer to code snippets for a better understanding of comments and indentations.

IDE Used: Dev C++

Program Code

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// dice class definition
class die
{
   // private members
   private :
       int value;
  
   // public members  
   public :
  
   // default constructor
       die()
       {
           value = 1;          
       }
      
       // function to roll a dice
       int roll()
       {
          
           value = rand()%6 + 1;
           return value;
      
       }
      
       // function to show the die value
       void show()
       {
           cout<<value;
       }  
};


int main()
{
   // create a dice object
   die d1;
  
      
   int idx;
  
   // emoty array to store dice value occurence
   int roll_val[6]={};
  
   // to store best value and its occurence
   int best_val;
   int best_count=0;
  
   // making time seed to null
   srand(time(NULL));
  
   // showing the roll values
   cout<<"Roll : ";
  
   // 5 draws
   // can be changed according to user's choice
   for(idx=0; idx<5; idx++)
   {
       roll_val[d1.roll()-1]++;
       d1.show();
   }
  
   // finding the best value
   cout<<"\nBest Result : ";
  
   for(idx = 0; idx <6; idx++)
   {
   if(roll_val[idx]>=best_count)
       {
           best_val = idx+1;
           best_count = roll_val[idx];
       }
   }
  
   // showing the best value occurence
   if(best_count == 1)
   {
       cout<<"You have one "<<best_val;
   }
   else if(best_count == 2)
   {
       cout<<"Two of a kind : "<<best_val;
   }
   else if(best_count == 3)
   {
       cout<<"Three of a kind : "<<best_val;
   }
   else if(best_count == 4)
   {
       cout<<"Four of a kind : "<<best_val;
   }
   else
   {
       cout<<"Five of a kind : "<<best_val;
   }
      
   return 0;
}

Code Snippets

#include <iostream> #include <ctime> #include <cstdlib> using namespace std; // dice class definition class die 997 // privatint idx; // emoty array to store dice value occurence int roll_val[6]={}; // to store best value and its occurence int best_velse if (best_count == 3) cout<<Three of a kind : <<best_val; else if (best_count == 4) cout<<Four of a kind : <<best_val

Sample Outputs

Roll : 44132 Best Result : Two of a kind : 4 Process exited after 0.03589 seconds with return value o Press any key to continRoll : 55116 Best Result : Two of a kind : 5 Process exited after 0.02324 seconds with return value o Press any key to continRoll : 32415 Best Result : You have one 5 Process exited after 0.01664 seconds with return value o Press any key to continue.

Add a comment
Know the answer?
Add Answer to:
C+++ Program Part A Create a class called die (the singular of dice). The die class...
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
  • 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;...

  • Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will...

    Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will get a die face value of 1-6. We can use rand function and math ceiling function to get a random number of 1-6 generated. The program will simulating rolling 5 dice simultaneously and store their face values into an array of size 5. The program will keep rolling the dice and print the following: 1. All face values in one line 2. If any...

  • Using the Die class in chapter 4, write a class called PairOfDice, composed of two Die...

    Using the Die class in chapter 4, write a class called PairOfDice, composed of two Die object. 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.

  • Create a Dice Game: PYTHON This game is between two people. They will roll a pair...

    Create a Dice Game: PYTHON This game is between two people. They will roll a pair of dice each 10 times. Each roll will be added to a total, and after 10 rolls the player with the highest total will be the Winner! You will use the RANDOM class RANDRANGE to create a random number from 1 to 6. You'll need a function for "rollDice" to roll a pair of dice and return a total of the dice. You must...

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • Can anyone help me with this java program? We are still very early with lessons so...

    Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you Create 2 Java files for this assignment: Die.java and TestDie.java Part 1 - The Die Class             The program Design a Die class that contains the following attributes: sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have...

  • C++ Homework: This will be the first in a series of assignments to create a game...

    C++ Homework: This will be the first in a series of assignments to create a game called Zilch. In this assignment, you will create the basic structure of the program and demonstrate the ability to create programs with multiple files. It should serve as a refresher in the basics of C++ programming, especially in the handling of arrays. The initial assignments only establish a rough outline of the game. We will be adding the rules that make the game more...

  • C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

    C++ program Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int). Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor. Create a static function getStudentCount() that returns the value of studentCount. studentCount is used to track the number of student object has been instantiated. Initialize it to 0...

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