In: Computer Science
Part I – Understand a Given Class (die class) (40 pts)
• Download the die class (attached as usingDieClass.cpp), save it as LabUseDieClassFirstName1_FirstName2.cpp
• Add proper opening comments at the beginning of the program. The comments must include the description of all three parts of this lab. You may want to modify the comments after all parts are done to be sure that it is done properly.
• Run the program and answer the following questions:
o How many objects are created in this program (in main function)?
o What is the name of each object?
o What is the face value of a die object on creation?
• Copy and paste these Q&A to the end of opening comment section
Part II – Use the die class (60 pts)
• Add a section of code to the end of main() function by write the following two cout statements: int main() {
// original code are omitted here
// … cout << “*** Beginning of Part II ***” << endl;
// part II code goes here cout << “*** Ending of Part II ***” << endl;
return 0; }
• (20 pts) Roll die1 and die2 for 2000 times and compute the times that two die has the same values (i.e. Both are 1, or both are 2, etc.).
Print this count with a proper label.
• (20 pts) Roll die1 and die2 as many times as needed, until the first pair of 6 occurs.
Print the count needed to get this result.
Do not forget label your output.
Print the values of two die and the count on each iteration before your conclusion.
• (20 pts) Roll die1 and die2 for 3000 times, and compute the times when the sum of two dice is an even number (the sum is 2, 4, 6, 8, …. or 12). Print this count out with a proper label.
Part III – Writing mystery class (Optional for extra 20 points)
• Start a new program, save it as LabExtraWriteMysteryFirstName.cpp
• Write the definition of a class that has the following properties:
o The name of the class is “mystery”
o The class mystery has the two integer variables, num1 and num2.
o The class mystery has the following member functions. set – void function to set the values of num1 and num2; this function has 2 int parameters. print – void function to output the values of num1 and num2. compute – value-return function with one integer parameter (say x) to return a value as follow:
if x > 0, return (num1 + num2) / 2
otherwise, return num1 – num2 + x
equal – value-return function with no parameter and returns true, if num1 equals num2. Otherwise return false.
mystery – default constructor which sets num1 and num2 to 0
mystery – constructor with 2 parameters to set values to num1 and num2 respectively
• Write the definitions of all member functions of mystery class
• Write a program with main() to test the mystery class. This part you have the freedom to create one or more mystery object(s) and invoke any public functions as your desire. But label your output so that other people can understand your output. You driver must test the mystery class thoroughly with at least 5 different seniors including an array of mystery objects. This is a good time to show your creative thinking skills.
The code in the file must include your name, the course, the file name and the program description in comments. Example
// File Name: C:\temp\ArrayRange_Lucinda_Jackson.cpp
// Author: Lucinda A. Jackson, Linda Smith
// Course: CSC-260L-01 or CSC-260L-02
// Program Description: … *** Be sure your description is very // detailed and codes are indented correctly ***
// **** questions and answers go here ****
Hand In:
a) Copy the 3 questions from part I with your answers to the end of the opening comment section with proper labels in the die class.
b) All member names must be in the cpp file. If your name is omitted in the source file, you will get 0 for the program. It simply shows that you are not involved. So each member should check the source file before your group submission is done.
c) Write in the “Written submission” box, the contribution of each of the group member. Fail to do so, the one who submit the work will get 0 for this lab.
d) Copy and paste the output to the end of the program within a pair of /* and */ (as comments) Save the finished program with all required comments as LabUseDieClassFirstName1_FirstName2.cpp
e) Upload only the cpp file to the Blackboard. Only one submission in each group.
f) If you can finish part III, then upload the file form part III as well for getting extra points.\
This is the code
*******************************Starting code, but need to be finish:*****************************************************************
//The user program that uses the class die
#include
#include
#include
using namespace std;
class die { public: die();
//Default constructor
//Sets the default number rolled by a die to 1 void roll();
//Function to roll a die.
//This function uses a random number generator to randomly
//generate a number between 1 and 6, and stores the number
//in the instance variable num. int getNum() const;
//Function to return the number on the top face of the die.
//Returns the value of the instance variable num. private: int num; }; int main() { die die1; // create 2 die objects die1 and die2 die die2; // print the values of die1 and die2 cout << "die1: " << die1.getNum() << endl; cout << "die2: " << die2.getNum() << endl; // roll die1 and die2 die1.roll(); die2.roll(); // print the values of die1 and die2 after the rolling cout << "After rolling die1: " << die1.getNum() << endl; cout << "After rolling die2: " << die2.getNum() << endl; cout << "The sum of the numbers rolled" << " by the dice is: " << die1.getNum() + die2.getNum() << endl; // roll die1 and die2 again die1.roll(); die2.roll(); // what is the sum of two dice after this roll cout << "After again rolling, the sum of " << "the numbers rolled is: " << die1.getNum() + die2.getNum() << endl; return 0; }//end main die::die() { // constructor num = 1; srand(time(0)); // put seed down for using rand() later for die rolling } void die::roll() { num = rand() % 6 + 1; // random value ranging 1-6 } int die::getNum() const { return num; }
/solution for part 1
*save file name as
LabUseDieClassFirstName1_FirstName2.cpp*/
#include<bits/stdc++.h>
using namespace std;
class die { public:
die();
//Default constructor
//Sets the default number rolled by a die to 1
void roll();
//Function to roll a die.
//This function uses a random number generator to randomly
//generate a number between 1 and 6, and stores the number
//in the instance variable num.
int getNum() const;
//Function to return the number on the top face of the die.
//Returns the value of the instance variable num.
void check ();
//count object creation created
static int total_obj(void)
{
return count_objects;
}
private:
int num;
static int count_objects;
};
int check(int num1,int num2);
int even(int num1,int num2);
int main()
{
die die1;
// create 2 die objects die1 and die2
die die2;
// print the values of die1 and die2
cout << "die1: " << die1.getNum() << endl; cout << "die2: " << die2.getNum() << endl;
// roll die1 and die2
die1.roll(); die2.roll();
// print the values of die1 and die2 after the rolling
cout << "After rolling die1: " << die1.getNum() << endl;
cout << "After rolling die2: " << die2.getNum() << endl;
cout << "The sum of the numbers rolled" << " by the dice is: ";
cout<< die1.getNum() + die2.getNum() << endl;
// roll die1 and die2 again
die1.roll(); die2.roll();
// what is the sum of two dice after this roll
cout << "After again rolling, the sum of " << "the numbers rolled is: ";
cout<< die1.getNum() + die2.getNum() << endl;
cout<<"Generating data in die 1 and die2 2000 times "<<endl;
int count=0;
for(int i=0;i<2000;i++)
{
die1.roll(); die2.roll();
int num1=die1.getNum();
int num2=die2.getNum();
count=count+check(num1,num2);
}
cout<<" NO of times they have same value is" <<count<<endl;
cout<<"Generating data in die 1 and die2 3000 times "<<endl;
count=0;
for(int i=0;i<3000;i++)
{
die1.roll(); die2.roll();
int num1=die1.getNum();
int num2=die2.getNum();
count=count+even(num1,num2);
}
cout<<" NO of times they have even value is" <<count<<endl;
cout<<"Generating data in until die1 and die2 get both value 6 "<<endl;
die1.roll();
die2.roll();
int num1=die1.getNum();
int num2=die2.getNum();
count=0;
cout << "After rolling : " <<num1<<" " <<num2<< endl;
while(num1!=6 || num2!=6)
{
num1=die1.getNum();
num2=die2.getNum();
die1.roll(); die2.roll();
cout << "After rolling : " <<num1<<" " <<num2<< endl;
count++;
}
cout<<"They are generating both value 6 in "<< count+1<<" Times:"<<endl;
cout<<"No of times object created : "<<die :: total_obj()<<endl;
}
//end main
int die :: count_objects=0;
die::die() {
// constructor
num = 1; srand(time(0));
// put seed down for using rand() later for die rolling
count_objects++;
}
void die::roll() { num = rand() % 6 + 1;
// random value ranging 1-6
}
int die::getNum() const { return num;
}
int check(int num1,int num2)
{
if(num1==num2)
return 1;
else
return
0;
}
int even(int num1,int num2)
{
int res=(num1+num2)%2;
if(res==0)
return 1;
else
return 0;
}
Output1:
Below is the image for part one.
Above image details about the out put for pragram descibe in part1 and part 2
which include rand method for generating random number 1-6 and also printing how many object is created also count number of even sum occur.
Output2.
above image the second image of out put of program in part 1 and part2
Here is the soultion for part3.
/*Program for part 3
save file name as LabExtraWriteMysteryFirstName.cpp
below code is the implimenataion of mystery class
*/
#include<bits/stdc++.h>
using namespace std;
class mystery
{
private:
int num1,num2;// varible declartion
public:
//constructor
mystery(){
num1=0;num2=0;
}
// setter method
void set(int num1,int num2){
this->num1=num1;
this->num2=num2;
}
//method declaration
void display();
int compute(int x);
bool equal();
int getnum1()
{
return num1;
}
int getnum2()
{
return num2;
}
};
//method definition
int mystery:: compute(int x)
{
if(x>0)
{
return (num1+num2)/2;
}
else
{
return (num1-num2+x);
}
}
void mystery::display()
{
cout<<"values of num1 and num2"<<endl;
cout<<num1<<" "<<num2<<endl;
}
bool mystery:: equal()
{
if(num1==num2)
{
return true;
}
else
{
return false;
}
}
//main method
int main()
{ cout<<"Number 1 and number2"<<endl;
int x,y;cin>>x>>y;
mystery obj;
obj.set(x,y);
cout<<"----------------"<<endl;
cout<<"Enter a parameter to be passed"<<endl;
cout<<"Enter positive or negative value"<<endl;
int x1;
cin>>x1;
cout<<"\nAfter Passing parameter compute function return value "<<obj.compute(x1)<<endl;
cout<<"----------------"<<endl;
obj.display();
if(obj.equal())
cout<<"Number is equal"<<endl;
else
cout<<"Number is not equal"<<endl;
cout<<"----------------"<<endl;
cout<<"Enter number of array object"<<endl;
int p; cin>>p;
mystery arr_obj[p];
for(int i=0;i<p;i++)
{
cout<<"Enter object number data (Number1 and Number 2)"<<endl;
cin>>x>>y;
arr_obj[i].set(x,y);
}
//For printing objects value
for(int i=0;i<p;i++)
{
cout<<"First Value :"<<arr_obj[i].getnum1()<<endl;
cout<<"Second Vlaue"<<arr_obj[i].getnum2()<<endl;
cout<<"----------"<<endl;
}
}
Output of part 3.
The above is the out put of program implemented for part 3 ,it is for array obeject.
Output2 for part3
The above image is output for taking parameter values as negative and also for object array and their input values.