Rational number assignment

Objectives

  • Create a class that implements an API specification.
  • Create a class that implements an interface.
  • Write overloaded methods/constructors.
  • Create and use an enumeration.
  • Create and use public and private variables (fields).
  • Create and use static and instance variables (fields).
  • Create and use public and private methods.
  • Create and use static and instance methods.
  • Test and debug a complex application.

Overview

The objective of this assignment is to write a class that handles rational numbers and conforms to an API contract supplied by a generic class. Rational numbers are numbers which can be represented as fractions which have an integer numerator and denominator, where the denominator is not allowed to be 0. The generic, abstract base class and a test class (which is unfortunately not thorough) is provided.

Resources

Requirements

  • (2) Include documentation comments
  • There is a starting document available for the rational class which includes some of the steps listed below. Start with the starting document.
  • (0) Name your class: Rational [already done]
  • (0) Declare that the class implements both the ExtendedNumber<Rational> and Comparable<Rational> interfaces [already done]
  • (3) Define a public enum named DisplayStyle with the following two values: FRACTIONAL, FLOATING_POINT
  • (1) Declare two private instance variables of type int. They should be used to store the numerator and denominator. Name them num and den.
  • (2) Declare a private static variable named displayStyle (of type DisplayStyle) to store one of the enum values. It should be initialized to the value: FRACTIONAL.
  • Create three public constructors:
    • (2) A default constructor which sets the numerator and denominator to 1
    • (2) A copy constructor
    • (2) A constructor that accepts int values for both the numerator and denominator
    • (2) Note: The constructors must do all error checking and simplification described for the setNums(int, int) and simplify() methods - so call them from the constructors. Better still, have the two argument constructor do all the work, and have the other two constructors call it.
  • (1) Write a public instance accessor method named getNumerator which returns the numerator value.
  • (1) Write a public instance accessor method named getDenominator which returns the denominator value.
  • (0) Write a public static accessor method named getDisplayStyle which returns the displayStyle value. [already done]
  • Write a public instance method with the signature: void setNums(int, int). The two parameters are the requested numerator and denominator. Do the following:
    • (2) If the denominator passed in is 0, throw an IllegalArgumentException with the message, "Invalid rational number: n/d", where the n and d are replaced with the requested numerator and denominator.
    • (1) If the numerator passed in is zero, set the denominator passed in to one.
    • (1) If the denominator passed in is negative, change the sign of both the passed in numerator and passed in denominator.
    • (1) Set the object's numerator and denominator from the (possibly modified) passed in values.
    • (1) Call the simplify method to reduce the rational number to its simplest form.
  • (3) Write a private instance method named simplify(). Have it get the greatest common divisor of the numerator and denominator and then divide the numerator and denominator by that greatest common divisor.
  • (0) Include a static method with the signature: int gcd(int, int). It should return the greatest common divisor of its two parameters. [already done]
  • (2) Write a public static method with the signature: void setDisplayStyle(DisplayStyle). This method should set the static displayStyle variable.
  • (12) Write public instance methods named add, subtract, multiply and divide. All three take a Rational object as an argument and return a new Rational object. Don't forget that the results must be simplified (which should be automatic since you are creating a new Rational object and teh constructors take care of that).
  • Assuming the current numerator and denominator are stored in fields named num and den, and the parameter (for the Rational object being passed in) is r Method
    Result numeratorResult denominator
    add(r)num*r.den + r.num*denden*r.den
    subtract(r)num*r.den - r.num*denden*r.den
    multiply(r)num*r.numden*r.den
    divide(r)num*r.denr.num*den
  • Note: We have ignored what happens with division when the second Rational number is 0. In a full implementation, we would expect to throw a runtime exception.
  • (2) Write a public instance increment method. This method just has to increase the Rational number by 1, which means adding the value of the denominator to the numerator. Return the current object.
  • (2) Write a public instance decrement method. This method just has to decrease the Rational number by 1, which means subtracting the value of the denominator from the numerator. Return the current object.
  • Write overrides for the following public instance methods:
    • (0) int compareTo(Rational): return 0 if the argument has the same value as the current Rational, an integer less than 0 if the current object is less than the argument, and an integer greater than 0 if the current object is greater than the argument. [already done]
    • (0) boolean equals(Object) [already done]
    • (0) int hashCode() [already done]
    • (5) String toString(): Stringify the Rational number as either a fraction or a floating-point number depending on the value of the static variable holding the DisplayStyle value. For example if the numerator is 1 and the denominator is 2, this method would return "1/2" if displayStyle is set to FRACTIONAL, and "0.5" if displayStyle is set to FLOATING_POINT.

GCD method (recursive)

Sample output

Sample run 1: 0 errors found by test program

Sample run 2: multiply(Rational) not working properly: 1/2 * 2/3 => 1/2 1 error found by test program