Using C++ please construct a \"Problem\" class. It is a part of a math quiz prog
ID: 3770558 • Letter: U
Question
Using C++ please construct a "Problem" class. It is a part of a math quiz program for elementary school children.
The "Problem" class should have the following member items:
- operand 1, an integer btwn 0-9
- operand 2, an integer btwn 0-9
-indication of the problem's operation (addition, subtraction, multiplication, or division)
- the user's answer
- the user's answers remainder (for division)
- the correct answer
- the correct answers remainder (for division)
The "Problem" class should have the following member functions:
- a default constructor that initalizes all of the member items.
- a mutator function to set the type of operanation (addition, subtraction, multiplication, division)
- a function to present the math problem, read in the user answer, and store users answer in the problem instance
- a function to check the user's answer for correctness
-an accessor function to get the problem's operation, users answer, users remainder, correct answer, correct answers remainder.
Explanation / Answer
#include <iostream>
using namespace std;
class Problem
{
//member items
private:
int operand1;
int operand2;
char operation;
int user_ans;
int user_rem;
int crct_ans;
int crct_rem;
public:
//constructor
Problem(int first,int second)
{
operand1 = first;
operand2 = second;
user_ans = user_rem = crct_ans = crct_rem = 0;
operation = 'x';
}
//mutator function
void set_operation(char op){operation = op;}
//function to display the problem on screen
void display()
{
if(operation=='x')
{
cout<<"You have not defined the operation. ";
return;
}
cout<<"What is "<<operand1<<operation<<operand2<<" evaluates to? ";
if(operation == '/')
{
cout<<"Quotient->";
cin>>user_ans;
cout<<"Remainder->";
cin>>user_rem;
crct_ans = operand1/operand2;
crct_rem = operand1%operand2;
}else
{
cout<<"Result->";
cin>>user_ans;
if(operation == '+')
crct_ans = operand1 + operand2;
if(operation == '-')
crct_ans = operand1 - operand2;
if(operation == '*')
crct_ans = operand1 * operand2;
}
}
//check for correctioness of the user answer
void correct()
{
if((crct_ans==user_ans)&&(crct_rem==user_rem))
cout<<"Congratulations you got the answer right ";
else
cout<<"Sorry the answer is wrong ";
}
//accessor functions
int get_operand1(){ return operand1; }
int get_operand2(){ return operand2; }
char get_operation(){ return operation; }
int get_user_ans(){ return user_ans; }
int get_user_rem(){ return user_rem; }
int get_crct_ans(){ return crct_ans; }
int get_crct_rem(){ return crct_rem; }
};
int main()
{
Problem obj1(2,3);
obj1.set_operation('+');
obj1.display();
obj1.correct();
Problem obj2(3,2);
obj2.set_operation('/');
obj2.display();
obj2.correct();
Problem obj3(2,3);
obj3.set_operation('-');
obj3.display();
obj3.correct();
Problem obj4(2,3);
obj4.set_operation('*');
obj4.display();
obj4.correct();
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.