Next: Another constructor
Up: Assignment 9: Homemade Objects
Previous: Assignment 9: Homemade Objects
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.
- 1.
- Go to http://rocky.colby.edu/cs151/code/hw9/cs151.java.
It contains a class definition for complex numbers and a main
method that tests the various functions. Print a copy and make
sure you understand how it works. You should use this example as
a template for what follows.
- 2.
- Create a new project called Rational.prj.
- 3.
- In addition to the class named cs151, add a second
class named Rational. Remember that only cs151
should be a public class.
- 4.
- Add two instance variables (integers) to the Rational
class. These will contain the numerator and
denominator of each Rational object.
- 5.
- Write a constructor that takes no arguments and that sets the
two instance variables to zero.
- 6.
- Write a method called printRational that takes
a Rational object as an argument and prints it in some
reasonable format.
- 7.
- Write a main method that creates a new object with
type Rational, sets its instance variables to some values, and prints
the object.
- 8.
- 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: Another constructor
Up: Assignment 9: Homemade Objects
Previous: Assignment 9: Homemade Objects
Allen B. Downey
1999-11-02