Java error: Cannot find symbol (acm)

680 views Asked by At

well,i am trying to create a program that allocates 10000 rational objects without saving any of them in variables so that they all become garbage. After that i have to measure the amount of free memory before and after garbage collection and use the difference to report how many bytes were freed. So far, i have created two .java file. The Rational.java and gc.java. I create the constructor Rational so i can "call" it in my void run method of the gc.java files. The constructor creates a new rational object with the value x/y.

BUT,i get the error: Java error: cannot find symbol

symbol method gcd(Math.abs(x),Math.abs(y));

location class Rational

1 error

Here is my TWO JAVA FILES:

RATIONAL.java

import acm.program.*;

public class Rational {

    public Rational(int x,int y) {

        int g = gcd(Math.abs(x),Math.abs(y));
        int num;
        int den;
        num = x/g;
        den = Math.abs(y)/g;
        if (y<0) {
            num = -num;
        }
    }
}

AND GC.java

import acm.program.*;


public  class gc extends ConsoleProgram {

    public void run() {

        println("Allocating 10000 objects");

        for(int i = 0;i<10000;i++) {
            new Rational(i+1,i+2);
        }

        Runtime myTime = Runtime.getRuntime();
        long a = myTime.freeMemory();
        println(a);
        myTime.gc();
        long b = myTime.freeMemory();
        println(b);
        println((a-b));
    }
}

I can't find were my mistake is,so i can fix it. Your help would be appreciated

problem is,according to the compiler here: int g = gcd(Math.abs(x),Math.abs(y));

EDIT: WORKED!! Just created the gcd method right!

1

There are 1 answers

5
chrylis -cautiouslyoptimistic- On

Yes, because you don't have any method called gcd! Perhaps you're supposed to write one?