ECE-C301 Advanced Programming for Engineers
Laboratory Assignment: Week 3
(10 Points) 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().
(10 Points) 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().
(10 Points) Write a polymorphic function called presentQuestion that accepts a Question object (and
its derived classes) as a parameter. This function should display the question, get input from the user using
the build-in raw input, check the answer, and print if the supplied answer is correct or incorrect.
Demonstrate that this class works by creating a heterogeneous list of Question and ChoiceQuestion
objects—your list should have at least 5 questions. Use a for loop to cycle through this list and call
presentQuestion for each item in the list. Show your working “quiz” to the TA.