1

所以我试图在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;
    }
}

//********************* GETTERS *************************** **********

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

//************************ 测试单元 *************************** *********

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

1 回答 1

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 方法。

于 2018-07-09T18:45:27.983 回答