foundations
Class Fraction

java.lang.Object
  |
  +--java.lang.Number
        |
        +--foundations.Fraction

public final class Fraction
extends java.lang.Number
implements java.lang.Comparable, java.io.Serializable

See Also:
Serialized Form

Constructor Summary
Fraction()
           
Fraction(long num)
          Create an instance of Fraction with specified numerator.
Fraction(long num, long denom)
          Create an instance of Fraction with specified numerator and denominator.
Fraction(java.lang.String fString)
          Create an instance of Fraction by parsing a String.
 
Method Summary
 Fraction add(Fraction f)
          Return the sum of this object and the parameter.
 int compareTo(java.lang.Object obj)
          Returns -1, 0 or 1 as this number is less than, equal to, or greater than the Fraction parameter.
 long denominator()
          Return the denominator
 Fraction divide(Fraction f)
          return the quotient of this object and the parameter.
 double doubleValue()
          Return a representation of this fraction as type double.
 boolean equals(java.lang.Object obj)
          Returns true iff the parameter is a Fraction and is equal to this object.
 float floatValue()
          Return a representation of this fraction as type float.
 int hashCode()
          Compute a unique hash code for this fraction.
 int intValue()
          Return a representation of this fraction as type int.
 long longValue()
          Return a representation of this fraction as type long.
static void main(java.lang.String[] args)
          Short main function to test elements of Fraction.
 Fraction multiply(Fraction f)
          Return the product of this object and the parameter.
 long numerator()
          Return the numerator.
 void setDenominator(long denom)
          Set the denominator
 void setNumerator(long num)
          Set the numerator.
 Fraction subtract(Fraction f)
          Return this object minus the parameter.
 java.lang.String toString()
          Return a String representation for this fraction.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Fraction

public Fraction()

Fraction

public Fraction(long num,
                long denom)
Create an instance of Fraction with specified numerator and denominator.
Parameters:
num - initializes numerator
denom - initializes denominator

Fraction

public Fraction(long num)
Create an instance of Fraction with specified numerator.
Parameters:
num - initializes numerator; denominator = 1

Fraction

public Fraction(java.lang.String fString)
Create an instance of Fraction by parsing a String.
Parameters:
fString - must be in the form "[-]num/denom", where the [-] is optional and num and denom must be valid strings convertible to type long.
Method Detail

setNumerator

public void setNumerator(long num)
Set the numerator.

setDenominator

public void setDenominator(long denom)
Set the denominator

numerator

public long numerator()
Return the numerator.

denominator

public long denominator()
Return the denominator

add

public Fraction add(Fraction f)
Return the sum of this object and the parameter.
Parameters:
f - is an instance of class Fraction
Returns:
a new Fraction that is the sum of f and this object

subtract

public Fraction subtract(Fraction f)
Return this object minus the parameter.
Parameters:
f - is an instance of class Fraction
Returns:
a new Fraction that is this object minus f

multiply

public Fraction multiply(Fraction f)
Return the product of this object and the parameter.
Parameters:
f - is an instance of class Fraction
Returns:
a new Fraction that is the product of f and this object

divide

public Fraction divide(Fraction f)
return the quotient of this object and the parameter.
Parameters:
f - is an instance of class Fraction
Returns:
a new Fraction that is the quotient of this object divided by f

equals

public boolean equals(java.lang.Object obj)
Returns true iff the parameter is a Fraction and is equal to this object.
Overrides:
equals in class java.lang.Object
Parameters:
obj - must be an instance of class Fraction
Returns:
true iff the receiver and obj have same numerical value.

compareTo

public int compareTo(java.lang.Object obj)
Returns -1, 0 or 1 as this number is less than, equal to, or greater than the Fraction parameter. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) op 0), where op is one of the six comparison operators.
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
f - is an instance of class Fraction

intValue

public int intValue()
Return a representation of this fraction as type int.
Overrides:
intValue in class java.lang.Number
Returns:
the result of numerator / denominator - as a truncated int

longValue

public long longValue()
Return a representation of this fraction as type long.
Overrides:
longValue in class java.lang.Number
Returns:
the result of numerator / denominator - as a truncated long

floatValue

public float floatValue()
Return a representation of this fraction as type float.
Overrides:
floatValue in class java.lang.Number
Returns:
the result of (float)numerator / (float)denominator

doubleValue

public double doubleValue()
Return a representation of this fraction as type double.
Overrides:
doubleValue in class java.lang.Number
Returns:
the result of (double)numerator / (double)denominator

toString

public java.lang.String toString()
Return a String representation for this fraction. Minus sign (if appropriate) is always prepended to numerator
Overrides:
toString in class java.lang.Object
Returns:
a String with value [-]numerator/denominator

hashCode

public int hashCode()
Compute a unique hash code for this fraction.
Overrides:
hashCode in class java.lang.Object
Returns:
a unique value of type int representing this object

main

public static void main(java.lang.String[] args)
Short main function to test elements of Fraction. Output is as follows:

The value of f1 is: 2/3

The value of f2 is: -3/4

The value of f3 is: 5/1

The value of f4 is: -8/9

The value of f5 is: 2/3

The value of f6 is: 0/0

The value of f7 is: -9/8

The sum of f1 + f2 is: -1/12

The difference of f1 - f2 is: 17/12

The product of f1 * f2 is: -1/2

The quotient of f1 / f2 is: -8/9

f1.equals(f5) is: true

f4.compareTo(f2) = -1

The hashcode for f1 is: 1789919232

The intValue of f4 is: 0

The intValue of f7 is: -1

The longValue of f4 is: 0

The floatValue of f4 is: -0.8888889

The doubleValue of f4 is: -0.8888888888888888