I am working on a coding assignment where I am supposed to write the methods to simplify, find gcf, add, subtract, multiply and divide two fractions. I have written all of the methods, but I am having trouble in the divide method because whenever I attempt to divide by a negative fraction (such as the -6/17), it prints 0/1 as the result. Here is my Fraction class with all methods, but the only ones I added are the ones listed above. The rest were provided by my instructor. Here's the class:
class Fraction {
private int numerator = 0; // numerator (and keeps sign)
private int denominator = 1; // always 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 I was given a test code by my instructor that has test fractions. Here it 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 that 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 by zero' handling
Fraction zero = new Fraction();
System.out.println(f2 + " / " + zero + " = " + f2.divide(zero));
}
}
The results should show up like 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 UP AS 0/1 INSTEAD...)
75/175 / 0/1 = 0/1
I know it's something in the Divide method because when I changed -6/17 to just 6/17 in the last one, it worked and printed 17/14 when simplified. I just have no idea what in the Divide method is not working with negative fractions. Is there maybe something I can add in there to help with this issue? Thanks 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
is positive while f
is negative. According to your assumption,
den1 > 0
den2 > 0
num1 > 0
num2 < 0
num3 = num1 * den2 > 0
den3 = den1 * num2 < 0
However, when new Fraction(num3, den3)
which calls set()
,
public boolean set(int n, int d) {
if (d > 0) {
numerator = n;
denominator = d;
return true;
} else
return false;
}
When the denominator is less than 0, you returned false
which forbids the value from set into the class.
You could inverse signs for both numerator and denominator instead of simply returning false
.