Class Fraction

java.lang.Object
edu.kirkwood.model.Fraction
All Implemented Interfaces:
Comparable<Fraction>

public class Fraction extends Object implements Comparable<Fraction>
Represents a fraction with an integer numerator and denominator. This class provides methods for fraction arithmetic, simplification and comparison.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor Initializes a new fraction to 1/1
    Fraction(int numerator, int denominator)
    Constructs a fraction with a specified numerator and denominator
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Fraction other)
    Adds 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 fraction
    int
     
    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
    Reduces fraction to the lowest terms
    Subtracts 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").
    Returns a string representation of the fraction in the format "numerator/denominator".

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 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 fraction
      denominator - 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

      public String toString()
      Returns a string representation of the fraction in the format "numerator/denominator".
      Overrides:
      toString in class Object
      Returns:
      a string representation of the fraction
    • compareTo

      public int compareTo(Fraction o)
      Compares this fraction to another fraction.
      Specified by:
      compareTo in interface Comparable<Fraction>
      Parameters:
      o - the other Fraction to be compared.
      Returns:
      a negative integer, zero, or a positive integer.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • gcd

      public static int gcd(int a, int b)
      Calculates the greatest common divisor (GCD) of two integers.
      Parameters:
      a - the first integer
      b - 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 integer
      b - the second integer
      Returns:
      the least common multiple of a and b
    • simplify

      public void simplify()
      Reduces fraction to the lowest terms
    • toMixedNumber

      public static String toMixedNumber(int a, int b)
      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

      public Fraction add(Fraction other)
      Adds another fraction to this fraction.
      Parameters:
      other - the fraction to add
      Returns:
      a new Fraction object representing the sum
    • subtract

      public Fraction subtract(Fraction other)
      Subtracts another fraction from this fraction.
      Parameters:
      other - the fraction to subtract
      Returns:
      a new Fraction object representing the difference
    • multiply

      public Fraction multiply(Fraction other)
      Multiplies this fraction by another fraction.
      Parameters:
      other - the fraction to multiply by
      Returns:
      a new Fraction object representing the product
    • divide

      public Fraction divide(Fraction other)
      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