• Use if/if-else/switch statements to control program flow in algorithmic solutions
• Translate UML class diagrams into Java code
• Design and implement simple classes
• Design and implement class and object methods
• Test and debug code.

# Details

Create a Fraction class given in the UML diagram below. You should also design a test program that ensures that your Fraction class is implemented correctly.

## Fraction class

You should implement the class shown in the following UML class diagram:

 Fraction You need to determine the approriate fields for your class. + Fraction() + Fraction(numerator: int) + Fraction(numerator: int, denominator: int) + Fraction(value: String) + add(frac: Fraction): Fraction + subtract(frac: Fraction): Fraction + divide(frac: Fraction): Fraction + multiply(frac: Fraction): Fraction + equals(frac: Fraction): boolean + compareTo(frac: Fraction): int + 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.
• 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 Fraction object that is the sum of the calling object and the object passed in.
• subtract should return a Fraction object whose value is the result of the calling object minus the object passed in.
• divide should return a Fraction object whose value is the result of the calling object divided by the object passed in.
• multiply should return a Fraction object whose value is the result of the calling object multiplied by the object passed in.
• In the above four methods, the value of the calling object and object passed in should not be modified.
• equals should return true if the calling object and the object passed in represent the same value. Otherwise, the method should return false.
• compareTo should return 0 if the calling object and the object passed in represent the same value. If the calling object 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 has 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 should 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.

# Lab report (due 11:00pm, the day prior to week 7 lab)

Each student should indicate how much time you spend on this assignment in the FAST system. You are encouraged to log your activity as you work on the project. At a minimum, you should log all of the time spent on this assignment before the due date given above. All time spent on this assignment should be entered into the week 6 column (even if you worked on it in week 7).

Dr. Taylor's students should use this XML template as a starting point for their reports.

Each student should submit a lab report. Your report should include:

• Sample program output for the program.
• Discussion (how you designed your test program, problems you encountered (and how they were overcome), what you learned, what surprised you, etc...)
• Documented source code for your two classes.

As with any report you submit, correct spelling and grammar are required. In addition, your report should be submitted electronically following the Electronic submission guidelines. (You may wish to consult the sample report before submitting your report.) Be sure to keep copies of all your files, in case something gets lost.

If you have any questions, consult your instructor.

# Acknowledgment

This laboratory assignment was developed by Dr. Chris Taylor.

 © 2001-2006 Dr. Christopher C. Taylor Office: CC-36C Phone: 277-7339