2015-10-14 52 views
-1

我必須創建一個名爲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); 
    } 

} 
+1

'static vs instance' methods。 –

+0

報告了什麼錯誤? –

+0

錯誤實際發生在哪條線上? – tnw

回答

1

這種方法應該是靜態的,然後你怎麼稱呼它Fraction.multiply(f1, f2)

public static Fraction multiply(Fraction fraction, Fraction fractionTwo) { 
    Fraction newFraction = new Fraction(fraction.getNumerator() * fractionTwo.getNumerator(), fraction.getDenominator() * fractionTwo.getDenominator()); 
    return newFraction.reduce(); 
} 

或方法應該使用對象的當前狀態。那你叫它new Fraction(3,4).multiply(f1)。此方法可能如下所示:

public Fraction multiply(Fraction fractionTwo) { 
    Fraction newFraction = new Fraction(numerator * fractionTwo.getNumerator(), denominator * fractionTwo.getDenominator()); 
    return newFraction.reduce(); 
} 

因此,所有代碼都可能如下所示。

class Fraction { 

    private int numerator; 
    private int denominator; 

    public Fraction(int num, int den) { 
     numerator = num; 
     denominator = den; 
    } 

    public static void main (String[] args){ 
     Fraction r1 = new Fraction (2,3); // 2/3 
     Fraction r2 = new Fraction(3,4); // 3/4 
     Fraction r3 = r1.multiply(r2); // 2/3 * 3/4 
     System.out.println(r3); // print 1/2 
    } 

    public int getNumerator() { 
     return numerator; 
    } 

    public int getDenominator() { 
     return denominator; 
    } 

    public Fraction multiply(Fraction fractionTwo) { 
     Fraction newFraction = new Fraction(numerator * fractionTwo.getNumerator(), denominator * fractionTwo.getDenominator()); 
     return newFraction.reduce(); 
    } 

    public Fraction reduce() { 
     for (int x = Math.max(numerator, denominator); x > 0; x--) { 
      if (numerator % x == 0 && denominator % x == 0) { 
       numerator = numerator/x; 
       denominator = denominator/x; 
      } 
      if (denominator == numerator) { 
       numerator = 1; 
       denominator = 1; 
      } 

     } 
     return this; 
    } 

    @Override 
    public String toString() { 
     return numerator + "/" + denominator; 
    } 
} 
+0

我將其更改爲第二種方法,但出於某種原因,它將參數中的小數部分平方。不知道爲什麼會這樣。 – Sarah

+0

您問過「爲什麼會出現編譯錯誤」。我回答了這個問題。我不知道當你在這個對象上調用方法時會發生什麼。我嘗試瞭解,但'減少'方法有點讓我困惑。也許你應該寫下其他問題,並解釋你的期望和你得到的。 –

+0

好的,謝謝你解釋編譯錯誤。我認爲乘法可能會出錯,因爲它正確地減少了。 – Sarah

0

我不會給出你已經得到的答案。所以我回答有關toString()方法。

請記住,任何對象都繼承自類object。如果您使用方法toString(),則可以從對象類中覆蓋它。

在這裏可以看到該對象類中的方法中: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html

正如你可以看到,該方法toString()表示在那裏使用的對象,它需要沒有參數。所以,如果你實現它,如下所示:

public String toString() { 
    return **your Code**; 
} 

那麼這個人應該沒有給出奇怪的數字和字母(這實際上是對象的當前內存地址)。

所有最優秀的

0

我想通了,爲什麼它是不可能完成的最後一個數字,而不是兩個數字相乘。這是因爲我的實例變量是靜態的,它們不應該是。