edu.rit.numeric
Class Rational

java.lang.Object
  extended by edu.rit.numeric.Rational
All Implemented Interfaces:
Serializable, Comparable

public class Rational
extends Object
implements Comparable, Serializable

Class Rational provides a 32-bit rational number. A 32-bit rational number is the ratio of two 32-bit integers (type int). Operations are provided for exact arithmetic and comparison with rational numbers.

Class Rational overrides the equals() and hashCode() methods, making it suitable for use as a key in a hashed data structure like a HashMap or HashSet. However, a Rational object is mutable. Take care not to change the value of a Rational object if it is used as a key in a hashed data structure.

Class Rational is not multiple thread safe.

See Also:
Serialized Form

Constructor Summary
Rational()
          Construct a new rational number.
Rational(int value)
          Construct a new rational number.
Rational(int numer, int denom)
          Construct a new rational number.
Rational(Rational value)
          Construct a new rational number.
Rational(String s)
          Construct a new rational number.
 
Method Summary
 Rational abs(int x)
          Set this rational number to the absolute value of the given number.
 Rational abs(Rational x)
          Set this rational number to the absolute value of the given number.
 Rational add(int x, int y)
          Set this rational number to the sum of the given numbers.
 Rational add(int x, Rational y)
          Set this rational number to the sum of the given numbers.
 Rational add(Rational x, int y)
          Set this rational number to the sum of the given numbers.
 Rational add(Rational x, Rational y)
          Set this rational number to the sum of the given numbers.
 Rational assign(int x)
          Set this rational number to the given number.
 Rational assign(Rational x)
          Set this rational number to the given number.
 Rational assign(String s)
          Set this rational number to the value parsed from the given string.
 int compareTo(Object obj)
          Compare this rational number to the given object.
 Rational div(int x, int y)
          Set this rational number to the quotient of the given numbers.
 Rational div(int x, Rational y)
          Set this rational number to the quotient of the given numbers.
 Rational div(Rational x, int y)
          Set this rational number to the quotient of the given numbers.
 Rational div(Rational x, Rational y)
          Set this rational number to the quotient of the given numbers.
 double doubleValue()
          Converts this rational number to a double precision floating point number.
 boolean eq(int x)
          Determine if this rational number is equal to the given number.
 boolean eq(Rational x)
          Determine if this rational number is equal to the given number.
 boolean equals(Object obj)
          Determine if this rational number is equal to the given object.
 boolean eqZero()
          Determine if this rational number is equal to zero.
 float floatValue()
          Converts this rational number to a single precision floating point number.
 Rational fracPart(Rational x)
          Set this rational number to the fractional part of the given rational number.
 boolean ge(int x)
          Determine if this rational number is greater than or equal to the given number.
 boolean ge(Rational x)
          Determine if this rational number is greater than or equal to the given number.
 boolean geZero()
          Determine if this rational number is greater than or equal to zero.
 boolean gt(int x)
          Determine if this rational number is greater than the given number.
 boolean gt(Rational x)
          Determine if this rational number is greater than the given number.
 boolean gtZero()
          Determine if this rational number is greater than zero.
 int hashCode()
          Returns a hash code for this rational number.
 Rational intPart(Rational x)
          Set this rational number to the integer part of the given rational number.
 int intValue()
          Converts this rational number to an integer.
 boolean le(int x)
          Determine if this rational number is less than or equal to the given number.
 boolean le(Rational x)
          Determine if this rational number is less than or equal to the given number.
 boolean leZero()
          Determine if this rational number is less than or equal to zero.
 long longValue()
          Converts this rational number to a long integer.
 boolean lt(int x)
          Determine if this rational number is less than the given number.
 boolean lt(Rational x)
          Determine if this rational number is less than the given number.
 boolean ltZero()
          Determine if this rational number is less than zero.
 Rational max(int x, int y)
          Set this rational number to the maximum of the given numbers.
 Rational max(int x, Rational y)
          Set this rational number to the maximum of the given numbers.
 Rational max(Rational x, int y)
          Set this rational number to the maximum of the given numbers.
 Rational max(Rational x, Rational y)
          Set this rational number to the maximum of the given numbers.
 Rational min(int x, int y)
          Set this rational number to the minimum of the given numbers.
 Rational min(int x, Rational y)
          Set this rational number to the minimum of the given numbers.
 Rational min(Rational x, int y)
          Set this rational number to the minimum of the given numbers.
 Rational min(Rational x, Rational y)
          Set this rational number to the minimum of the given numbers.
 Rational mul(int x, int y)
          Set this rational number to the product of the given numbers.
 Rational mul(int x, Rational y)
          Set this rational number to the product of the given numbers.
 Rational mul(Rational x, int y)
          Set this rational number to the product of the given numbers.
 Rational mul(Rational x, Rational y)
          Set this rational number to the product of the given numbers.
 boolean ne(int x)
          Determine if this rational number is not equal to the given number.
 boolean ne(Rational x)
          Determine if this rational number is not equal to the given number.
 Rational neg(int x)
          Set this rational number to the negative of the given number.
 Rational neg(Rational x)
          Set this rational number to the negative of the given number.
 boolean neZero()
          Determine if this rational number is not equal to zero.
 Rational sub(int x, int y)
          Set this rational number to the difference of the given numbers.
 Rational sub(int x, Rational y)
          Set this rational number to the difference of the given numbers.
 Rational sub(Rational x, int y)
          Set this rational number to the difference of the given numbers.
 Rational sub(Rational x, Rational y)
          Set this rational number to the difference of the given numbers.
 String toString()
          Returns a string version of this rational number.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Rational

