## Class Fraction

• java.lang.Object
• Fraction

• ```public class Fraction
extends java.lang.Object```
A class to represent fractions. Fraction objects will meet the following class invariants:

1) Fraction objects are always stored in reduced form.

2) Fraction objects always carry their sign (negative or postive) in their numerator only.
• ### Constructor Summary

Constructors
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.
• ### Method Summary

Methods
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.
• ### Methods inherited from class java.lang.Object

`clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
• ### Constructor Detail

• #### Fraction

`public Fraction()`
Default constructor - Constructs a fraction with a numerator of 0 and a denominator of 1.
• #### Fraction

`public Fraction(int numerator)`
Constructor - Constructs a fraction with the specified numerator and a denominator of 1.
Parameters:
`numerator` - The numerator of the fraction.
• #### Fraction

```public Fraction(int numerator,
int denominator)```
Constructor - Constructs a fraction from the specified numerator and denominator that meets the class invariants.
Parameters:
`numerator` - The numerator of the fraction.
`denominator` - The denominator of the fraction.
Throws:
`java.lang.IllegalArgumentException` - when denominator is zero.
• ### Method Detail

• #### getNumerator

`public int getNumerator()`
Returns the numerator of the fraction
Returns:
The numerator of the fraction.
• #### getDenominator

`public int getDenominator()`
Returns the denominator of the fraction
Returns:
The denominator of the fraction.
• #### value

`public double value()`
Returns the value of the fraction as a number.
Returns:
The value of the fraction as a number.
• #### toString

`public java.lang.String toString()`
Returns a string-representation of the fraction (see details for specific examples).
Overrides:
`toString` in class `java.lang.Object`
Returns:
The fraction as a string, for example "1/2" or "-3/4". If the fraction is a whole number, say 4/1, then "4" is returned.
• #### equals

`public 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).
Overrides:
`equals` in class `java.lang.Object`
Parameters:
`other` - A reference to the object to compare to this fraction.
Returns:
Returns true if equal, otherwise false.

`public 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.
Parameters:
`other` - The fraction to add to this fraction.
Returns:
A fraction representing the sum of this and the other fraction.
• #### sub

`public 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.
Parameters:
`other` - The fraction to subtract from this fraction.
Returns:
A fraction representing the difference beteen this and the other fraction.
• #### mul

`public 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.
Parameters:
`other` - The fraction to multiply with this fraction.
Returns:
A fraction representing the product of this and the other fraction.
• #### div

`public 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.
Parameters:
`other` - The fraction to divide this fraction by.
Returns:
A fraction representing the division of this by the other fraction.
Throws:
`java.lang.IllegalArgumentException` - when other is zero.