|
Fall 2009
Overview
For this assignment, you will implement the class described by the
UML diagram below. Also, you are responsible for designing and implementing
a test program that ensures that your Fraction
class is implemented correctly.
A man is like a fraction whose numerator is what he is and whose denominator is what he thinks of himself. The larger the denominator, the smaller the fraction.-- Leo Tolstoy
Assignment
Implement the Fraction class shown in the
UML class diagram below.
| Fraction |
You need to determine the
approriate fields for your class.
|
+ Fraction()
+ Fraction(numerator: int)
+ Fraction(numerator: int, denominator: int)
+ Fraction(value: String) (Not required, do it for the challenge)
+ add(frac: Fraction): Fraction
+ subtract(frac: Fraction): Fraction
+ divide(frac: Fraction): Fraction (Not required, do it for fun)
+ multiply(frac: Fraction): Fraction
+ equals(frac: Fraction): boolean
+ compareTo(frac: Fraction): int (Not required, do it to learn something)
+ toString(): String
+ displayAsDecimal(decimalDisplay: boolean): void
|
Be sure to note the following:
- The default constructor which should create an object
with the value 0/1.
- The second constructor should create an object with the
value passed in as the numerator and a denominator of 1.
- The third constructor should create an object with the
first value passed in as the numerator and the second value
passed in as the denominator.
- Optional: The string passed to the fourth constructor
should be of the form -9/20 or
-9 / 20. If the string is not
of this format, an error message should be displayed, and the
object should be given a value of
0/1.
- add should return a reference to a
Fraction object that is the sum of the
object upon which the method was called and the object passed in.
- subtract should return a reference to a
Fraction object that is the
object upon which the method was called less the object passed in.
- Optional: divide should return a
reference to a Fraction object that is the
object upon which the method was called divided by the object
passed in.
- multiply should return a reference to a
Fraction object that is the product of the
object upon which the method was called and the object passed in.
- In the above four methods, the value of the object upon which the method
was called and object passed in should not be modified.
- equals
should return true if the
object upon which the method was called and the object passed in
represent the same value. Otherwise, the method should return
false.
- Optional: compareTo
should return 0 if the
object upon which the method was called and the object passed in
represent the same value. If the object upon which the method was
called represents a smaller value than the object passed in,
-1 should be returned. Otherwise,
1 should be returned.
- toString should return
a string that displays the value of the fraction. The
format of the string should be in one of two forms:
-1/4
(not 1/-4) or
0.25. (see
displayAsDecimal for more
details).
- displayAsDecimal determines
the format of the string returned by
toString. If the method
was most recently called with
true being passed to it,
the format of the string returned by the
toString method should be
of a decimal form (0.25).
Otherwise, the string returned by the
toString method should be
in fractional form.
- The denominator of a Fraction
object must never be zero. If the user attempts this (e.g.,
using a constructor, dividing by zero, etc), an error message
should be displayed, and the object should be given the value
0/1.
Test Program
You should also write a test program that ensures that
your implementation of the Fraction
class is correct. This program is for your benefit. It will not
be graded.
Submission (due at 11pm the day after week 7 lab)
Each student must submit their class with complete functionality. In
addition, each the class and each method should be documented using the
javadoc format discussed in lecture and a comment at the beginning of the
file must include: Course name, Quarter, Assignment
name, Author's name, and date (see
here). Student's must use the
electronic submission form
to submit their code.
Enter your MSOE username for the username, select "Lab 6" for the
assignment, select your Fraction.java
implementation for the report file name and leave the support file name
blank.
Acknowledgment
This laboratory assignment was developed by
Dr. Chris Taylor.
|