edu.cmu.cs.pattis.cs151xx
Class Rational

java.lang.Object
  extended by edu.cmu.cs.pattis.cs151xx.Rational

public class Rational
extends java.lang.Object

Objects constructed from the Rational class act like fractions (rational numbers have a sign, numerator, and denominator). This class implements some standard arithemetic and relational operations (as methods) on values of this class. Object constructed from this class obey the following class invariants

Note that objcets constructed from this class are immutable, so once these invariants are satisfied by the constructor, they always stay true.


Field Summary
static Rational ONE
          Stores the fraction 1/1.
static Rational ZERO
          Stores the fraction 0/1.
 
Constructor Summary
Rational()
          Constructs a Rational object 0/1.
Rational(int numerator)
          Constructs a Rational object, specifying the numerator of the fraction (the denominator is one).
Rational(int numerator, int denominator)
          Constructs a Rational object, specifying the numerator and denominator of the fraction.
Rational(Rational other)
          Constructs a Rational object whose state is the same as other.
 
Method Summary
 Rational abs()
          Returns a new fraction that is the absolute value of this one.
 Rational add(Rational other)
          Returns a new fraction that is the sum of this one and other.
 int compareTo(Rational other)
          Returns the trichotomous comparison of this value and other.
 Rational divide(Rational other)
          Returns a new fraction that is the dividend of this one and other.
 boolean equals(Rational other)
          Returns whether or not this fraction is equal to other.
 int getDenominator()
          Returns the denominator of the fraction.
 int getNumerator()
          Returns the numerator of the fraction.
 Rational multiply(Rational other)
          Returns a new fraction that is the product of this one and other.
 Rational negate()
          Returns a new fraction that is the negated value of this one.
static Rational prompt(java.lang.String message)
          Returns a new fraction after prompting the user for its numerator and denominator.
 Rational reciprocal()
          Returns a new fraction that is the reciprocal value of this one: 1/this.
 Rational subtract(Rational other)
          Returns a new fraction that is the difference of this one and other.
 java.lang.String toDecimalString(int decimalPlaces)
          Returns a String representation of the state of this fraction, as a decimal value approximating the fraction.
 java.lang.String toString()
          Returns a String representation of the state of this fraction: numerator/denominator.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Rational ZERO
Stores the fraction 0/1.


ONE

public static final Rational ONE
Stores the fraction 1/1.

Constructor Detail

Rational

public Rational(int numerator,
                int denominator)
         throws java.lang.IllegalArgumentException
Constructs a Rational object, specifying the numerator and denominator of the fraction. These value are examined and put in a canonical form to ensure the following class invariants

Parameters:
numerator - specifies the numerator of the fraction
denominator - specifies the denominator of the fraction
Throws:
java.lang.IllegalArgumentException - if denominator is 0

Rational

public Rational(int numerator)
Constructs a Rational object, specifying the numerator of the fraction (the denominator is one).

Parameters:
numerator - specifies the numerator of the fraction

Rational

public Rational()
Constructs a Rational object 0/1.


Rational

public Rational(Rational other)
Constructs a Rational object whose state is the same as other.

Parameters:
other - specifies the fraction whose value to store into the state of this object
Method Detail

getNumerator

public int getNumerator()
Returns the numerator of the fraction.

Returns:
the numerator of the fraction

getDenominator

public int getDenominator()
Returns the denominator of the fraction.

Returns:
the denominator of the fraction

equals

public boolean equals(Rational other)
Returns whether or not this fraction is equal to other.

Parameters:
other - specifies the other fraction in the equality check
Returns:
whether or not this fraction is equal to other

compareTo

public int compareTo(Rational other)
Returns the trichotomous comparison of this value and other. More specifically:
  • a negative value if this < other
  • zero if this = other
  • a positive value if this > other
Example use, checking whether f1 is less than or equal to f2:

  if (f1.compareTo(f2) <= 0)

Parameters:
other - specifies the other fraction in the comparison
Returns:
the trichotomous comparison of this value and other

abs

public Rational abs()
Returns a new fraction that is the absolute value of this one.

Returns:
a new fraction that is the absolute value of this one.

negate

public Rational negate()
Returns a new fraction that is the negated value of this one.

Returns:
a new fraction that is the negated value of this one.

reciprocal

public Rational reciprocal()
                    throws java.lang.IllegalStateException
Returns a new fraction that is the reciprocal value of this one: 1/this.

Returns:
a new fraction that is the reciprocal value of this one: 1/this
Throws:
java.lang.IllegalStateException - if this value is 0

add

public Rational add(Rational other)
Returns a new fraction that is the sum of this one and other.

Parameters:
other - specifies the other fraction in the sum
Returns:
a new fraction that is the sum of this one and other

subtract

public Rational subtract(Rational other)
Returns a new fraction that is the difference of this one and other.

Parameters:
other - specifies the other fraction in the difference
Returns:
a new fraction that is the difference of this one and other

multiply

public Rational multiply(Rational other)
Returns a new fraction that is the product of this one and other.

Parameters:
other - specifies the other fraction in the product
Returns:
a new fraction that is the product of this one and other

divide

public Rational divide(Rational other)
                throws java.lang.IllegalArgumentException
Returns a new fraction that is the dividend of this one and other.

Parameters:
other - specifies the other fraction in the division
Returns:
a new fraction that is the dividend of this one and other
Throws:
java.lang.IllegalArgumentException - if other value is 0

toString

public java.lang.String toString()
Returns a String representation of the state of this fraction: numerator/denominator.

Overrides:
toString in class java.lang.Object
Returns:
a String representation of the state of this fraction: numerator/denominator

toDecimalString

public java.lang.String toDecimalString(int decimalPlaces)
Returns a String representation of the state of this fraction, as a decimal value approximating the fraction. For example (new Rational(1,3)).toDecimalString(20) returns ".33333333333333333333"; (new Rational(1,7)).toDecimalString(13) returns ".1428571428571".

Parameters:
decimalPlaces - specifies the number of decimal places to use in the approximation
Returns:
a String representation of the state of this fraction, as a decimal value approximating the fraction

prompt

public static Rational prompt(java.lang.String message)
Returns a new fraction after prompting the user for its numerator and denominator. If an illegal value is entered, the user is reprompted For example, if we write Rational.prompt("Enter height") an interaction on the console screen might look like
  Enter Height
    Enter numerator  : 1
    Enter denominator: 3

Parameters:
message - specifies the message with which to prompt the user
Returns:
a new fraction after prompting the user for its numerator and denominator.