Class RationalNumber

java.lang.Object
  extended by RationalNumber

public class RationalNumber
extends java.lang.Object

Class for rational numbers (fractions).

See Also:
Source code

Constructor Summary
RationalNumber(int n, int d)
          Constructs a rational number with specified numerator, denominator.
 
Method Summary
 boolean equals(RationalNumber n)
          Compares two rational numbers for equality.
static boolean isEqual(RationalNumber n1, RationalNumber n2)
          Compares two rational numbers for equality.
static void main(java.lang.String[] args)
          Main method showing example uses of methods.
static RationalNumber multiply(RationalNumber n1, RationalNumber n2)
          Multiplies two rational numbers.
 java.lang.String toString()
          Converts a rational number to printable form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RationalNumber

public RationalNumber(int n,
                      int d)
Constructs a rational number with specified numerator, denominator.

Parameters:
n - numerator
d - denominator
Method Detail

toString

public java.lang.String toString()
Converts a rational number to printable form.

Overrides:
toString in class java.lang.Object
Returns:
printable representation of number ( numerator / denominator).

isEqual

public static boolean isEqual(RationalNumber n1,
                              RationalNumber n2)
Compares two rational numbers for equality. (This is not a very Java-esque function; the instance method equals() below is usually preferred.)

Parameters:
n1 -
n2 -
Returns:
true if n1 and n2 are equal, false if not

equals

public boolean equals(RationalNumber n)
Compares two rational numbers for equality.

Parameters:
n -
Returns:
true if current object and n are equal, false if not

multiply

public static RationalNumber multiply(RationalNumber n1,
                                      RationalNumber n2)
Multiplies two rational numbers. (Again, it would probably be more Java-esque to make this an instance method.)

Parameters:
n1 -
n2 -
Returns:
n1 times n2

main

public static void main(java.lang.String[] args)
Main method showing example uses of methods.

Parameters:
args - command-line arguments, unused