public Rational()
Construct a new rational number. Its value is 0.


Rational

public Rational(int value)
Construct a new rational number. Its value is value.

Parameters:
value - Value.

Rational

public Rational(int numer,
                int denom)
Construct a new rational number. Its value is numer/denom.

Parameters:
numer - Numerator.
denom - Denominator.
Throws:
ArithmeticException - (unchecked exception) Thrown if denom is 0.

Rational

public Rational(Rational value)
Construct a new rational number. Its value is value.

Parameters:
value - Rational number.

Rational

public Rational(String s)
Construct a new rational number. Its value comes from the string s. The string must obey this syntax: optional minus sign (-), numerator (decimal integer), slash (/), denominator (decimal integer). The slash-and-denominator is optional. If present, the denominator must be greater than 0. There is no whitespace within the string.

Parameters:
s - String.
Throws:
NumberFormatException - (unchecked exception) Thrown if s cannot be parsed into a rational number.
Method Detail

assign

public Rational assign(Rational x)
Set this rational number to the given number.

Parameters:
x - Rational number.
Returns:
This rational number, with its value set to x.

assign

public Rational assign(int x)
Set this rational number to the given number.

Parameters:
x - Integer.
Returns:
This rational number, with its value set to x.

assign

public Rational assign(String s)
Set this rational number to the value parsed from the given string. The string must obey this syntax: optional minus sign (-), numerator (decimal integer), slash (/), denominator (decimal integer). The slash-and-denominator is optional. If present, the denominator must be greater than 0. There is no whitespace within the string.

Parameters:
s - String.
Returns:
This rational number, with its value set to s.
Throws:
NumberFormatException - (unchecked exception) Thrown if s cannot be parsed into a rational number.

neg

public Rational neg(Rational x)
Set this rational number to the negative of the given number.

Parameters:
x - Rational number.
Returns:
This rational number, with its value set to -x.

neg

public Rational neg(int x)
Set this rational number to the negative of the given number.

Parameters:
x - Integer.
Returns:
This rational number, with its value set to -x.

abs

public Rational abs(Rational x)
Set this rational number to the absolute value of the given number.

Parameters:
x - Rational number.
Returns:
This rational number, with its value set to abs(x).

abs

public Rational abs(int x)
Set this rational number to the absolute value of the given number.

Parameters:
x - Integer.
Returns:
This rational number, with its value set to abs(x).

intPart

public Rational intPart(Rational x)
Set this rational number to the integer part of the given rational number.

Parameters:
x - Rational number.
Returns:
This rational number, with its value set to int(x).

fracPart

public Rational fracPart(Rational x)
Set this rational number to the fractional part of the given rational number.

Parameters:
x - Rational number.
Returns:
This rational number, with its value set to frac(x).

add

public Rational add(Rational x,
                    Rational y)
Set this rational number to the sum of the given numbers.

Parameters:
x - Rational number.
y - Rational number.
Returns:
This rational number, with its value set to x+y.

add

public Rational add(Rational x,
                    int y)
Set this rational number to the sum of the given numbers.

Parameters:
x - Rational number.
y - Integer.
Returns:
This rational number, with its value set to x+y.

add

public Rational add(int x,
                    Rational y)
Set this rational number to the sum of the given numbers.

Parameters:
x - Integer.
y - Rational number.
Returns:
This rational number, with its value set to x+y.

add

public Rational add(int x,
                    int y)
Set this rational number to the sum of the given numbers.

Parameters:
x - Integer.
y - Integer.
Returns:
This rational number, with its value set to x+y.

sub

