CPSC 124, Fall 2005
Lab 8: Writing a Class

In this lab, you will write a small, complete class that can be used for creating objects. The class must work with two different main programs that have already been written. The class will be named QuizQuestion, and an object created from the class will represent a question on a math quiz.

To begin the lab, you should start a new project in Eclipse and import the three files MathQuizCL.java, MathQuizGUI.java, and TextIO.java into your project. These can be found in the directory /classes/f05/cs124/lab8.

MathQuizCL and MathQuizGUI are the two main programs that will use your class. When you import them into your project, they will contain errors, since they need the QuizQuestion class that you have not yet written. The two programs are quite different. MathQuizCL is a command-line program that always administers the same 10-question quiz; the specific questions that it asks are built into the program. MathQuizGUI is a GUI program that opens a window on the screen. The window contains a message display where the questions and other information is displayed to the user. It also contains a text-input box where the user types his answer and a button that the user clicks to submit the answer. The questions are randomly generated, and the program will continue to ask questions until the window is closed.

Completed versions of the programs can be found in the directory /classes/f05/cs124 in the .jar files MathQuizCL.jar and MathQuizGUI.jar. You can run the command-line version by changing into that directory and typing the command

               java -jar MathQuizCL.jar

on the command line. You have to run this program on the command line since it uses standard console IO. You can execute MathQuizGUI.jar with a similar command or (if you have set up your Linux environment correctly) by clicking the .jar file.

You do not need to understand the main programs in order to complete this lab, but I encourage you to take a look at them. The MathQuizGUI program is an "event-driven" program that is quite a bit different from the type of straight-through command-line program that you have been writing up to now. You won't understand everything in it at this point, but you might want to try to figure out how it works in general.


Writing a Class

To begin the actual work of the lab, you should create a new class named QuizQuestion. A QuizQuestion object represents a simple integer addition, subtraction, multiplication, or division problem. The problem has an operator (which will be one of the characters '+', '-', '*', and '/') and two integers that are combined by the operator. Your class will need three instance variables to represent these data. You should declare the instance variables to be private.

The public interface of the class consists of two constructors and two instance methods. You must write these members using the names and parameter lists that are expected by the main programs. To be specific, you need to define the following in the class:

  1. a constructor with no parameters. This constructor creates a random question by selecting an operator and two integers at random
  2. a constructor with three parameters, an int, a char, and an int in that order. This constructor creates the question specified by the actual parameters in the constructor call. For example, the constructor call "new QuizQuestion(25,'+',37)" creates an object that represents the problem "25 + 37". This constructor doesn't have to anything more than copy the parameter values to the instance variables of the object.
  3. a method named getQuestion that has a return type of String and no parameters. This method returns a short string that asks the question represented by the object. My version of the class returns a string such as: Compute: 25 + 37
  4. a method named getAnswer that has a return type of int and no parameters that returns the correct answer to the question.

As you write the constructors and methods, the errors in the main program should go away, and when you have written all four, you should be able to run both main programs.

Exercise: You should write the QuizQuestion class, as described above, and make sure that it works correctly with both main programs. You should also add a Javadoc comment to the class as a whole and to each constructor and method in the class.

It should not take you too long to complete this exercise. Hopefully, you will be able to finish it before the end of the lab. However, it is not due until next week in lab. But you should be spending most of your programming effort over the next week on the second programming assignment. If you finish the exercise before the end of the lab, you can work on the programming assignment.


David J. Eck, October 2005