CS 124, Fall 2011
Lab 10: Using Objects (Math Quiz Redux)

With a test coming up on Monday, it is important that you be able to write and use simple classes. In the previous lab, you wrote a simple MathQuestion class. In this lab, you will use that class in another program to administer math quizzes.

This lab is due at the beginning of lab next Thursday. You should copy your work into your homework folder by that time.

There is only one exercise for this lab. You will need a copy of TextIO.java and a copy of the MathQuestion class that you wrote for Lab 9. You should create a project named lab10, and you should copy those files into the src folder in lab10.


Math Quiz Redux

In the previous lab, Exercise 2, you wrote a class for representing simple math questions. For this exercise, you should write a program that uses that class to administer a math quiz. The quiz will work similarly to this example:

Question 1:  Compute 30 + 21
Please enter your answer: 51
Congratulations!  That is correct.

Question 2:  Compute 26 * 10
Please enter your answer: 260
Congratulations!  That is correct.

Question 3:  Compute 12 * 6
Please enter your answer: 72
Congratulations!  That is correct.

Question 4:  Compute 33 - 28
Please enter your answer: 5
Congratulations!  That is correct.

Question 5:  Compute 140 / 4
Please enter your answer: 32
Sorry, that is not correct.

Question 6:  Compute 87 + 11
Please enter your answer: 98
Congratulations!  That is correct.

Question 7:  Compute 31 * 5
Please enter your answer: 155
Congratulations!  That is correct.

Question 8:  Compute 28 - 5
Please enter your answer: 23
Congratulations!  That is correct.

Question 9:  Compute 16 + 21
Please enter your answer: 37
Congratulations!  That is correct.

Question 10:  Compute 24 * 1
Please enter your answer: 25
Sorry, that is not correct.

You had 8 correct answers out of 10 questions.
Your score on this quiz is 80%

Here are the correct answers to all the question on the quiz:

Question 1.  30 + 21  =  51
Question 2.  26 * 10  =  260
Question 3.  12 * 6  =  72
Question 4.  33 - 28  =  5
Question 5.  140 / 4  =  35
Question 6.  87 + 11  =  98
Question 7.  31 * 5  =  155
Question 8.  28 - 5  =  23
Question 9.  16 + 21  =  37
Question 10.  24 * 1  =  24

Note that each question is used twice, once to ask the question and once at the end, to tell the user the correct answer. This means that you need to save the questions somewhere. For that you are going to need a variable of type ArrayList<MathQuestion>. See below for the information that you need about ArrayList.

The program has three sections: (1) create random MathQuestion objects and add them to the ArrayList; (2) administer the quiz to the user; and (3) print out the questions with the correct answers. It is natural to make the three sections into subroutines, and to have a fairly short main program that uses those subroutines. You can also use the following subroutine for creating random questions. Copy and paste this code into your program. You might have to make minor changes, depending on how you wrote your MathQuestion class:

private static MathQuestion createRandomQuestion() {
   char op;    // The operator for the question.
   int n1, n2; // The operands for the question.
   switch ( (int)(4*Math.random()) ) {
      case 0:
         op = '+';
         n1 = 10 + (int)( 90 * Math.random() );
         n2 = (int)(30*Math.random());
         break;
      case 1:
         op = '-';
         n1 = 10 + (int)( 90 * Math.random() );
         n2 = (int)((n1+1)*Math.random());
         break;
      case 2:
         op = '*';
         n1 = 10 + (int)( 30 * Math.random() );
         n2 = (int)(11*Math.random());
         break;
      default:
         op = '/';
         int ans = 2 + (int)(39*Math.random());
         n2 = 2 + (int)(10*Math.random());
         n1 = ans*n2;
         break;
   }
   MathQuestion question = new MathQuestion(op,n1,n2);
   return question;
}

Some requirements for the program. You must use a MathQuestion class to represent the questions on the quiz. You must use a named constant to represent the number of questions on the quiz, and it should be possible to change the number of questions simply by changing the value of that constant. You should use subroutines. The ArrayList that holds the math questions can be a global variable in your program. You should, of course, follow all style rules, including commenting.

It would be nice, in the third section of the program, to list the user's answer to each problem in addition to the correct answer. To do this, you can store the user's answers in a second ArrayList. The second ArrayList should be of type ArrayList<Integer>. Note that the type is ArrayList<Integer>, not ArrayList<int>. There is no such thing as ArrayList<int>, since ArrayLists only work with objects. However, an ArrayList<Integer> can be used with values of type int.


About ArrayLists

The ArrayList class is defined in the package java.util. To use ArrayLists in your program, you should say import java.util.ArrayList; at the top of your source code file.

ArrayList is an example of a "parameterized type." Although we have not encountered such types before, the idea is not too hard to understand. A given ArrayList is meant to hold objects of a certain type. The type of object that the ArrayList holds is used as part of the name for the ArrayList type; it is a "type parameter." An ArrayList that holds objects of type Foo is declared to be of type ArrayList<Foo>. This type name can be used to declare variables, it can be the return type of a method, and it can specify the type of a dummy parameter. For example, to declare a global variable, quiz, and make it refer to an ArrayList of MathQuestion objects, you could use these statement:

ArrayList<MathQuestion> quiz;
quiz = new ArrayList<MathQuestion>();

If list is an ArrayList, you can add an object, obj, to the end of the list by saying list.add(obj). The number of items currently in the list is given by list.size(). The size changes as items are added and removed. You can get the i-th item from the list with the function call list.get(i). Items are numbered from 0 to list.size() − 1.

ArrayLists are often processed using for loops. The basic for loop for going through an ArrayList<Foo> generally looks like this:

for ( int i = 0;  i < list.size();  i++ ) {
   Foo  item = list.get(i);  // Get item number i from the list.
     .
     .  // Do something with item.
     .
}