Up: APS105 Home
APS 105 - Computer Fundamentals
Lab #5: Objects
Fall 2000
You must submit this lab by 11:59pm Wednesday, October 25.
Objective
In this lab you will be writing a program that defines and uses objects.
A rational number is one that can be expressed in the form
where both a and b are integers.
Your first task is to write a class Rational that can
be used to perform arithmetic with rational numbers.
Most of the methods in this class will return new rational numbers
as their results.
Your Rational class must contain the methods
below:
public Rational plus(Rational other)
returns the result of adding another rational to this one
public Rational minus(Rational other)
returns the result of subtracting another rational from this one
public Rational times(Rational other)
returns the result of multiplying this rational by another one
public Rational dividedBy(Rational other)
returns the result of dividing this rational by another one
public void reduce()
removes any common factors from the numerator and denominator and
ensures the denominator is positive;
infinities should be reduced to
or
;
this is the only method that will modify the contents of the object
public Rational()
a constructor to initialize the rational value to
public Rational(int numerator, int denominator)
a constructor using the given values for the numerator and denominator
public Rational(Rational r)
a constructor giving an object with values equal to those
in the other object r
public String toString()
returns a string representing a rational in string form,
"3/8", for example.
More specifically:
- 1.
- the string should be in reduced form:
"2/3"
rather than "6/9"
- 2.
- denominators should never be negative:
"-3/4" rather than "3/-4"
- 3.
- rationals whose values are integers should be
returned as such:
"4" rather than "4/1"
- 4.
- since 0/0 is indeterminate, return "NaN" instead
- 5.
- since N/0 is undefined for N not equal to zero,
return "Infinity" or "-Infinity" instead,
depending on the sign of N.
public static Rational getRational(String prompt)
Note that this method is static, so it is a class method.
This method must print the string parameter as a initial prompt.
In the first example below, the parameter would contain the value
"Please enter the first rational number."
which is passed from the main method. You must then prompt
for the numerator and denominator, printing
"numerator: " then "denominator: " before each input.
This method creates and returns a Rational object containing these
numerator and denominator values.
In the arithmetic and constructor methods, you must always keep/create
the object in reduced form.
Write a single main method that uses the Rational class
to perform all of the following tasks (in the same order).
You should place this main method inside a class
named Lab5.
The requirements for the main program are as follows:
- 1.
- Prompt the user for two rational numbers and print their
sum, difference, product, and quotient (in that order, one per line).
- 2.
- Find and print the values of
for
.
Each sum should be printed on a separate line as a fraction
in lowest terms.
- 3.
- Repeat the previous part for the expression
for
.
You should find that writing this main program is relatively
straightforward once you get used to creating objects.
All of the work is done in the Rational class,
so this code should be very easy to follow and easy to write.
For the output, please do not print anything other than what
is required.
Print a single blank line between each part, and label
the three output sections as "Part 1", "Part 2", and "Part 3".
The output for this main program should appear as follows:
Part 1
Please enter the first rational number.
numerator: 9
denominator: 10
Please enter the second rational number.
numerator: 3
denominator: 10
sum is 6/5
difference is 3/5
product is 27/100
quotient is 3
Part 2
For n = 1, sum = 1/2
For n = 2, sum = ...etc...
...etc...
Part 3
For n = 1, sum = 2/3
For n = 2, sum = ...etc...
...etc...
Do not attempt this part unless you have the rest of
your lab working. There are no extra marks for this, it
is merely for practice.
The reduce() method can be written very efficiently.
Try your best to make it run fast.
- 1.
- How does the getRational(String) prompting work?
Your method must take a string parameter and print it, then
prompt for the numerator and denominator. The method description
was updated to help make this more clear.
- 2.
- Why is the plus(Rational) method, for example, not
written as plus(Rational r). That is, why is it missing the
parameter name (the identifier)?
This is a common way to specify an API. The actual identifier
chosen for the parameter is irrelevant because signature
matching uses just the data types.
Of course you must insert a reasonable identifier there yourself.
Occasionally the identifier may be given to you, such as in the
Rational(int,int) constructor. This is done because you need
to know which integer parameter contains the numerator.
- 3.
- What does
evaluate to?
Do I reduce
? What about similar
cases involving zeros, NaNs and infinities?
The first case would evaluate to
.
The second case should probably be reduced to
.
Handle other
cases the same way you would do arithmetic with regular values.
If in doubt, just think it out, the answer will be clear.
There is no need to ask every case on the newsgroup.
Clarification. Always perform the basic cross-multiply rules
and so on for arithmetic operations (even if the denominators are already similar).
This will generate weird results with infinities and NaNs, but we will
only be testing very basic cases.
- 4.
- Do I have to call reduce() in toString()?
Yes, it is always best to be safe and clear in your programs.
Under ideal circumstances, you would not have to because
rational numbers are created in reduced form by the constructors
or the arithmetic operations.
However, to be absolutely sure the fields are reduced,
you should still call it before converting to a string.
The reasons are: 1) the numerator may perchance be modified
without you realizing it, 2) at a later date, you or another
programmer may add methods to this class that put it in non-reduced
form, and 3) the reduce operation can be made very efficient.
- 5.
- Do the reduction/simplification rules listed under toString() actually
apply to the reduce method as well?
Yes they do.
You must submit your Rational.java and Lab5.java programs,
containing the object definition and the main method from Sections 1 and 2.
Section 3 is optional and may be included as well.
computer.ecf% submitaps105f 5 Rational.java Lab5.java Stdin.java
Up: APS105 Home
Guy G. Lemieux
2000-10-24