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.intCompares this fraction to another fraction.Divides this fraction by another fraction.static intgcd(int a, int b) Calculates the greatest common divisor (GCD) of two integers.intGets the denominator of the fraction.intGets the numerator of the fraction.static intlcm(int a, int b) Calculates the least common multiple (LCM) of two integers.Multiplies this fraction by another fraction.voidsetDenominator(int denominator) Sets the denominator of the fraction.voidsetNumerator(int numerator) Sets the numerator of the fraction.voidsimplify()Simplifies this fraction to its lowest terms by dividing the numerator and denominator by their greatest common divisor.Subtracts another fraction from this fraction.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 numerator of the fractiondenominator- the denominator of the fraction
-
-
Method Details
-
getNumerator
public int getNumerator()Gets the numerator 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:
compareToin interfaceComparable<Fraction>- Parameters:
o- the other Fraction to be compared.- Returns:
- a negative integer, zero, or a positive integer.
-
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()Simplifies this fraction to its lowest terms by dividing the numerator and denominator by their greatest common divisor. -
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
-