Class Fraction

java.lang.Object
  extended by Fraction

public class Fraction
extends java.lang.Object

A class to represent fractions. Fractions will always be in reduced form.


Constructor Summary
Fraction()
          Default constructor - sets the numerator to 0 and the denominator to 1.
Fraction(int numerator)
          Constructor - Constructs a fraction with the specified numerator and a denominator of 1.
Fraction(int numerator, int denominator)
          Constructor - Constructs a fraction with the specified numerator and denominator (the fraction will be reduced, if possible).
 
Method Summary
 Fraction add(Fraction other)
          Command method that adds this fraction to the other fraction and returns the result as a third new fraction.
 Fraction div(Fraction other)
          Command method that divides this fraction by the other fraction and returns the result as a third new fraction.
 boolean equals(Fraction other)
          Query method to determine if one fraction is equal to another.
 int getDenominator()
          Query method to get the denominator of the fraction
 int getNumerator()
          Query method to get the numerator of the fraction
 Fraction mul(Fraction other)
          Command method that multiplies this fraction to the other fraction and returns the result as a third new fraction.
 Fraction sub(Fraction other)
          Command method that subracts the other fraction from this fraction and returns the result as a third new fraction.
 java.lang.String toString()
          Query method to obtain a string-representation of the fraction.
 double value()
          Query/Command method to get the value of the fraction as a number.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Fraction

public Fraction()
Default constructor - sets the numerator to 0 and the denominator to 1.


Fraction

public Fraction(int numerator)
Constructor - Constructs a fraction with the specified numerator and a denominator of 1.

Parameters:
numerator - The numerator of the fraction.

Fraction

public Fraction(int numerator,
                int denominator)
Constructor - Constructs a fraction with the specified numerator and denominator (the fraction will be reduced, if possible).

Parameters:
numerator - The numerator of the fraction.
denominator - The denominator of the fraction.
Method Detail

getNumerator

public int getNumerator()
Query method to get the numerator of the fraction

Returns:
The numerator of the fraction.

getDenominator

public int getDenominator()
Query method to get the denominator of the fraction

Returns:
The denominator of the fraction.

value

public double value()
Query/Command method to get the value of the fraction as a number.

Returns:
The value of the fraction as a number.

toString

public java.lang.String toString()
Query method to obtain a string-representation of the fraction.

Overrides:
toString in class java.lang.Object
Returns:
The fraction as a string, for example "1/2" or "3/4". If the fraction is a whole number, say 4/1, then "4" is returned.

equals

public boolean equals(Fraction other)
Query method to determine if one fraction is equal to another.

Parameters:
other - The fraction to compare to this fraction.
Returns:
Returns true if equal, otherwise false.

add

public Fraction add(Fraction other)
Command method that adds this fraction to the other fraction and returns the result as a third new fraction.

Parameters:
other - The fraction to add to this fraction.
Returns:
A fraction representing the sum of this and the other fraction.

sub

public Fraction sub(Fraction other)
Command method that subracts the other fraction from this fraction and returns the result as a third new fraction.

Parameters:
other - The fraction to subtract from this fraction.
Returns:
A fraction representing the difference beteen this and the other fraction.

mul

public Fraction mul(Fraction other)
Command method that multiplies this fraction to the other fraction and returns the result as a third new fraction.

Parameters:
other - The fraction to multiply with this fraction.
Returns:
A fraction representing the product of this and the other fraction.

div

public Fraction div(Fraction other)
Command method that divides this fraction by the other fraction and returns the result as a third new fraction.

Parameters:
other - The fraction to divide this fraction by.
Returns:
A fraction representing the division of this by the other fraction.