Question

In: Computer Science

Part I – Understand a Given Class (die class) (40 pts) • Download the die class...

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; }

Solutions

Expert Solution

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


Related Solutions

Download the data type class named as MathOperation:(given below) Create the project of part 2 then...
Download the data type class named as MathOperation:(given below) Create the project of part 2 then add class as a data type class and class MathCalculator_yourLastName as a driver class with main() of part 2 a. Draw UML of class MathOperation (See the topic about UML at TIP FOR LAB on eCampus) b. Create the pseudo-code or draw flowchart of the main of a driver class based on the requirement listed below (see the topic about pseudo-code or flowchart at...
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think of the personType as the base class. Lab10.cpp is provided below: #include <iostream> #include <string> using namespace std; // Base class personType class personType { public: void print()const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); string getFirstName()const; string getLastName()const; personType(string first = "", string last = ""); //Constructor //Sets firstName and lastName...
The first part of the question was answered and I was able to understand it, but...
The first part of the question was answered and I was able to understand it, but the second part of it was answered. Can you explain this part to me. Calculate the amount to be recorded for each item in the table below and enter the value in the associated cell. Round all numbers to the nearest whole number. Item Amount Building Leased equipment Land received from Club on Link's books Land received from Link on Club's books QUESTION 22...
In class, I claimed that most people who can understand the details of football can understand...
In class, I claimed that most people who can understand the details of football can understand rocket science. Many people are interested in football, and few people are interested in rocket science. However, very few people interested in football actually have a career as a professional football player. Likewise, few people have careers as rocket scientists. Yet why are so many more people are interested in football than rocket science? I claimed that the reasons concerned the way that football...
I have a homework class, but I don't really understand anything and I have to submit...
I have a homework class, but I don't really understand anything and I have to submit my homework next week. Homework must be written in C ++ program language. Can someone help me please... Working with classes (everything written below is one task): Define a class Date that contains integer variables for day, month, and year. 1.1. Create the necessary methods for the class: set, get, default constructor, constructor with arguments. 1.2. Create a method that calculates the number of...
(40 pts) Consider the von Thunen Model discussed in class. Alternative uses of one acre of...
(40 pts) Consider the von Thunen Model discussed in class. Alternative uses of one acre of land around a city is summarized as follows: Revenue, output sold in the city Nonland cost of production Cost of transportation 1. Milk $1000 $200 $80/mile 2. Wheat $800 $200 $30/mile 3. Potato $600 $200 $10/mile (a) (10 pts) What is the bid-rent curves for milk producers, wheat producers, and potato pro- ducers, respectively? (b) (10 pts) What is the land-use pattern (what is...
Part 1 Understand the Problem and Class Design with UML The client needs a program to...
Part 1 Understand the Problem and Class Design with UML The client needs a program to calculate the age of a tuna fish. The program must store the length of the fish in cm, and the weight of the fish in pounds. To calculate the estimated age of the fish in years multiply the length times the weight then divide by 10000. Design a class based on this word problem. Testing values are 300 cm length, and 1111.12 pounds. Using...
I posted this question earlier but I don't quite understand the development for part b) and...
I posted this question earlier but I don't quite understand the development for part b) and c). I need some details because it's really not obvious for me. Part a) is good. The question is : We start the operation of a reversible Carnot Engine between two reservoir of temperature T1 and T2 where T2 > T1. The colder reservoir is so cold that his temperature doesn't change during the process of the engine. The hotter reservoir is composed of...
Question 23 40 pts (TCO 9) An important part of many federal information security regulations, such...
Question 23 40 pts (TCO 9) An important part of many federal information security regulations, such as the Gramm-Leach-Bliley Act, is to identify and assess threats against information systems. Define and explain the following concepts associated with this process: threats, threat assessment, threat analysis, threat risk, and threat probability.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT