Java Rational Lab

时间:2016-02-25 01:05:25

标签: java

我似乎无法弄清楚我的代码有什么问题。我注释掉了reduce()方法以查看它是否适用于未减少的分数(我认为我的reduce()不起作用),并且对于它确实有效的第一个,但是当它变为两位数而后者变为两位数时我无法弄清楚它为什么不起作用。

public class Rational implements Comparable<Rational> {
private int numerator, denominator;

public Rational() { numerator = 1; denominator = 1; }
public Rational(int num, int denom) { numerator = num; denominator = denom; }

public void setRational(int num, int denom) { setNumerator(num); setDenominator(denom); }
public void setNumerator(int num) { numerator = num; }
public void setDenominator(int denom) { denominator = denom; }

public int getNumerator() { return numerator; }
public int getDenominator() { return denominator; }

public void reduce() {
    setNumerator(this.numerator / gcd(this.numerator, this.denominator));
    setDenominator(this.denominator / gcd(this.numerator, this.denominator));
}
public int gcd(int num1, int num2) {
    if (num2 == 0) return num1;
    return gcd(num2,num1 % num2);
}

public Object clone() { return new Rational(getNumerator(), getDenominator()); }
public boolean equals(Object obj){ return this.compareTo((Rational)obj) == 0; }

public void add(Rational other) {
    this.setNumerator( (this.getNumerator() * other.denominator ) + ( other.numerator * this.getDenominator() ));
    this.setDenominator( this.getDenominator() * other.denominator );
    //reduce();
}

public void sub(Rational other) {
    numerator = ( this.getNumerator() * other.getDenominator() ) - ( other.getNumerator() * this.getDenominator() );
    denominator = ( this.getDenominator() * other.getDenominator() );
    //reduce();
}

public void mult(Rational other) {
    numerator = ( this.getNumerator() * other.getNumerator() );
    denominator = ( this.getDenominator() * other.getDenominator() );
    //reduce();
}

public void div(Rational other) {
    numerator = (this.getNumerator() * other.getDenominator());
    denominator = (this.getDenominator() * other.getNumerator());
    //reduce();
}

public int compareTo(Rational other) {
    reduce();
    other.reduce();

    if ( this.getDenominator() < other.getDenominator() ) {
        return -1;
    }
    else if ( this.getDenominator() == other.getDenominator() ) {
        if( this.getNumerator() < other.getNumerator() ) {
            return -1;
        }
        else if( this.getNumerator() > other.getNumerator() ) {
            return 1;
        }
        else return 0;
    }
    else return 1;
}

public String toString() {
    return this.numerator + "/" + this.denominator + "\n";

} }

这是跑步者

import static java.lang.System.out;
public class RationalRunner {
public static void main(String args[]) {
    Rational test = new Rational();
    out.println("test = " + test);
    Rational newOne = new Rational(3, 4);
    out.println("newOne = " + newOne);
    out.println("test.equals(newOne) = " + test.equals(newOne));
    newOne = (Rational) test.clone();
    out.println("\n\nnewOne after test.clone() = " + newOne);
    out.println("test.equals(newOne) = " + test.equals(newOne));

    Rational rOne = new Rational(1, 2);
    Rational rTwo = new Rational(2, 3);
    out.println("1/2.equals(2/3) = " + rOne.equals(rTwo));
    test.setRational(4, 6);
    out.println("2/3.equals(4/6) = " + rTwo.equals(test));

    out.println("\n\nrOne = " + rOne);
    out.println("rTwo = " + rTwo);
    out.println("rOne.compareTo(rTwo) = " + rOne.compareTo(rTwo));
    out.println("rTwo.compareTo(rOne) = " + rTwo.compareTo(rOne));
    rOne.add(rTwo);
    out.println("\n\nrOne.add(rTwo) = " + rOne);
    rOne.setRational(1, 2);
    rTwo.setRational(1, 3);
    rOne.add(rTwo);
    out.println("\n\n1/2.add(1/3) = " + rOne);

    rOne.setRational(4, 10);
    rTwo.setRational(3, 5);
    rOne.add(rTwo);
    out.println("\n\n4/10.add(3/5) = " + rOne);
    rOne.setRational(2, 10);
    rTwo.setRational(3, 6);
    rOne.add(rTwo);
    out.println("\n\n2/10.add(3/6) = " + rOne);
    //1/4 + 2/8 = 1/2
    rOne.setRational(1, 4);
    rTwo.setRational(2, 8);
    out.println("\n\n1/4.equals(2/8) = " + rOne.equals(rTwo));
    rOne.add(rTwo);
    out.println("\n\n1/4.add(2/8) = " + rOne);

    //1/6 + 2/8 = 5/12
    rOne.setRational(1, 6);
    rTwo.setRational(2, 8);
    out.println("\n\n1/6.equals(2/8) = " + rOne.equals(rTwo));
    rOne.add(rTwo);
    out.println("\n\n1/6.add(2/8) = " + rOne);
}

}

1 个答案:

答案 0 :(得分:0)

  

例如,当你将其中一个设置为1/6和2/8时,当它应该是20/48时,它会出现14/48。我已经尝试重新输入add方法并尝试不同的方式,但它仍然出现在14/48,我不知道为什么

作为您的示例,当我注释掉.equals()方法时,它会返回正确的值。 所以,我检查你的.compareTo()方法并找出,你减少了原始数据。这意味着在比较之后,rOnerTwo的值已被修改。

你应该克隆对象并对它们进行计算

    Rational num1 = new Rational(numerator, denominator);
    Rational num2 = new Rational(other.getNumerator(), other.getDenominator());

我纠正了他们:

  public int compareTo(Rational other) {
  //Should create copy constructor
    Rational num1 = new Rational(numerator, denominator);
    Rational num2 = new Rational(other.getNumerator(), other.getDenominator());
    num1.reduce();
    num2.reduce();

    if (num1.getDenominator() < num2.getDenominator()) {
      return -1;
    } else if (num1.getDenominator() == num2.getDenominator()) {
      if (num1.getNumerator() < num2.getNumerator()) {
        return -1;
      } else if (num1.getNumerator() > num2.getNumerator()) {
        return 1;
      } else {
        return 0;
      }
    } else {
      return 1;
    }
  }

P / s:我只是查看你的例子。所以任何计算器都是错误的,你可以用同样的方法纠正。