Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Dice class that simulates rolling a die with a different number of side

ID: 3858560 • Letter: C

Question

Create a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. You should have a default constructor that sets the number of sides to 6. You should have another constructor that creates a new instance of Dice with the number of sides in the argument. You should have one member function, Roll(), that rolls one die.

There are some firm physical constraints. For example, you cannot have a die with 0 or negative sides. Are there any other constraints you must validate?

Write a main function that creates two Dice objects with a number of sides of your choosing. Roll both dice and add the results. Roll the dice 10 times. Do the results make sense? Now create a class, LoadedDice that is derived from Dice. Override the Roll() function. For any result other than the number of sides there is a 5% chance a one will be added to the results. For example on a 6-sided die a roll of 1, 2, 3, 4, or 5 has a 5% chance of being 1 higher. A 6 is always a 6. You should still be able to create a LoadedDice object that has the default 6 sides, or the number specified when you call the constructor. Do you need to change or add anything else?

Test your class by adding code to roll dice with LoadedDice objects. You should not need to change anything else.

Test your program. How can you test it to ensure the distribution of results is higher for the loadedDice objects? Each class should be in its own file. Separate header files are not required for this lab but feel free to use them!

Explanation / Answer

/*The C ++ program that creats two objects of Dice class and calls the roll function for ten times
and 100 times and sum the results and print to console. The create two obejcsts of LoadedDice class
and calls the roll function for ten times and 100 times and sum the results and print to console*/
//header files
#include <iostream>
#include<time.h>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;

//class Die
class Dice
{

//private data members
protected:
   int sideUp;
   int NUM_SIDES;

//public member funtions
public:
   //no-arg costructor
   Dice();
   //paramter die constructor
   Dice(int sides);
   //function to set value
   void setValue(int);
   //function to get value
   int getValue();
   //function roll
   void roll();

};

//no-arg constructor
Dice::Dice()
{
   //Set NUM_SIDES
   NUM_SIDES = 6;
   //call roll method to set random sideUp value
   roll();
}

//paramter die constructor
Dice::Dice(int sides)
{
   //Set NUM_SIDES
   NUM_SIDES = sides;
   //call roll method to set random sideUp value
   roll();
}

//Function that takes value and sets value to sideUp
void Dice::setValue(int value)
{
   sideUp=value;
}
//Function that returns sideUp
int Dice::getValue()
{
   return sideUp;
}
//function roll that sets sideUp value
void Dice::roll()
{
   sideUp= rand()%NUM_SIDES+1;
}


//class defintion of LoadedDice
//class LoadedDice
class LoadedDice:public Dice
{

//public member funtions
public:
   //no-arg costructor
   LoadedDice();
   //paramter die constructor
   LoadedDice(int sides);  
   void roll();
};


//default constructor
LoadedDice::LoadedDice()
{
   NUM_SIDES=6;
}
//parameterized constructor
LoadedDice::LoadedDice(int sides)
{
   NUM_SIDES=sides;
}


/*Overloaded roll function that rolls the dice
and check if random value is 1,2,3,4,5 then increment by 1 */
void LoadedDice::roll()
{
   Dice::roll();

   int r=getValue();

   switch(r)
   {
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
       sideUp=sideUp+1;
       break;
   }

}

//start of main method
int main()
{
   //seed the random number generator  
   srand ( time(NULL) );

   int sum=0;
   int COUNT=10;
   //Create a two Die class objects for dealer
   Dice die1;
   Dice die2;
  

   cout<<"Rolling die1 and die2 "<<COUNT<<" times ."<<endl;
   //Roll the die
   for( int cnt = 1; cnt <= COUNT; cnt++ )
   {
       die1.roll();
       die2.roll();
       sum=die1.getValue()+die2.getValue();
       cout<<"Sum of die1 + die2 : "<<sum<<endl;
   }

   COUNT=100;
   cout<<"Rolling die1 and die2 "<<COUNT<<" times ."<<endl;
   //Roll the die
   for( int cnt = 1; cnt <= COUNT; cnt++ )
   {
       die1.roll();
       die2.roll();
       sum=die1.getValue()+die2.getValue();
       cout<<"Sum of die1 + die2 : "<<sum<<endl;
   }


   //Create a two Die class objects for dealer
   LoadedDice loadDie1;
   LoadedDice loadDie2;
  

   cout<<"Rolling loaded die1 and loaded die2 "<<COUNT<<" times ."<<endl;
   //Roll the loaded die
   for( int cnt = 1; cnt <= COUNT; cnt++ )
   {
       loadDie1.roll();
       loadDie2.roll();
       sum=loadDie1.getValue()+loadDie2.getValue();
       cout<<"Sum of loaded die1 + die2 : "<<sum<<endl;
   }

   COUNT=100;
   cout<<"Rolling loaded die1 and die2 "<<COUNT<<" times ."<<endl;
  
   //Roll the die
   for( int cnt = 1; cnt <= COUNT; cnt++ )
   {
       loadDie1.roll();
       loadDie2.roll();
       sum=loadDie1.getValue()+loadDie2.getValue();
       cout<<"Sum of loaded die1 + die2 : "<<sum<<endl;
   }

   //pause program output on console
   system("pause");
   return 0;
}