Next: Another constructor
Up: Assignment 9: Rational Numbers
Previous: Reading
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 (recall that the top of a fraction is called the
numerator and the bottom is called the denominator).
For this assignment, you are going to write a class definition for an
implementation of rational numbers. You should use the Complex number
example as a template.
- 1.
- Create a new Java project called Rational and make it an
Application.
- 2.
- Create two instance variables, both integers, named num
and den. Be sure to look at the Complex example and copy what
it does.
- 3.
- Write a constructor that takes no arguments and that sets the
two instance variables to zero. Be sure to look at the Complex
example and copy what it does.
- 4.
- Write a toString method that represents your rational numbers
in a reasonable way as a String. Be sure to look at the Complex
example and copy what it does.
- 5.
- Modify the existing main method to create a new object with
type Rational, set its instance variables to some values, and print
the object. Be sure... ok, I'll stop.
SO FAR, THIS ASSIGNMENT SHOULD REQUIRE NO ACTUAL THINKING ON YOUR
PART. IF YOU FIND YOURSELF THINKING, YOU ARE DOING SOMETHING WRONG
AND YOU SHOULD STOP AND GET HELP.
Next: Another constructor
Up: Assignment 9: Rational Numbers
Previous: Reading
Allen B. Downey
4/8/1998