public class Fraction
extends java.lang.Object
Constructor and Description |
---|
Fraction()
Default constructor - Constructs a fraction with a numerator of 0 and
a denominator of 1.
|
Fraction(int numerator)
Constructor - Constructs a fraction with the specified numerator and a
denominator of 1.
|
Fraction(int numerator,
int denominator)
Constructor - Constructs a fraction from the specified numerator and
denominator that meets the class invariants.
|
Modifier and Type | Method and Description |
---|---|
Fraction |
add(Fraction other)
Adds this fraction to the other fraction and returns the result as
a third new fraction that meets the class invariants and without
modifying this or other.
|
Fraction |
div(Fraction other)
Divides this fraction by the other fraction and returns the result as
a third new fraction that meets the class invariants and does not modify
this or other.
|
boolean |
equals(java.lang.Object other)
Determines if this Fraction is equal other - handles nulls and when
other refers to a non-Fraction object by returning false (See
How to Override the equals Method for detailed assistance).
|
int |
getDenominator()
Returns the denominator of the fraction
|
int |
getNumerator()
Returns the numerator of the fraction
|
Fraction |
mul(Fraction other)
Multiplies this fraction to the other fraction and returns the result
as a third new fraction that meets the class invariants and does not
modify this or other.
|
Fraction |
sub(Fraction other)
Subracts the other fraction from this fraction and returns the result
as a third new fraction that meets the class invariants and does not
modify this or other.
|
java.lang.String |
toString()
Returns a string-representation of the fraction (see details for
specific examples).
|
double |
value()
Returns the value of the fraction as a number.
|
public Fraction()
public Fraction(int numerator)
numerator
- The numerator of the fraction.public Fraction(int numerator, int denominator)
numerator
- The numerator of the fraction.denominator
- The denominator of the fraction.java.lang.IllegalArgumentException
- when denominator is zero.public int getNumerator()
public int getDenominator()
public double value()
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
other
- A reference to the object to compare to this fraction.public Fraction add(Fraction other)
other
- The fraction to add to this fraction.public Fraction sub(Fraction other)
other
- The fraction to subtract from this fraction.public Fraction mul(Fraction other)
other
- The fraction to multiply with this fraction.public Fraction div(Fraction other)
other
- The fraction to divide this fraction by.java.lang.IllegalArgumentException
- when other is zero.