Package edu.kirkwood.model
Class Fraction
java.lang.Object
edu.kirkwood.model.Fraction
- All Implemented Interfaces:
Comparable<Fraction>
Represents a fraction with an integer numerator and denominator.
This class provides methods for fraction arithmetic, simplification
and comparison.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds another fraction to this fraction.int
Compares this fraction to another fraction.Divides this fraction by another fraction.boolean
static int
gcd
(int a, int b) Calculates the greatest common divisor (GCD) of two integers.int
Gets the denominator of the fraction.int
Gets the numerator (the top part) of the fractionint
hashCode()
static int
lcm
(int a, int b) Calculates the least common multiple (LCM) of two integers.Multiplies this fraction by another fraction.void
setDenominator
(int denominator) Sets the denominator of the fraction.void
setNumerator
(int numerator) Sets the numerator of the fraction.void
simplify()
Reduces fraction to the lowest termsSubtracts another fraction from this fraction.static String
toMixedNumber
(int a, int b) Converts this fraction to a mixed number string representation (e.g., "1 2/3").toString()
Returns a string representation of the fraction in the format "numerator/denominator".
-
Constructor Details
-
Fraction
public Fraction()Default constructor Initializes a new fraction to 1/1 -
Fraction
public Fraction(int numerator, int denominator) Constructs a fraction with a specified numerator and denominator- Parameters:
numerator
- The top part of the fractiondenominator
- The bottom part of the fraction
-
-
Method Details
-
getNumerator
public int getNumerator()Gets the numerator (the top part) of the fraction- Returns:
- the numerator
-
setNumerator
public void setNumerator(int numerator) Sets the numerator of the fraction.- Parameters:
numerator
- the new numerator
-
getDenominator
public int getDenominator()Gets the denominator of the fraction.- Returns:
- the denominator
-
setDenominator
public void setDenominator(int denominator) Sets the denominator of the fraction.- Parameters:
denominator
- the new denominator- Throws:
ArithmeticException
- if the denominator is zero
-
toString
Returns a string representation of the fraction in the format "numerator/denominator". -
compareTo
Compares this fraction to another fraction.- Specified by:
compareTo
in interfaceComparable<Fraction>
- Parameters:
o
- the other Fraction to be compared.- Returns:
- a negative integer, zero, or a positive integer.
-
equals
-
hashCode
public int hashCode() -
gcd
public static int gcd(int a, int b) Calculates the greatest common divisor (GCD) of two integers.- Parameters:
a
- the first integerb
- the second integer- Returns:
- the greatest common divisor of a and b
-
lcm
public static int lcm(int a, int b) Calculates the least common multiple (LCM) of two integers.- Parameters:
a
- the first integerb
- the second integer- Returns:
- the least common multiple of a and b
-
simplify
public void simplify()Reduces fraction to the lowest terms -
toMixedNumber
Converts this fraction to a mixed number string representation (e.g., "1 2/3"). If the fraction is a proper fraction, it returns the fraction itself.- Returns:
- a string representation of the fraction as a mixed number
-
add
Adds another fraction to this fraction.- Parameters:
other
- the fraction to add- Returns:
- a new Fraction object representing the sum
-
subtract
Subtracts another fraction from this fraction.- Parameters:
other
- the fraction to subtract- Returns:
- a new Fraction object representing the difference
-
multiply
Multiplies this fraction by another fraction.- Parameters:
other
- the fraction to multiply by- Returns:
- a new Fraction object representing the product
-
divide
Divides this fraction by another fraction.- Parameters:
other
- the fraction to divide by (the divisor)- Returns:
- a new Fraction object representing the quotient
- Throws:
IllegalArgumentException
- if the divisor is zero
-