next up previous
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.

1. Creating a Class for Rational Numbers

A rational number is one that can be expressed in the form $a\over b$ 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:

In the arithmetic and constructor methods, you must always keep/create the object in reduced form.

2. Using the Rational class

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

\begin{displaymath}\sum_{i=1}^n{\left({1\over i} \times {1\over{i+1}}\right)} \end{displaymath}

for $n = 1, 2, 3, \ldots, 10$.

Each sum should be printed on a separate line as a fraction in lowest terms.

3.
Repeat the previous part for the expression

\begin{displaymath}\sum_{i=1}^n{\left({1\over {2i-1}} - {1\over{2i+1}}\right)}\end{displaymath}

for $n = 1, 2, 3, \ldots, 10$.

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...

3. Optional Part for Advanced Programmers

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.

4. FAQ

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 ${1 \over 0} - {1 \over 0}$ evaluate to? Do I reduce $0 \over 24$? What about similar cases involving zeros, NaNs and infinities?
The first case would evaluate to $0\over 0$. The second case should probably be reduced to $0\over1$. 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.

5. What to Submit

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


next up previous
Up: APS105 Home
Guy G. Lemieux
2000-10-24