|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.cmu.cs.pattis.cs151xx.Rational
public class Rational
Objects constructed from the Rational class act like fractions (rational numbers have a sign, numerator, and denominator). This class implements some standard arithemetic and relational operations (as methods) on values of this class. Object constructed from this class obey the following class invariants
Field Summary | |
---|---|
static Rational |
ONE
Stores the fraction 1/1. |
static Rational |
ZERO
Stores the fraction 0/1. |
Constructor Summary | |
---|---|
Rational()
Constructs a Rational object 0/1. |
|
Rational(int numerator)
Constructs a Rational object, specifying the
numerator of the fraction (the denominator is one). |
|
Rational(int numerator,
int denominator)
Constructs a Rational object, specifying the
numerator and denominator of the fraction. |
|
Rational(Rational other)
Constructs a Rational object whose state is the
same as other . |
Method Summary | |
---|---|
Rational |
abs()
Returns a new fraction that is the absolute value of this one. |
Rational |
add(Rational other)
Returns a new fraction that is the sum of this one and other . |
int |
compareTo(Rational other)
Returns the trichotomous comparison of this value and other . |
Rational |
divide(Rational other)
Returns a new fraction that is the dividend of this one and other . |
boolean |
equals(Rational other)
Returns whether or not this fraction is equal to other . |
int |
getDenominator()
Returns the denominator of the fraction. |
int |
getNumerator()
Returns the numerator of the fraction. |
Rational |
multiply(Rational other)
Returns a new fraction that is the product of this one and other . |
Rational |
negate()
Returns a new fraction that is the negated value of this one. |
static Rational |
prompt(java.lang.String message)
Returns a new fraction after prompting the user for its numerator and denominator. |
Rational |
reciprocal()
Returns a new fraction that is the reciprocal value of this one: 1/ this . |
Rational |
subtract(Rational other)
Returns a new fraction that is the difference of this one and other . |
java.lang.String |
toDecimalString(int decimalPlaces)
Returns a String |
java.lang.String |
toString()
Returns a String |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Rational ZERO
public static final Rational ONE
Constructor Detail |
---|
public Rational(int numerator, int denominator) throws java.lang.IllegalArgumentException
Rational
object, specifying the
numerator and denominator of the fraction.
These value are examined and put in a canonical form to ensure
the following class invariants
numerator
- specifies the numerator of the fractiondenominator
- specifies the denominator of the fraction
java.lang.IllegalArgumentException
- if denominator is 0
public Rational(int numerator)
Rational
object, specifying the
numerator of the fraction (the denominator is one).
numerator
- specifies the numerator of the fractionpublic Rational()
Rational
object 0/1.
public Rational(Rational other)
Rational
object whose state is the
same as other
.
other
- specifies the fraction whose value to store into
the state of this objectMethod Detail |
---|
public int getNumerator()
public int getDenominator()
public boolean equals(Rational other)
other
.
other
- specifies the other fraction in the equality check
other
public int compareTo(Rational other)
other
.
More specifically:
this
< other
this
= other
this
> other
f1
is less than or equal
to f2
:
if (f1.compareTo(f2) <= 0)
other
- specifies the other fraction in the comparison
other
public Rational abs()
public Rational negate()
public Rational reciprocal() throws java.lang.IllegalStateException
this
.
this
java.lang.IllegalStateException
- if this value is 0public Rational add(Rational other)
other
.
other
- specifies the other fraction in the sum
other
public Rational subtract(Rational other)
other
.
other
- specifies the other fraction in the difference
other
public Rational multiply(Rational other)
other
.
other
- specifies the other fraction in the product
other
public Rational divide(Rational other) throws java.lang.IllegalArgumentException
other
.
other
- specifies the other fraction in the division
other
java.lang.IllegalArgumentException
- if other value is 0
public java.lang.String toString()
String representation of the state of this
fraction: numerator/denominator.
- Overrides:
toString
in class java.lang.Object
- Returns:
- a
String representation of the state of this
fraction: numerator/denominator
public java.lang.String toDecimalString(int decimalPlaces)
String representation of the state of this
fraction, as a decimal value approximating the fraction.
For example (new Rational(1,3)).toDecimalString(20)
returns ".33333333333333333333"
;
(new Rational(1,7)).toDecimalString(13)
returns ".1428571428571"
.
- Parameters:
decimalPlaces
- specifies the number of decimal places to use
in the approximation
- Returns:
- a
String representation of the state of this
fraction, as a decimal value approximating the fraction
public static Rational prompt(java.lang.String message)
Rational.prompt("Enter height")
an interaction on the console screen might look like
Enter Height Enter numerator : 1 Enter denominator: 3
message
- specifies the message with which to prompt the user
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |