|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.Number | +--foundations.Fraction
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 |
public Fraction()
public Fraction(long num, long denom)
num
- initializes numeratordenom
- initializes denominatorpublic Fraction(long num)
num
- initializes numerator; denominator = 1public Fraction(java.lang.String fString)
String
.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 |
public void setNumerator(long num)
public void setDenominator(long denom)
public long numerator()
public long denominator()
public Fraction add(Fraction f)
f
- is an instance of class Fraction
Fraction
that is the sum of f
and this objectpublic Fraction subtract(Fraction f)
f
- is an instance of class Fraction
Fraction
that is this object minus f
public Fraction multiply(Fraction f)
f
- is an instance of class Fraction
Fraction
that is the product of f
and this objectpublic Fraction divide(Fraction f)
f
- is an instance of class Fraction
Fraction
that is the quotient of this object divided by f
public boolean equals(java.lang.Object obj)
Fraction
and is equal to this object.obj
- must be an instance of class Fraction
obj
have same numerical value.public int compareTo(java.lang.Object obj)
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.f
- is an instance of class Fraction
public int intValue()
int
.int
public long longValue()
long
.numerator / denominator
- as a truncated long
public float floatValue()
float
.(float)numerator / (float)denominator
public double doubleValue()
double
.(double)numerator / (double)denominator
public java.lang.String toString()
numerator
String
with value [-]numerator/denominator
public int hashCode()
int
representing this objectpublic static void main(java.lang.String[] args)
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
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |