2. Create a class named MathTutor that generates simple math problems. Each inst
ID: 3768417 • Letter: 2
Question
2. Create a class named MathTutor that generates simple math problems. Each instance of this class is a math problem whose instance variables are its two operands (both int), its operation (char), and its result (int). • The constructor with parameters should receive the two operands and the operation, and set the result. E.g., MathTutor(5,8,‘+’) should represent the addition problem 5 + 8, and set the four instance variables as follows: o operand 1 = 5 o operand 2 = 8 o operation = ‘+’ o result = 13 • The default constructor should randomly generate the two operands in the range of 0 to 99, as well as the operation (set up a scheme such as 1 for ‘+’, 2 for ‘-‘, 3 for ‘*’, 4 for ‘/’). • The method solve (int solution) should receive a student’s attempt to solve the problem, and check it against the correct result. If it is correct, it should display “Correct”; if it is incorrect, it should give the student a maximum of three tries to get it right. o You should first write a simple solve that simply receives the attempted solution in parameter solution and checks whether it is right or wrong, displaying an appropriate message. Once you have this working, submit it for a grade and then move on to the next step: You will need a static variable to count up to three attempts. (Note that an attempt equates to a call to the method.) Your code for this method should display “Correct”, “Try again” or “Sorry – you have used up 3 tries. The correct answer is ____”.
Explanation / Answer
<code>
#include<bits/stdc++.h>
using namespace std;
class MathTutor{
private:
int operand1;
int operand2;
int result;
char operation;
public:
MathTutor(int a,int b,char c){
if (c == '+'){
result =a+b;
}else if (c == '-'){
result = a - b;
}else if(c == '*'){
result = a*b;
}else{
result = a/b;
}
operand1 = a;
operand2 = b;
}
MathTutor(){
srand (time(NULL));
operand1 = rand()%100;
operand2 = rand()%100;
char opt[] = {'+','-','*','/'};
operation = opt[rand()%4];
if (operation == '+'){
result = operand1 + operand2;
}else if (operation == '-'){
result = operand1 - operand2;
}else if(operation == '*'){
result = operand1 * operand2;
}else{
result = operand1 / operand2;
}
}
int getResult(){
return result;
}
int getOperand1(){
return operand1;
}
int getOperand2(){
return operand2;
}
char getOperation(){
return operation;
}
};
int main()
{
MathTutor* mathTutor = new MathTutor();
cout <<"Solve expression:"<< mathTutor->getOperand1() << mathTutor->getOperation() << mathTutor->getOperand2()<<endl;
int tries = 0;
int ans;
bool flag = true;
while(tries < 3){
cout<<"Ansewer:";
cin >> ans;
if (ans == mathTutor->getResult()){
cout<<"Correct"<<endl;
flag = false;
break;
}
if (tries < 2){
cout<< "Try Again"<<endl;
}
tries += 1;
}
if (flag){
cout<<"Sorry"<<endl;
}
return 0;
}
</code>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.