In this lab, you will work with a quiz application that asks a sequence of questions of the user. The interface is shown here:

The application displays problems in a text area (on the top) and messages (on the bottom right); the user types answers in a text box (on the bottom left) and presses enter.

Three classes work together to create the application:

Exercise 13.1

Create a new package for this lab called lab13 and copy the starting code files into this package:

Familiarize yourself with the quiz mechanism by doing the following:

Inheritance

Right now our quiz mechanism can only ask short answer problems, but this is a bit too limited for our purposes. We'd like to add fill-in-the-blank problems, true-false problems, and maybe even multiple choice problems. As we start planning, we realize that we will be duplicating code if we write each kind of problem from scratch. Instead, let's start with a Problem class that will be the parent class of all the different kinds of problems. This class will collect all of the attributes and methods that are shared between all problems.

So what is shared between all problems? Each problem has some text, but asks the question in a different way (e.g. short-answer just added a question mark, but a fill-in-the-blank problem should add both the question mark and an indication to "Fill in the blank."). Further, all problems have answers, but a true-false problem has a boolean answer instead of a string. Considering these properties, we proceed as follows:

Exercise 13.2

Refactor your code to include a Problem class and a ShortAnswer class that inherits from Problem.

The new Problem class:

The updated ShortAnswer class:

If all has gone well, your controller should run just as it did before, and all of the ShortAnswer tests should still pass.

Python does not require that the question class definitions be placed in separate files, but it is common practice to separate more complex classes into separate files. These question classes are probably simple enough that they can be kept together, but if you choose this option, you should rename the file to “problem” to more accurately indicate what it contains.

Polymorphism

We are now ready to add more types of problems, such as true-false problems and fill-in-the-blank problems. Examples of how your application should present these questions are shown below:

The new problem classes are very similar to a ShortAnswer problem, but they each have differences:

Because all problems are using the same method names, the quiz will be able to create a list of Problems, and ask each problem to ask its question, check its answer, and tell us the correct answer.

Exercise 13.3

Do the following (if you haven't already):

Your quiz application should now operate as it did before, with randomly ordered questions of all three subtypes.

Checking In

Submit the final version of all of your files. We will grade this exercise according to the following criteria:

If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!