For some reason, I'm getting the correct result, but my negative sign is having issues. For example, if I do 1/4 - (-2/4), I get (-3/4).
Here is my minus method for subtracting fractions.
Here is my reduce() method, just in case.../** Subtracts a fraction from another fraction. @param toUse, the fraction to subtract. @return minusFraction, the result after subtraction. */ public Fraction minus(Fraction toUse) { int newNum = ((toUse.numerator * denominator) - (numerator * toUse.denominator)); int newDen = denominator * toUse.denominator; Fraction minusFraction = new Fraction(newNum, newDen); return minusFraction; }
I only switched an operator from my previous addition method, given here as well. I think only switching the + to a - may have caused my issue./** Reduces the fraction, if possible, to it's simplest form. Converts negative fractions to the form -x/y, or if -x/-y --> x/y */ private void reduce() { int lowest = Math.abs(numerator); int highest = Math.abs(denominator); if (lowest > highest) { int temp = highest; highest = lowest; lowest = temp; } while (lowest != 0) { int temp = lowest; lowest = highest % lowest; highest = temp; } numerator /= highest; denominator /= highest; if (denominator < 0) { numerator *= -1; denominator *= -1; } }
/** Adds two fractions together. @param toUse, the fraction to be added. @return plusFraction, the sum of the two fractions. */ public Fraction plus(Fraction toUse) { int newNum = ((toUse.numerator * denominator) + (numerator * toUse.denominator)); int newDen = denominator * toUse.denominator; Fraction plusFraction = new Fraction(newNum, newDen); return plusFraction; }