1

I'm working on a program that estimates pi using the Ramanujan series. In the series, it requires me to calculate the factorial of a couple expressions, which I have a class written for. I had used the class for a previous assignment, explaining the tests at the top of the class. My issue comes into play when I try to create the Factorial object to use Factorial.calculate(nthNum). I've tried everything I can think of, searched many forums, went back over my notes, and I cannot figure out why this error keeps happening.

.\Ramanujan.java:9: error: cannot find symbol
        Factorial f = new Factorial();
        ^
  symbol:   class Factorial
  location: class Ramanujan
.\Ramanujan.java:9: error: cannot find symbol
        Factorial f = new Factorial();
                          ^
  symbol:   class Factorial
  location: class Ramanujan
2 errors
error: compilation failed

If someone could explain to me why the symbol cannot be found, it would be very helpful. Thank you.

public class Ramanujan {
    public static void main (String[] args){
        String nthRamNumString = args[0];
        int nthRamNum = Integer.parseInt(nthRamNumString);
        findRamNum(nthRamNum);
    }

    public static double findRamNum(int nthNum){
        Factorial f = new Factorial();
        double factNum = f.calculate(nthNum);
        double firstVal = ((2 * Math.sqrt(2)) / 9801);
        double piNum = 0;
        for (int i = 0; i <= nthNum; i++){
            piNum = piNum + (4 * factNum) * (1103 + (26390 * nthNum)) / 
            (Math.pow(factNum, 4) * (Math.pow(396, 4 * nthNum)));
        }
        double finalPiVal = (firstVal * piNum);
        return 1 / finalPiVal;
    }
}

public class Factorial {
    public static void main (String[] args){
        String value = args[0];
        Long n = Long.parseLong(value);
        if (n > 20){
            System.out.println("Value must be less than 20!");
            System.exit(0);
        }
        else if (n < 0){
            System.out.println("Value must be greater than 1!");
            System.exit(0);
        }
        else{
            System.out.println(calculate(n));
        }
        Long result = calculate(0);
        if (result == 1){
            System.out.println("Factorio.calculate(0) returned " + result + ". Test passed!");
        }
        else{
            System.out.println("Factorio.calculate(0) returned " + result + ". Test failed!");
        }
        result = calculate(5);
        if (result == 120){
            System.out.print("Factorio.calculate(5) returned " + result + ". Test passed!");
        }
        else{
            System.out.print("Factorio.calculate(5) returned " + result + ". Test failed!");
        }
    }

    public static long calculate(long n){
        long factNum = 0;
        if (n == 0){
            return 1;
        }
        else{
            for (int i = 0; i <= n; i++){
                factNum = factNum + (n * (n - 1));
            }
        }
        return factNum;
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
bruh3436
  • 49
  • 3
  • @Abra Not really. I read through all the reasons of why I might be getting this error but I cant seem to find the reason in this case. – bruh3436 Feb 14 '23 at 04:47
  • 1
    Have you imported `Factorial` class in `Ramanujan.java`? Please also specify your IDE and java compiler you are using. Also put the screenshots of the classes, package explorer etc. Because at first it doesn't sound like a javac problem. There can also be something wrong in IDE settings. – Aqeel Ashiq Feb 14 '23 at 04:53
  • 1
    Is class `Ramanujan` in the [default package](https://stackoverflow.com/questions/2335211/what-is-the-default-package-in-which-my-classes-are-put-if-i-dont-specify-it) or in the [unnamed module](https://stackoverflow.com/questions/46263256/how-many-unnamed-modules-are-created-in-java-9)? Likewise for class `Factorial`? What Java version are you using? What IDE are you using? How have you configured the build path in the Java project in your IDE? – Abra Feb 14 '23 at 05:02
  • @Abra I am just using VSCODE text editor. The java version is the latest version, I recently updated. The Ramanujan class has not been touched. I simply went into a new folder for my homework and created a new file called Ramanujan.java. Same for the Factorial class. – bruh3436 Feb 14 '23 at 05:04
  • _The java version is the latest version,_ Not really what I asked. Does that version have a number? Is that number 19? Do you know how to find out the version number of the Java version that you are using? _I simply went into a new folder_ That doesn't answer my question. I can't help you unless you provide more details by answering my questions. If you don't kmow whether your classes are in the default package or the unnamed module, then say so. Nonetheless this seems more a VS Code problem than a Java problem. Consider using a real Java IDE like IntelliJ IDEA or Eclipse. – Abra Feb 14 '23 at 05:19

1 Answers1

0

I forgot to compile the code :(. Thanks everyone for the help.

bruh3436
  • 49
  • 3