Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 2.7 Part 1 - Implement a base class called Question . The class should ha

ID: 3820754 • Letter: P

Question

Python 2.7

Part 1 - Implement a base class called Question. The class should have attributes to store a question string and an answer string. The class should also have mutator methods setText, which sets the question text, and setAnswer, which sets the answer to the question. Additionally, supply a display method for printing the question to the screen and a checkAnswer method, which takes a string as an argument, for checking if a supplied answer is correct. Demonstrate that this class works by creating a Question instance, setting its text and answer, calling display() for the instance, and using checkAnswer().

class Question(object):
def __init__(self):
self._question = None
self._answer = None

def setText(self, q):
self._question = q

def setAnswer(self, a):
self._answer = a

def display(self):
return "The question is '%s'." % self._question

def checkAnswer(self, reply):
self._reply = reply
if self._answer == self._reply:
return "Correct"
else:
return "Incorrect"

Part 2 - Using code from part 1 complete part 2

Implement a derived class called ChoiceQuestion that inherits from Question. ChoiceQuestion implements a multiple choice question, and should encapsulate a list of strings. Each string in the list is one of the answer choices to the ChoiceQuestion—only one should be the right answer. Add a method called addChoice, which takes two parameters: (1) the choice text and (2) a boolean indicating if the choice is the correct answer. Override the display method to print both the question and the answer choices—each choice should be numbered (i.e. 1, 2, 3, etc). The overridden method should call the superclass’s display method to print the question. Also override the checkAnswer method to accept the number of the correct answer—the overridden method should call the superclass’s checkAnswer method to check if the supplied solution is correct. Demonstrate that this class works by creating a ChoiceQuestion instance, setting its text and answer, calling display() for the instance, and using checkAnswer().

Explanation / Answer

# pastebin link for code: https://pastebin.com/aPeiaWx6

class Question(object):
def __init__(self):
self._question = None
self._answer = None
def setText(self, q):
self._question = q
def setAnswer(self, a):
self._answer = a
def display(self):
return "The question is '%s'." % self._question
def checkAnswer(self, reply):
self._reply = reply
if self._answer == self._reply:
return "Correct"
else:
return "Incorrect"

class ChoiceQuestion (Question):
def __init__(self):
super(ChoiceQuestion, self).__init__()
self._answerList = []
self._correct = ""
def addChoice(self, choiceText, isCorrect):
self._answerList.append(choiceText)
if isCorrect:
self._answer = choiceText
def display(self):
question = super(ChoiceQuestion, self).display() + " "
for i in range(0, len(self._answerList)):
question += str(i+1) + " " + self._answerList[i] + " "
return question
  
def checkAnswer(self, answerIndex):
reply = self._answerList[answerIndex-1]
return super(ChoiceQuestion, self).checkAnswer(reply)

def main():
choiceQuestion = ChoiceQuestion()
choiceQuestion.setText("Sample question?")
choiceQuestion.addChoice("Choice 1", False)
choiceQuestion.addChoice("Choice 2", False)
choiceQuestion.addChoice("Choice 3", True)
choiceQuestion.addChoice("Choice 4", False)
print(choiceQuestion.display())
print("Choice 2: " + choiceQuestion.checkAnswer(2))
print("Choice 3: " + choiceQuestion.checkAnswer(3))

if __name__ == '__main__':
main()
  

Please rate positively if this solved your question.