为什么我的倍增不起作用?

时间:2015-10-14 18:39:11

标签: java

我必须创建一个名为fraction的类和测试程序。创建分数,可以对其进行简化,相乘,分割,相加和相减。当我使用乘法方法时,它会一直返回第二个乘法的平方,我不知道为什么。

  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);
    }

 }

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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**;
}

然后这个不应该给出奇怪的数字和字母(实际上是对象的当前内存地址)。

一切顺利

答案 2 :(得分:0)

我弄清楚为什么它正在调整最后一个数字而不是乘以这两个数字。这是因为我的实例变量是静态的,不应该是它们。