java语法帮助ADT合理实现[错误:找不到符号]

时间:2018-01-30 17:52:02

标签: java rational-number

所以我试图在java中为有理数建立这个ADT但是由于某种原因我不断得到这个错误,说在尝试编译时找不到符号。我做错了什么?这是因为我的语法错误吗?

Author: Juan Suarez
// Class : CS1102 ~ java
// Date  : 01/30/2018
// Topic : This porblem set focuse on the implemantation of an
//         ADT for rational numbers.

public class RationalC implements Rational {

private int num;
private int den;

// ******************建设者************************** ********

public RationalC (int numerator, int denominator) {
    if (this.den == 0){
        throw new ArithmeticException("*** WARNING! input non zero denominator");
    }
    int reduceFraction = gcd(numerator, denominator);
    this.num = numerator / reduceFraction;
    this.den = denominator / reduceFraction;

    if (this.den < 0) {
        this.den = this.den * -1;
        this.num = this.num * -1;
    }
}

// ********************* GITTERS *********************** *************

public int getNumerator() { return this.num; }
public int getDenominator() { return this.den; }

public boolean equal(Rational b) {
  boolean
        a = this.getNumerator == b.getNumerator;
        v = this.getDenominator == b.getDenominator;

    return a && v;
}

// *******************营运************************* *********

//return this + that
//
public RationalC plus(Rational b) {
    int commonDenominator = this.getDenominator() * b.getDenominator();
    int num1 = b.getDenominator() * this.getNumerator();
    int num2 = b.getNumerator() * this.getDenominator();
    int complete = num1 + num2;

    return new RationalC (complete, commonDenominator);
}
//returns this - that
//
public RationalC subtract(Rational b) {
    int commonDenominator = this.getDenominator() * b.getDenominator();
    int num1 = b.getDenominator() * this.getNumerator();
    int num2 = b.getNumerator() * this.getDenominator();
    int complete = num1 - num2;

    return new RationalC (complete, commonDenominator);

}
// return this * that
//
public Rational multiply(Rational b){
    int top = this.getNumerator() * b.getNumerator();
    int bottom = this.getDenominator() * b.getDenominator();

    return new RationalC (top, bottom);

}
//return this / that
//
public Rational divide(Rational b){
    int top = this.getNumerator() * b.getDenominator();
    int bottom = this.getDenominator() * b.getNumerator();

    return new RationalC (top, bottom);

}
//retuns value
//
public boolean equals(Rational b) {
    if (num == b.getNumerator() && this.getDenominator() == b.getDenominator() ) 
return(true);

    }

// *********************工具*********************** ***************

//returns the rational number to a string
//
    public String toString() {
        return "(" + this.num + "," + this.den + ")";

    }

//returns -1 , 0, +1 if the value of the rational is <, >, or =
//
    public int compareTo(Rational b) {
    long leftHand = this.getNumerator() * b.getDenominator();
    long rightHand = this.getDenominator() * b.getNumerator();
    if (leftHand < rightHand) return -1;
    if (leftHand > rightHand) return +1;
    return 0;
}
    private static int gcd(int m, int n) {
    if(m < 0) m = -m;
    if(n < 0) n = -n;
    return m * (n / gcd(m,n));

}
        public Rational reciprical(Rational b){
        return new RationalC (this.getDenominator(), this.getNumerator() );
    }

// ******************* TEST UNIT ************************ ************

    public static void main(String[] args) {
        x = new Rational (1,2);
        y = new Rational (1,3);
        z = x.plus(y);
        StdOut.println(z);
    }
}

1 个答案:

答案 0 :(得分:0)

在下面的代码中,您没有声明局部变量v。

public boolean equal(Rational b) {
 boolean
    a = this.getNumerator == b.getNumerator;
    v = this.getDenominator == b.getDenominator;

 return a && v;
}

getNumerator和getDenominator是方法。 称它们为this.getNumerator()和this.getDenominator()。

还要确保Rational类具有getNumerator和getDenominator方法。