0

I have 3 java files in the same folder:

Main.java

package com.company;

public class Main {
    public static void main(String[] args) {
        double a,b,c,d;
        a = Float.parseFloat(args[0]);
        b = Float.parseFloat(args[1]);
        c = Float.parseFloat(args[2]);
        d = Float.parseFloat(args[3]);
        cardano_cubic.solve_cubic(a,b,c,d);
    }
}

cardano_cubic.java

package com.company;

public class cardano_cubic {
    private static Complex square_root(double n){
        double sqrt = Math.sqrt(Math.abs(n));
        return (n<0)?new Complex(sqrt,0):new Complex(0,sqrt);
    }

    public static void solve_cubic(double a, double b, double c, double d){
        System.out.println(
                (a==b&&b==c&&c==d&&d==0)?"0=0":
                (a!=0?a+"x³":"")
                +(b>0?"+"+b+"x²":b<0?b+"x²":"")
                +(c>0?"+"+c+"x":c<0?c+"x":"")
                +(d>0?"+"+d:d<0?d:"")
                +"=0"
        );

        if (a==0 && b==0 && c==0) {
            if (d==0) System.out.println("Решение - все числа");
            else System.out.println("Нет решений");
        }
        else if (a==0 && b==0 && d==0) {
            System.out.println("x1 = " + 0);
        }
        else if (a==0 && b==0) {
            System.out.println("x1 = " + -d/c);
        }
        else if (a==0) {
            Complex D = square_root(c*c-4*b*d);
            Complex x1 = new Complex(-c,0).plus(D);
            x1 = x1.scale(1/(2*b));
            Complex x2 = new Complex(-c,0).minus(D);
            x2 = x2.scale(1/(2*b));

            System.out.println("x1 = " + x1);
            System.out.println("x2 = " + x2);
        } else {
            double Q = (3 * a * c - b * b) / (9 * a * a);
            double R = (9 * a * b * c - 27 * a * a * d - 2 * b * b * b) / (54 * a * a * a);
            double sqrt = Math.sqrt(Q * Q * Q + R * R);
            double S = Math.pow(R + sqrt, 1d/3d);
            double T = Math.pow(R - sqrt, 1d/3d);

            Complex x1 = new Complex(S + T - b / (3 * a), 0);
            double c1 = -(S + T) / 2 - b / (3 * a);
            double c2 = Math.sqrt(3) / 2 * (S - T);
            Complex x2 = new Complex(c1, c2);
            Complex x3 = new Complex(c1, -c2);

            System.out.println("x1 = " + x1);
            System.out.println("x2 = " + x2);
            System.out.println("x3 = " + x3);
        }
    }
}

and Complex.java

package com.company;

public class Complex {
    private double re;
    private double im;

    private double eps = 10e-12;

    public Complex(double real, double imag) {
        re = real;
        im = imag;
    }

    public boolean equals(Object o) {
        Complex other = (Complex) o;
        return re==other.re && im==other.im;
    }

    private String format(double n){
        return String.format("%.2f",n);
    }

    public String toString() {
        if (Math.abs(im)<eps) return format(re);
        if (Math.abs(re)<eps) return format(im) + "i";
        if (im <  0) return format(re) + " - " + format(-im) + "i";
        return format(re) + " + " + format(im) + "i";
    }

    public Complex plus(Complex b) {
        Complex a = this;
        double real = a.re + b.re;
        double imag = a.im + b.im;
        return new Complex(real, imag);
    }

    public Complex minus(Complex b) {
        Complex a = this;
        double real = a.re - b.re;
        double imag = a.im - b.im;
        return new Complex(real, imag);
    }

    public Complex times(Complex b) {
        Complex a = this;
        double real = a.re * b.re - a.im * b.im;
        double imag = a.re * b.im + a.im * b.re;
        return new Complex(real, imag);
    }

    public Complex scale(double alpha) {
        return new Complex(alpha * re, alpha * im);
    }
}

The contents of these files are not that important, what's important is that the Main.java file has a reference to the cardano_cubic.java file and the cardano_cubic.java file has references to the Complex.java file and this runs perfectly fine in and IDE but in order to start using command line arguments it would be more convenient to run this project from a command line

So after using the cd command to navigate to the folder these files are located in and executing the javac Main.java I get this error:

Main.java:10: error: cannot find symbol
        cardano_cubic.solve_cubic(a,b,c,d);
        ^
  symbol:   variable cardano_cubic
  location: class Main
1 error

Which I'm guessing means the java compiler cannot find the cardano_cubic.java file the code from Main.java is referencing to, even though they are in the same folder. How do I properly execute my program from a command line with command line arguments?

ps1nk4
  • 3
  • 2
  • did you compile all your files? – Stultuske Nov 23 '21 at 13:02
  • @Stultuske yes, I did succesfully compile the *Complex.java* file which does not have any references, but running the ```javac``` command for other files with references returns the same error – ps1nk4 Nov 23 '21 at 13:08
  • so, your problem is compiling them, not running them? did you also compile the cardano_cubic class? – Stultuske Nov 23 '21 at 13:12
  • your issue is caused because you are using packages. Check this link for a short explanation and example: https://www.webucator.com/article/how-to-compile-packages-in-java/ – Stultuske Nov 23 '21 at 13:13
  • @Stultuske yes, the problem is compiling them, in my case only *Complex.java* file compiles successfully because it doesn't have references to other java files, compiling the *cardano_cubic.java* file returns the ```cannot find symbol``` error for each line where it references methods from the *Complex.java* file – ps1nk4 Nov 23 '21 at 13:17
  • @ps1nk4 so read my last comment. It points directly to an example – Stultuske Nov 23 '21 at 13:18
  • @Stultuske thanks a lot for the help, the article was really helpful, this solves my initial question – ps1nk4 Nov 23 '21 at 13:30

0 Answers0