next up previous
Next: Another constructor Up: Assignment 10: Homemade Objects Previous: Assignment 10: Homemade Objects

Rational numbers

A rational number is a number that can be represented as the ratio of two integers. For example, 2/3 is a rational number, and you can think of 7 as a rational number with an implicit 1 in the denominator. For this assignment, you are going to write a class definition for rational numbers. You should use the Complex number example as a template.

1.
Create a new project called Rational.

2.
Add two instance variables to store the numerator and denominator of each Rational object.

3.
Write a constructor that takes no arguments and that sets the two instance variables to zero.

4.
Write a method called printRational that takes a Rational object as an argument and prints it in some reasonable format.

5.
Write a main method that creates a new object with type Rational, sets its instance variables to some values, and prints the object.

6.
At this stage, you have a minimal testable (debuggable) program. Debug it. Whenever I start writing a new class, I start with these three steps: instance variables, a simple constructor, and a print method. Then I add methods and test them one at a time.


next up previous
Next: Another constructor Up: Assignment 10: Homemade Objects Previous: Assignment 10: Homemade Objects
Allen B. Downey
1999-04-14