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
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 ChoiceQuestiononly 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 choiceseach choice should be numbered (i.e. 1, 2, 3, etc). The overridden method should call the superclasss display method to print the question. Also override the checkAnswer method to accept the number of the correct answerthe overridden method should call the superclasss 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().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started