CS 124, Spring 2017
Lab 9: Designing and Writing Classes

For this lab, you will design and write two classes, and you will write programs that use those classes. The first exercise requires you to solve a specific problem that is similar to the "AdditionQuiz" programs that you have worked on previously. For the second exercise, you have to come up with your own idea for a class.

Your work for this lab will be written completely from scratch. There are no existing files for you to copy into your project. You will submit four files. The two files for the first exercise must be named ArithmeticQuiz.java and ArithmeticProblem.java. The names of the files for the second exercise are up to you. Submit your files, as usual, using the web site http://math.hws.edu/submit124. Note that the web site will not try to compile the files that you submit for the second exercise. Your work for this lab is due by the beginning of the lab next Thursday.

Arithmetic Quiz

You worked on AdditionQuiz programs in Lab 3 and in Lab 5. The first exercise for this lab asks you to write a program with similar functionality, except that the new program can have subtraction, multiplication, and division problems as well as addition problems. But the main point of the exercise is that you will write and use a class that represents such problems.

To read input from the user for this exercise, you should use the Scanner class, not TextIO. See below for more information about this.

You should write a class named ArithmeticProblem, defined in a file named ArithmeticProblem.java. An object of type ArithmeticProblem represents one simple integer arithmetic problem. The problem can be either an addition, a subtraction, a multiplication, or a division. The object must include instance variables to record the first operand of the problem, the second operand of the problem, and the operation. The operands are integers. The operator can be represented in various ways, but it's probably easiest to use a char whose value is one of '+', '-', '*', or '/'. Other than that, the design of the class is up to you. However, you class should follow rules of good style for class design (private instance variables with getters and setters as appropriate, constructors for creating the objects, other methods to represent useful operations on the objects, maybe a toString() method, and of course JavaDoc comments).

You should also write a main program that administers a quiz consisting of randomly generated arithmetic problems. Each problem should be represented by an object of type ArithmeticProblem. The main program itself should be in a separate file named ArithmeticQuiz.java. The exact structure of the quiz is up to you, but it should be pleasant and easy to use, it should make it clear to the user what is going on at all times, and it should tell the user their score on the quiz. The quiz should be appropriate for young students who are just learning arithmetic, so don't make the problems too hard.

You should decide where to generate the random problems, either in the main program or in an ArithmeticProblem constructor. There are two specific requirements for the problems: In a subtraction problem, the second operand must be less than or equal to the first operand, so that the answer is not negative. And in a division problem, the second operand must evenly divide the first, so that the answer is a whole number. The easiest way to meet the second requirement is to choose the second operand and the answer at random, and then multiply those two values to get the first operand.

Using Scanner for Input

We have been using TextIO to get input from the user, but TextIO is not a standard part of Java. Yesterday in class, we looked at the standard class Scanner, which can be used for textual input not just from the user but from almost any source. For this lab, you should use Scanner for input. Scanner is introduced in the textbook in Section 2.4.6.

Since Scanner is defined in the package java.util, you should import it into any file that uses it. This can be done with the import directive

import java.util.Scanner;

To use a scanner to read input from the user, you need to "wrap" the standard input stream, System.in, in a Scanner object. For example:

Scanner in = new Scanner(System.in);

Do this just once, and then use the same Scanner object throughout your program.

If you have a Scanner named in, you can use the function in.nextInt() to read an integer. This function is much like TextIO.getInt(). However, in.nextInt() will throw an exception if the next item in input is not in fact an integer. Unfortunately, it is no so easy to deal with that issue, and you can ignore the problem for this assignment.

A Class of Your Own

The second exercise for this lab is to write a class to represent something, and write a main program to test that class. The main program should be in a separate file from the class; it should test the constructors and methods from the class, but it doesn't have to do anything practical. You should, of course, follow all the rules of good style.

You can select any concrete or abstract entity to represent, as long as its not one of the examples we have looked at already (PairOfDice, PianoKey, Card, Deck, Hand, ArithmeticProblem). Hopefully, though, it will be something that could be useful in some sort of program. You should come up with your own idea, not copy someone else's. At a minimum, your class should have a constructor and three methods. (Note that "at a minimum" does not mean "for full credit.")