In: Computer Science
Using C#
Create the “TestQuestion” class. It will have two class variables: 1) a question and 2) the answer to that question. Please create an accessor and mutator method for both of those variables, without which we would not be able to see or change the question or the answer.
There should be a constructor method. We will also have a “ToString” or “__str__” method, which will print the question followed by its answer.
The constructor method has two parameters to allow the user to enter a test question and answer, or if no input/parameters are given then a default question and answer should be supplied.
The last method of the class, CheckAnswer, should take in a string as a parameter and compare it to the answer. If the input was incorrect, it should return false. Otherwise, it should return true and then the main should tell the user they were correct and have them input a new test question and answer.
TestQuestion.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Simple_qusestion_class
{
class TestQuestion
{
private string question, answer;
public TestQuestion(string ques, string ans)
{
this.question = ques;
this.answer = ans;
}
public string Question
{
get
{
return question;
}
set
{
question = value;
}
}
public string Answer
{
get
{
return answer;
}
set
{
answer = value;
}
}
public Boolean CheckAnswer(string ans)
{
if (answer == ans)
return true;
else
return false;
}
public override string ToString()
{
return "Question : " + question + "\nAnswer :" + answer;
}
}
}
Program.cs with main method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Simple_qusestion_class
{
class TestQuestion
{
private string question, answer;
public TestQuestion(string ques, string ans)
{
this.question = ques;
this.answer = ans;
}
public string Question
{
get
{
return question;
}
set
{
question = value;
}
}
public string Answer
{
get
{
return answer;
}
set
{
answer = value;
}
}
public Boolean CheckAnswer(string ans)
{
if (answer == ans)
return true;
else
return false;
}
public override string ToString()
{
return "Question : " + question + "\nAnswer :" + answer;
}
}
}
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.