java - Issues with Dividing by Negative Fractions -


i working on coding assignment supposed write methods simplify, find gcf, add, subtract, multiply , divide 2 fractions. have written of methods, having trouble in divide method because whenever attempt divide negative fraction (such -6/17), prints 0/1 result. here fraction class methods, ones added ones listed above. rest provided instructor. here's class:

class fraction {     private int numerator = 0; // numerator (and keeps sign)     private int denominator = 1; // stores positive value      public fraction() {     }      public fraction(int n, int d) {         if (set(n, d) == false)            set(0, 1);     }      public boolean set(int n, int d) {         if (d > 0) {            numerator = n;            denominator = d;            return true;         } else            return false;     }      public string tostring() {        return (numerator + "/" + denominator);     }      public int getnumerator() {         return numerator;     }      public int getdenominator() {         return denominator;     }      public double decimal() {         return (double) numerator / denominator;     }      public fraction simplify(){        int gcd = getgcd(this);         int simpnum = this.numerator;        int simpden = this.denominator;         simpnum /= gcd;        simpden /= gcd;         fraction f = new fraction (simpnum, simpden);        return f;     }      public int getgcd (fraction f){        int testnum = f.numerator;        int testden = f.denominator;         if (testnum < 0)            testnum = 0 - testnum;        else if (testden < 0)            testden = 0 - testden;         if (testnum == 0){            return testden;        }         while (testnum != testden){            if (testnum > testden)               testnum -= testden;            else               testden -= testnum;         }       return testnum;     }      public fraction add (fraction f){        int cd = this.denominator * f.denominator;         int den1 = this.denominator;        int den2 = f.denominator;        int num1 = this.numerator * (cd / den1);        int num2 = f.numerator * (cd / den2);        int num3 = num1 + num2;        fraction f2 = new fraction (num3, cd);        f2 = f2.simplify();        return f2;     }      public fraction subtract (fraction f){        int cd = this.denominator * f.denominator;         int den1 = this.denominator;        int den2 = f.denominator;        int num1 = this.numerator * (cd / den1);        int num2 = f.numerator * (cd / den2);        int num3 = num1 - num2;         fraction f2 = new fraction (num3, cd);        f2 = f2.simplify();        return f2;     }      public fraction multiply (fraction f){        int den1 = this.denominator;        int den2 = f.denominator;        int num1 = this.numerator;        int num2 = f.numerator;        int num3 = num1 * num2;        int den3 = den1 * den2;         fraction f2 = new fraction (num3, den3);        f2 = f2.simplify();        return f2;     }      public fraction divide (fraction f){        int den1 = this.denominator;        int den2 = f.denominator;        int num1 = this.numerator;        int num2 = f.numerator;         int num3 = num1 * den2;        int den3 = den1 * num2;         fraction f2 = new fraction (num3, den3);        f2 = f2.simplify();        return f2;     } } 

and given test code instructor has test fractions. here is:

    public class fractiontester {     public static void main (string[] args) {      system.out.println("\n\nfraction tests:\n");      fraction f1 = new fraction(4, 6);     fraction f2 = new fraction(75, 175);     fraction f3 = new fraction(-6, 17);      system.out.println(f1 + " simplified = " + f1.simplify());     system.out.println(f2 + " simplified = " + f2.simplify());     system.out.println(f3 + " simplified = " + f3.simplify());      // show f1, f2, f3 haven't changed     system.out.println("f1 = " + f1);     system.out.println("f2 = " + f2);     system.out.println("f3 = " + f3);      // arithmetic     system.out.println(f1 + " + " + f2 + " = " + f1.add(f2));     system.out.println(f1 + " - " + f2 + " = " + f1.subtract(f2));     system.out.println(f1 + " * " + f2 + " = " + f1.multiply(f2));     system.out.println(f1 + " / " + f2 + " = " + f1.divide(f2));     system.out.println();      system.out.println(f2 + " + " + f3 + " = " + f2.add(f3));     system.out.println(f2 + " - " + f3 + " = " + f2.subtract(f3));     system.out.println(f2 + " * " + f3 + " = " + f2.multiply(f3));     system.out.println(f2 + " / " + f3 + " = " + f2.divide(f3));     system.out.println();      // test 'division zero' handling     fraction 0 = new fraction();     system.out.println(f2 + " / " + 0 + " = " + f2.divide(zero));   } } 

the results should show this:

4/6 simplified = 2/3 75/175 simplified = 3/7 -6/17 simplified = -6/17 f1 = 4/6 f2 = 75/175 f3 = -6/17 4/6 + 75/175 = 23/21 4/6 - 75/175 = 5/21 4/6 * 75/175 = 2/7 4/6 / 75/175 = 14/9  75/175 + -6/17 = 9/119 75/175 - -6/17 = 93/119 75/175 * -6/17 = -18/119 75/175 / -6/17 = -17/14 (this shows 0/1 instead...)  75/175 / 0/1 = 0/1  

i know it's in divide method because when changed -6/17 6/17 in last one, worked , printed 17/14 when simplified. have no idea in divide method not working negative fractions. there maybe can add in there issue? in advance.

in divide(),

public fraction divide (fraction f){    int den1 = this.denominator;    int den2 = f.denominator;    int num1 = this.numerator;    int num2 = f.numerator;     int num3 = num1 * den2;    int den3 = den1 * num2;     fraction f2 = new fraction (num3, den3);    ... 

assume this positive while f negative. according assumption,

den1 > 0 den2 > 0 num1 > 0 num2 < 0 num3 = num1 * den2 > 0 den3 = den1 * num2 < 0 

however, when new fraction(num3, den3) calls set(),

public boolean set(int n, int d) {     if (d > 0) {        numerator = n;        denominator = d;        return true;     } else        return false; } 

when denominator less 0, returned false forbids value set class.

you inverse signs both numerator , denominator instead of returning false.


Comments