edu.rit.numeric
Class Rational
- java.lang.Object
-
- edu.rit.numeric.Rational
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Comparable
public class Rational extends java.lang.Object implements java.lang.Comparable, java.io.SerializableClass 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
HashMaporHashSet. 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
Constructors Constructor and Description 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(java.lang.String s)Construct a new rational number.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description Rationalabs(int x)Set this rational number to the absolute value of the given number.Rationalabs(Rational x)Set this rational number to the absolute value of the given number.Rationaladd(int x, int y)Set this rational number to the sum of the given numbers.Rationaladd(int x, Rational y)Set this rational number to the sum of the given numbers.Rationaladd(Rational x, int y)Set this rational number to the sum of the given numbers.Rationaladd(Rational x, Rational y)Set this rational number to the sum of the given numbers.Rationalassign(int x)Set this rational number to the given number.Rationalassign(Rational x)Set this rational number to the given number.Rationalassign(java.lang.String s)Set this rational number to the value parsed from the given string.intcompareTo(java.lang.Object obj)Compare this rational number to the given object.Rationaldiv(int x, int y)Set this rational number to the quotient of the given numbers.Rationaldiv(int x, Rational y)Set this rational number to the quotient of the given numbers.Rationaldiv(Rational x, int y)Set this rational number to the quotient of the given numbers.Rationaldiv(Rational x, Rational y)Set this rational number to the quotient of the given numbers.doubledoubleValue()Converts this rational number to a double precision floating point number.booleaneq(int x)Determine if this rational number is equal to the given number.booleaneq(Rational x)Determine if this rational number is equal to the given number.booleanequals(java.lang.Object obj)Determine if this rational number is equal to the given object.booleaneqZero()Determine if this rational number is equal to zero.floatfloatValue()Converts this rational number to a single precision floating point number.RationalfracPart(Rational x)Set this rational number to the fractional part of the given rational number.booleange(int x)Determine if this rational number is greater than or equal to the given number.booleange(Rational x)Determine if this rational number is greater than or equal to the given number.booleangeZero()Determine if this rational number is greater than or equal to zero.booleangt(int x)Determine if this rational number is greater than the given number.booleangt(Rational x)Determine if this rational number is greater than the given number.booleangtZero()Determine if this rational number is greater than zero.inthashCode()Returns a hash code for this rational number.RationalintPart(Rational x)Set this rational number to the integer part of the given rational number.intintValue()Converts this rational number to an integer.booleanle(int x)Determine if this rational number is less than or equal to the given number.booleanle(Rational x)Determine if this rational number is less than or equal to the given number.booleanleZero()Determine if this rational number is less than or equal to zero.longlongValue()Converts this rational number to a long integer.booleanlt(int x)Determine if this rational number is less than the given number.booleanlt(Rational x)Determine if this rational number is less than the given number.booleanltZero()Determine if this rational number is less than zero.Rationalmax(int x, int y)Set this rational number to the maximum of the given numbers.Rationalmax(int x, Rational y)Set this rational number to the maximum of the given numbers.Rationalmax(Rational x, int y)Set this rational number to the maximum of the given numbers.Rationalmax(Rational x, Rational y)Set this rational number to the maximum of the given numbers.Rationalmin(int x, int y)Set this rational number to the minimum of the given numbers.Rationalmin(int x, Rational y)Set this rational number to the minimum of the given numbers.Rationalmin(Rational x, int y)Set this rational number to the minimum of the given numbers.Rationalmin(Rational x, Rational y)Set this rational number to the minimum of the given numbers.Rationalmul(int x, int y)Set this rational number to the product of the given numbers.Rationalmul(int x, Rational y)Set this rational number to the product of the given numbers.Rationalmul(Rational x, int y)Set this rational number to the product of the given numbers.Rationalmul(Rational x, Rational y)Set this rational number to the product of the given numbers.booleanne(int x)Determine if this rational number is not equal to the given number.booleanne(Rational x)Determine if this rational number is not equal to the given number.Rationalneg(int x)Set this rational number to the negative of the given number.Rationalneg(Rational x)Set this rational number to the negative of the given number.booleanneZero()Determine if this rational number is not equal to zero.Rationalsub(int x, int y)Set this rational number to the difference of the given numbers.Rationalsub(int x, Rational y)Set this rational number to the difference of the given numbers.Rationalsub(Rational x, int y)Set this rational number to the difference of the given numbers.Rationalsub(Rational x, Rational y)Set this rational number to the difference of the given numbers.java.lang.StringtoString()Returns a string version of this rational number.
-
-
-
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:
java.lang.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(java.lang.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:
java.lang.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(java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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 java.lang.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:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.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:
equalsin classjava.lang.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:
hashCodein classjava.lang.Object
-
compareTo
public int compareTo(java.lang.Object obj)
Compare this rational number to the given object.- Specified by:
compareToin interfacejava.lang.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:
java.lang.NullPointerException- (unchecked exception) Thrown if obj is null.java.lang.ClassCastException- (unchecked exception) Thrown if obj is not an instance of class Rational.
-
-
DMelt 3.0 © DataMelt by jWork.ORG