public Rational sub(Rational x,
                    Rational y)
Set this rational number to the difference of the given numbers.

Parameters:
x - Rational number.
y - Rational number.
Returns:
This rational number, with its value set to x-y.

sub

public Rational sub(Rational x,
                    int y)
Set this rational number to the difference of the given numbers.

Parameters:
x - Rational number.
y - Integer.
Returns:
This rational number, with its value set to x-y.

sub

public Rational sub(int x,
                    Rational y)
Set this rational number to the difference of the given numbers.

Parameters:
x - Integer.
y - Rational number.
Returns:
This rational number, with its value set to x-y.

sub

public Rational sub(int x,
                    int y)
Set this rational number to the difference of the given numbers.

Parameters:
x - Integer.
y - Integer.
Returns:
This rational number, with its value set to x-y.

mul

public Rational mul(Rational x,
                    Rational y)
Set this rational number to the product of the given numbers.

Parameters:
x - Rational number.
y - Rational number.
Returns:
This rational number, with its value set to x*y.

mul

public Rational mul(Rational x,
                    int y)
Set this rational number to the product of the given numbers.

Parameters:
x - Rational number.
y - Integer.
Returns:
This rational number, with its value set to x*y.

mul

public Rational mul(int x,
                    Rational y)
Set this rational number to the product of the given numbers.

Parameters:
x - Integer.
y - Rational number.
Returns:
This rational number, with its value set to x*y.

mul

public Rational mul(int x,
                    int y)
Set this rational number to the product of the given numbers.

Parameters:
x - Integer.
y - Integer.
Returns:
This rational number, with its value set to x*y.

div

public Rational div(Rational x,
                    Rational y)
Set this rational number to the quotient of the given numbers.

Parameters:
x - Rational number.
y - Rational number.
Returns:
This rational number, with its value set to x/y.
Throws:
ArithmeticException - (unchecked exception) Thrown if y is 0.

div

public Rational div(Rational x,
                    int y)
Set this rational number to the quotient of the given numbers.

Parameters:
x - Rational number.
y - Integer.
Returns:
This rational number, with its value set to x/y.
Throws:
ArithmeticException - (unchecked exception) Thrown if y is 0.

div

public Rational div(int x,
                    Rational y)
Set this rational number to the quotient of the given numbers.

Parameters:
x - Integer.
y - Rational number.
Returns:
This rational number, with its value set to x/y.
Throws:
ArithmeticException - (unchecked exception) Thrown if y is 0.

div

public Rational div(int x,
                    int y)
Set this rational number to the quotient of the given numbers.

Parameters:
x - Integer.
y - Integer.
Returns:
This rational number, with its value set to x/y.
Throws:
ArithmeticException - (unchecked exception) Thrown if y is 0.

min

public Rational min(Rational x,
                    Rational y)
Set this rational number to the minimum of the given numbers.

Parameters:
x - Rational number.
y - Rational number.
Returns:
This rational number, with its value set to the smaller of x and y.

min

public Rational min(Rational x,
                    int y)
Set this rational number to the minimum of the given numbers.

Parameters:
x - Rational number.
y - Integer.
Returns:
This rational number, with its value set to the smaller of x and y.

min

public Rational min(int x,
                    Rational y)
Set this rational number to the minimum of the given numbers.

Parameters:
x - Integer.
y - Rational number.
Returns:
This rational number, with its value set to the smaller of x and y.

min

public Rational min(int x,
                    int y)
Set this rational number to the minimum of the given numbers.

Parameters:
x - Integer.
y - Integer.
Returns:
This rational number, with its value set to the smaller of x and y.

max

public Rational max(Rational x,
                    Rational y)
Set this rational number to the maximum of the given numbers.

Parameters:
x - Rational number.
y - Rational number.
Returns:
This rational number, with its value set to the larger of x and y.

max

public Rational max(Rational x,
                    int y)
Set this rational number to the maximum of the given numbers.

Parameters:
x - Rational number.
y - Integer.
Returns:
This rational number, with its value set to the larger of x and y.

max

public Rational max(int x,
                    Rational y)
Set this rational number to the maximum of the given numbers.

Parameters:
x - Integer.
y - Rational number.
Returns:
This rational number, with its value set to the larger of x and y.

max

public Rational max(int x,
                    int y)
Set this rational number to the maximum of the given numbers.

Parameters:
x - Integer.
y - Integer.
Returns:
This rational number, with its value set to the larger of x and y.

eqZero

public boolean eqZero()
Determine if this rational number is equal to zero.

Returns:
True if this rational number is equal to 0, false otherwise.

neZero

