SE1011 -- Lab 7: More Fraction Class

Overview

For this assignment, you will implement correct implementation problems from the previous assignment and add additional functionality. You will also write a program that uses the Fraction class to generate a game.

Assignment

Correct any problems with your implementation of the Fraction class from lab 6 and implement a few additional methods. The the UML class diagram is shown below.

Fraction
  You need to determine the
approriate fields for your class.
+ Fraction()
+ Fraction(numerator: int)
+ Fraction(numerator: int, denominator: int)
+ Fraction(value: String) (new)
+ add(frac: Fraction): Fraction
+ subtract(frac: Fraction): Fraction
+ divide(frac: Fraction): Fraction (new)
- reduce(): void (new)
+ multiply(frac: Fraction): Fraction
+ equals(frac: Fraction): boolean
+ compareTo(frac: Fraction): int (Optional)
+ toString(): String
+ displayAsDecimal(decimalDisplay: boolean): void
- gcd(num: int, den: int): int (new)

In addition to the specifications from lab 6, be sure to note the following:

  • 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.
  • 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.
  • Still 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.
  • reduce should ensure that the object upon which the method is called is in reduced form. Be sure to notice the minus sign in front of this method in the class diagram.
  • gcd is short for "greatest common divisor." This method should use the Euclidean algorithm to calculate the greatest common divisor (see algorithm pseudocode below). Be sure to note that this method is underlined and preceded by a minus sign in the class diagram.
  • Fractions should always be in reduced form.

Pseudocode for Euclidean algorithm for greatest common divisor

int gcd(a, b) {
  while b is not equal to 0 {
    set t equal to b
    set b equal to the remainder of a divided by b
    set a equal to t
  }
  return a
}

Note: a, b, and t are all integers.

Program

You should also write a simple game (in a class named Lab7 that presents the user with a random math problem involving fractions. Both fractions and the math operation (add, subtract, divide, or multiply) should be chosen at random (although you may want to place some limits on the size of the numerator and denominator values). Using a GUI interface, the program should start by presenting the user with a math problem. If the user answers the problem (answer should be in the form of a fraction), the program will indicate whether or not the user answered correctly, and then repeat the process. If the user selects cancel, the program should indicate the number of correct answers versus the number of answers given.

Submission (due at 11pm the Sunday of week 9 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 7" for the assignment, select your Lab7.java file for the report file and select your revised Fraction.java implementation for the support file.

Acknowledgment

This laboratory assignment was developed by Dr. Chris Taylor.

  • © 2001-2009 Dr. Christopher C. Taylor •
  • Office: L-343 •
  • Phone: 277-7339 •
  • npǝ˙ǝosɯ@ɹolʎɐʇ