1

so I am trying to build this ADT for rational numbers in java but for some reason I keep getting this error that says cannot find symbol when trying to compile. What em I doing wrong? Is this error because of my syntax?

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;

// ****************** CONSTRUCTOR **********************************

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

// ******************* OPERATIONS **********************************

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

    }

//********************* TOOLS **************************************

//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);
    }
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
Juan Suarez
  • 31
  • 1
  • 3

1 Answers1

0

In the below piece of code, you didn't declare local variable v.

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

 return a && v;
}

getNumerator and getDenominator are methods. Call them as this.getNumerator() and this.getDenominator().

Also make sure Rational class is having getNumerator and getDenominator methods.

sree
  • 63
  • 6