public boolean neZero()
Determine if this rational number is not equal to zero.

Returns:
True if this rational number is not equal to 0, false otherwise.

ltZero

public boolean ltZero()
Determine if this rational number is less than zero.

Returns:
True if this rational number is less than 0, false otherwise.

leZero

public boolean leZero()
Determine if this rational number is less than or equal to zero.

Returns:
True if this rational number is less than or equal to 0, false otherwise.

gtZero

public boolean gtZero()
Determine if this rational number is greater than zero.

Returns:
True if this rational number is greater than 0, false otherwise.

geZero

public boolean geZero()
Determine if this rational number is greater than or equal to zero.

Returns:
True if this rational number is greater than or equal to 0, false otherwise.

eq

public boolean eq(Rational x)
Determine if this rational number is equal to the given number.

Parameters:
x - Rational number.
Returns:
True if this rational number is equal to x, false otherwise.

eq

public boolean eq(int x)
Determine if this rational number is equal to the given number.

Parameters:
x - Integer.
Returns:
True if this rational number is equal to x, false otherwise.

ne

public boolean ne(Rational x)
Determine if this rational number is not equal to the given number.

Parameters:
x - Rational number.
Returns:
True if this rational number is not equal to x, false otherwise.

ne

public boolean ne(int x)
Determine if this rational number is not equal to the given number.

Parameters:
x - Integer.
Returns:
True if this rational number is not equal to x, false otherwise.

lt

public boolean lt(Rational x)
Determine if this rational number is less than the given number.

Parameters:
x - Rational number.
Returns:
True if this rational number is less than x, false otherwise.

lt

public boolean lt(int x)
Determine if this rational number is less than the given number.

Parameters:
x - Integer.
Returns:
True if this rational number is less than x, false otherwise.

le

public boolean le(Rational x)
Determine if this rational number is less than or equal to the given number.

Parameters:
x - Rational number.
Returns:
True if this rational number is less than or equal to x, false otherwise.

le

public boolean le(int x)
Determine if this rational number is less than or equal to the given number.

Parameters:
x - Integer.
Returns:
True if this rational number is less than or equal to x, false otherwise.

gt

public boolean gt(Rational x)
Determine if this rational number is greater than the given number.

Parameters:
x - Rational number.
Returns:
True if this rational number is greater than x, false otherwise.

gt

public boolean gt(int x)
Determine if this rational number is greater than the given number.

Parameters:
x - Integer.
Returns:
True if this rational number is greater than x, false otherwise.

ge

public boolean ge(Rational x)
Determine if this rational number is greater than or equal to the given number.

Parameters:
x - Rational number.
Returns:
True if this rational number is greater than or equal to x, false otherwise.

ge

public boolean ge(int x)
Determine if this rational number is greater than or equal to the given number.

Parameters:
x - Integer.
Returns:
True if this rational number is greater than or equal to x, false otherwise.

intValue

public int intValue()
Converts this rational number to an integer. If this rational number is not an exact integer, any fractional part is truncated.

Returns:
Integer value of this rational number.

longValue

public long longValue()
Converts this rational number to a long integer. If this rational number is not an exact integer, any fractional part is truncated.

Returns:
Long integer value of this rational number.

floatValue

public float floatValue()
Converts this rational number to a single precision floating point number.

Returns:
Single precision floating point value of this rational number.

doubleValue

public double doubleValue()
Converts this rational number to a double precision floating point number.

Returns:
Double precision floating point value of this rational number.

toString

public String toString()
Returns a string version of this rational number. If this rational number is an exact integer, the string is in the form "<numer>", otherwise the string is in the form "<numer>/<denom>". If this rational number is less than 0, the string begins with a minus sign (-).

Overrides:
toString in class Object

equals

public boolean equals(Object obj)
Determine if this rational number is equal to the given object. To be equal, obj must be a non-null instance of class Rational whose value is the same as this rational number's value.

Overrides:
equals in class Object
Parameters:
obj - Object to test.
Returns:
True if this rational number is equal to obj, false otherwise.

hashCode

public int hashCode()
Returns a hash code for this rational number.

Overrides:
hashCode in class Object

compareTo

public int compareTo(Object obj)
Compare this rational number to the given object.

Specified by:
compareTo in interface Comparable
Parameters:
obj - Object to compare to.
Returns:
A number less than, equal to, or greater than 0 if this rational number is less than, equal to, or greater than obj, respectively.
Throws:
NullPointerException - (unchecked exception) Thrown if obj is null.
ClassCastException - (unchecked exception) Thrown if obj is not an instance of class Rational.


Copyright © 2005-2012 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.