Class BigFraction

java.lang.Object
BigFraction
All Implemented Interfaces:
Comparable<BigFraction>

public class BigFraction extends Object implements Comparable<BigFraction>
This is a a class of immutible extended-precision rational numbers. All BigFractions are fully reduced and any negative is in the numerator.
  • Field Details

    • ONE

      public static BigFraction ONE
      This is the BigFraction 1/1.
    • ZERO

      public static BigFraction ZERO
      This is the BigFraction 0/1.
    • HALF

      public static BigFraction HALF
      This is the BigFraction 1/2.
  • Constructor Details

    • BigFraction

      public BigFraction(BigInteger num, BigInteger denom)
      This is a general-purpose constructor that creates a BigFraction from a specified numerator denominator.
      Parameters:
      num - the numerator of this BigFraction
      denom - the denominator of this BigFraction
      Throws:
      IllegalArgumentException - if a zero denominator is provided.
    • BigFraction

      public BigFraction(BigInteger num)
      This creates the BigFraction num/1.
      Parameters:
      num - the numerator of this BigFraction. The denominator will be 1.
    • BigFraction

      public BigFraction()
      This no-args constructor creates the BigFraction 0/1.
  • Method Details

    • valueOf

      public static BigFraction valueOf(long num, long denom)
      This static factory method produces a BigFraction with specified numerator and denominator that are longs.
      Parameters:
      num - the numerator
      denom - the denominator
      Returns:
      the BigFraction num/denom
    • toString

      public String toString()
      This gives a string representation of the form num/denom.
      Overrides:
      toString in class Object
      Returns:
      A string of the form num/denom representing this BigFraction.
    • equals

      public boolean equals(Object o)
      This returns true when o is a BigFraction numerically equal to this BigFraction.
      Overrides:
      equals in class Object
      Parameters:
      o - an object we are comparing this BigFraction to.
      Returns:
      true if o is a BigFraction numerically equal to this BigFraction.
    • compareTo

      public int compareTo(BigFraction that)
      This compares this BigFraction to the BigFraction that.
      Specified by:
      compareTo in interface Comparable<BigFraction>
      Parameters:
      that - the BigFraction we are comparing this BigFraction to.
      Returns:
      a negative integer if this < that and a positive integer if this > that, and zero otherwise.
    • add

      public BigFraction add(BigFraction that)
      This computes the sum of two BigFractions.
      Parameters:
      that - the BigFraction we are adding this BigFraction to.
      Returns:
      this + that as a BigFraction.
    • subtract

      public BigFraction subtract(BigFraction that)
      This computes the difference of two BigFractions.
      Parameters:
      that - the BigFraction we are subtraction from this BigFraction.
      Returns:
      this - that as a BigFraction.
    • multiply

      public BigFraction multiply(BigFraction that)
      This computes the product of two BigFractions.
      Parameters:
      that - the BigFraction we are multiplying by. this BigFraction.
      Returns:
      this*that as a BigFraction.
    • divide

      public BigFraction divide(BigFraction that)
      This computes the quotient of two BigFractions.
      Parameters:
      that - the BigFraction we are dividing by. this BigFraction.
      Returns:
      this/that as a BigFraction.
    • pow

      public BigFraction pow(int n)
      This computes thisn.
      Parameters:
      n - the power we are raising this BigFraction to.
      Returns:
      thisn
    • main

      public static void main(String[] args)
      Test code goes in here
      Parameters:
      args - command-line arguments