-1

The CalculatorDemo1 class

package calc;
public class CalculatorDemo1{
  public static void main(String args){
    int integer1 = 1;
    int integer2 = 2;
    Calculator a = new Calculator(integer1, integer2);
    a.sum();

  }
}

The Calculator class

package calc;

public class Calculator{
  int left, right;

  public Calculator(int left, int right){
    this.left = left;
    this.right = right;

  }
  public void sum(){
    System.out.println(this.left + this.right);
  }
}

So these codes exist in different files. I tried to compile these two files. One with the constructor compiles, but the other doesn't. And, it spits these errors.

CalculatorDemo1.java:6: error: cannot find symbol
    Calculator a = new Calculator(integer1, integer2);
    ^
  symbol:   class Calculator
  location: class CalculatorDemo1
CalculatorDemo1.java:6: error: cannot find symbol
    Calculator a = new Calculator(integer1, integer2);
                       ^
  symbol:   class Calculator
  location: class CalculatorDemo1
2 errors

I don't think this was the problem of the return type. It didn't fix the problem. Also, I checked my identifiers whether I messed up my spelling or not, but there was not any of those. I didn't find any answer from commonly asked question neither. How should I fix this issue?

Chan
  • 1
  • 1
  • 1
    You've likely not added the Calculator class to the classpath. Try adding it with the `cp` option – user May 26 '20 at 18:04
  • 1
    I'm not sure problem related with @us the problem also related with your constructor declaration – User8500049 May 26 '20 at 18:10
  • What command did you use to compile your code? From which location? – Pshemo May 26 '20 at 20:01
  • javac Calculator.java and javac CalculatorDemo1.java and Calculator.java compiled, but javac CalculatorDemo1.java caused the error above. I fixed the constructor problem a while ago, and it doesn't work still. – Chan May 27 '20 at 00:44
  • 1
    Is current directory of console set to `calc` folder? Compiler (`javac`) needs to know location of *packages* holding classes you are using. You set it by `-classpath` or `-cp` parameter. So if you are inside `calc` you can try with `javac -cp .. CalculatorDemo1.java`, or move one directory above and call `javac -cp . calc/CalculatorDemo1.java`. – Pshemo May 27 '20 at 00:55
  • You are probably doing the build incorrectly. When I do the build correctly the code compiles. (It doesn't run because you still have an error in the code. But you can work that out for yourself when you get to it!) – Stephen C May 27 '20 at 00:56
  • What if I erase package calc, and just put these two files at the same time with a folder called Java? Do I need to mention package necessarily just to make sure they are correlated? – Chan May 27 '20 at 02:15
  • DON"T EVER create classes without package. If you will want to distribute such classes as Jar files and you will want to use that Jar in your other projects, you wouldn't be able to `import` such classes there. – Pshemo May 27 '20 at 06:25

1 Answers1

0

The problem related with your constructor declaration. Constructors don't have any return type. So, your Calculator method is not a constructor. when you try the instantiate,

Calculator c = new Calculator(5,7);//can't find true ctor.
Calculator c1 = new Calculator();//it is ok
public  void Calculator(int left, int right){ //delete void
    this.left = left;
    this.right = right;
}
User8500049
  • 410
  • 3
  • 12
  • I deleted it, but it is still causing a problem. – Chan May 26 '20 at 19:05
  • CalculatorDemo1.java:6: error: cannot find symbol Calculator a = new Calculator(integer1, integer2); ^ symbol: class Calculator location: class CalculatorDemo1 CalculatorDemo1.java:6: error: cannot find symbol Calculator a = new Calculator(integer1, integer2); ^ symbol: class Calculator location: class CalculatorDemo1 2 errors Chanwooui-MBP:java chanwookim$ – Chan May 26 '20 at 19:06