我必須創建一個名爲fraction的類和測試程序。一個分數被創建,它可以被簡化,相乘,分割,加,減。當我使用multiply方法時,它會一直返回第二個乘法的平方,我不知道爲什麼。爲什麼我的乘法不起作用?
class Fraction{
private static int numerator;
private static int denominator;
public Fraction(int num, int den){
numerator = num;
denominator = den;
}
public static int getNumerator(){
return numerator;
}
public static int getDenominator(){
return denominator;
}
public static String getFraction(){
return (numerator + "/" + denominator);
}
public static void setNumerator(int x){
numerator = x;
}
public static void setDenominator(int x){
denominator = x;
}
public static Fraction multiply(Fraction fraction, Fraction fractionTwo){
Fraction newFraction = new Fraction(fraction.getNumerator() * fractionTwo.getNumerator(), fraction.getDenominator() * fractionTwo.getDenominator());
return reduce(newFraction.getNumerator(), newFraction.getDenominator());
}
public static Fraction divide(Fraction fraction, Fraction fractionTwo){
Fraction newFraction = new Fraction(fraction.getNumerator() * fractionTwo.getDenominator(), fraction.getDenominator() *fractionTwo.getNumerator());
return reduce(newFraction.getNumerator(), newFraction.getDenominator());
}
public static Fraction commonDenominator(Fraction fraction, Fraction fractionTwo){
int x = 2;
Fraction multiply = new Fraction(x, x);
for(x = multiply.getNumerator(); fraction.getDenominator() != fractionTwo.getDenominator(); x++){
multiply.setNumerator(x);
multiply.setDenominator(x);
}
multiply(multiply, fraction);
multiply(multiply, fractionTwo);
Fraction commonDenominator = new Fraction(x, x);
return commonDenominator;
}
public static Fraction addition(Fraction fraction, Fraction fractionTwo){
Fraction commonDenominator = commonDenominator(fraction, fractionTwo);
Fraction fractionAdd = multiply(fraction, commonDenominator);
Fraction fractionAddTwo = multiply(fractionTwo, commonDenominator);
Fraction newFraction = new Fraction((fractionAddTwo.getNumerator() + fractionAdd.getNumerator()), (fractionAddTwo.getDenominator() + fractionAdd.getDenominator()));
return newFraction;
}
public static Fraction subtraction(Fraction fraction, Fraction fractionTwo){
Fraction commonDenominator = commonDenominator(fraction, fractionTwo);
Fraction fractionAdd = multiply(fraction, commonDenominator);
Fraction fractionAddTwo = multiply(fractionTwo, commonDenominator);
Fraction newFraction = new Fraction((fractionAdd.getNumerator() - fractionAddTwo.getNumerator()), (fractionAdd.getDenominator() + fractionAddTwo.getDenominator()));
return newFraction;
}
public static boolean equals(Fraction fraction){
if(numerator == fraction.getNumerator() && denominator == fraction.getDenominator()){
return true;
}
else{
return false;
}
}
public static double decimalValue(Fraction fraction){
double decimal = (double)fraction.getNumerator()/fraction.getDenominator();
return decimal;
}
public static Fraction reduce(int numerator, int denominator){
if(numerator==0&&denominator==0){
numerator = 0;
denominator = 0;
}
else{
for(int x = Math.max(numerator, denominator); x>0; x--){
if(denominator == numerator){
numerator = 1;
denominator = 1;
}
else if(numerator == 0){
numerator = 0;
denominator = 1;
}
else if(numerator%x==0 && denominator%x==0){
numerator = numerator/x;
denominator = denominator/x;
}
}
}
Fraction fraction = new Fraction(numerator, denominator);
return fraction;
}
public String toString(){
reduce(numerator, denominator);
return (numerator) + "/" + (denominator);
}
}
'static vs instance' methods。 –
報告了什麼錯誤? –
錯誤實際發生在哪條線上? – tnw