Class Fraction

java.lang.Object
  extended by Fraction
All Implemented Interfaces:
java.lang.Cloneable

public class Fraction
extends java.lang.Object
implements java.lang.Cloneable

The Fraction class implements fractions. Each fraction is a pair numerator/denominator of longs in simplified form, i.e. gcd(numerator,denominator) = 1


Constructor Summary
Fraction()
          Default constructor, constructs 0 as fraction 0/1
Fraction(long a)
          Constructs Fraction object from a long
Fraction(long num, long denom)
          Constructor with two long argument num and denom, constructs the fraction num/denom and simplifies it.
 
Method Summary
 void add(Fraction r)
          Adds fraction r to this fraction and simplifies result.
 java.lang.Object clone()
          clones this fraction by implementing Cloneable
 double doubleValue()
          Returns the double value of this fraction.
 boolean equals(Fraction r)
          Checks equality with other fraction r.
 long getDenominator()
          Get the denominator of this fraction
 long getNumerator()
          Get the nominator of this fraction
 void multiply(Fraction r)
          Multiplies this fraction with other fraction r and simplifies the result.
 java.lang.String toString()
          Returns a string representation of this fraction.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Fraction

public Fraction()
Default constructor, constructs 0 as fraction 0/1


Fraction

public Fraction(long a)
Constructs Fraction object from a long

Parameters:
a - yields the fraction a/1

Fraction

public Fraction(long num,
                long denom)
         throws java.lang.ArithmeticException
Constructor with two long argument num and denom, constructs the fraction num/denom and simplifies it.

Parameters:
num - the numerator
denom - the denominator Throws ArithmeticException if denom == 0
Throws:
java.lang.ArithmeticException
Method Detail

toString

public java.lang.String toString()
Returns a string representation of this fraction.

Overrides:
toString in class java.lang.Object
Returns:
fraction in the form "num/denum"

doubleValue

public double doubleValue()
Returns the double value of this fraction.

Returns:
num/denum

equals

public boolean equals(Fraction r)
Checks equality with other fraction r.

Parameters:
r - the fraction to be compared with
Returns:
true if this fraction equals r.

getNumerator

public long getNumerator()
Get the nominator of this fraction

Returns:
the numerator of this fraction

getDenominator

public long getDenominator()
Get the denominator of this fraction

Returns:
the denominator of this fraction

multiply

public void multiply(Fraction r)
Multiplies this fraction with other fraction r and simplifies the result.

Parameters:
r - the fraction to be multiplied with.

add

public void add(Fraction r)
Adds fraction r to this fraction and simplifies result.

Parameters:
r - the fraction to be added.

clone

public java.lang.Object clone()
clones this fraction by implementing Cloneable

Overrides:
clone in class java.lang.Object