我正在编写一个编码分配,我应该编写方法来简化,找到gcf,加,减,乘和除以两个分数。我已经编写了所有方法,但是我在divide方法中遇到了麻烦,因为每当我尝试除以负分数(例如-6/17)时,它就会打印0/1作为结果。这是我的Fraction类,包含所有方法,但我添加的只有上面列出的那些。其余的由我的导师提供。这是班级:
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;
}
}
我的教练给了我一个测试代码的测试代码。这是:
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));
}
}
结果应该显示如下:
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
我知道它在Divide方法中的一些东西,因为当我在最后一个方法中将-6/17改为6/17时,它在简化后工作并打印17/14。我只是不知道Divide方法中的哪些方法不适用于负分数。我可以添加一些东西来帮助解决这个问题吗?提前谢谢。
答案 0 :(得分:1)
在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);
...
假设this
为正,而f
为否定。根据你的假设,
den1 > 0
den2 > 0
num1 > 0
num2 < 0
num3 = num1 * den2 > 0
den3 = den1 * num2 < 0
但是,当new Fraction(num3, den3)
调用set()
时,
public boolean set(int n, int d) {
if (d > 0) {
numerator = n;
denominator = d;
return true;
} else
return false;
}
当分母小于0时,您返回false
,禁止将值设置到类中。
您可以反转分子和分母的符号,而不是简单地返回false
。
答案 1 :(得分:0)
尝试将set
方法更改为:
public boolean set(int n, int d) {
if (d > 0) {
numerator = n;
denominator = d;
return true;
}
else if (d < 0) {
numerator = n * -1;
denominator = d * -1;
return true;
}
else
return false;
}
如果没有第二个if
语句,set
将对具有负恶魔的分数评估为false。在这种情况下,Fraction
方法返回的divide
将为0/1。