I am trying to re-learn some programming skills.
I have decided to use an old favourite - Logic Gates Simulation - as a learning tool.
I want to set this project up to use packages.
My CLASSPATH is "C:\Users\ruthm\Documents\java"
My project code is in the directory:
C:\Users\ruthm\Documents\java\logic
I am using Java 11
My classes so far are Connector.java and ConnectorTest.java
the code for Connector is as follows:
package logic;
/*
Class Connector
A connector forms the input and output of a LogicGate.
The connector "output" object of a gate can be passed to another gate to form the input.
*/
public class Connector{
private int value;
public Connector(){
value=0;
}
public Connector(int state){
value=state;
}
public void setValue(int state){
value=state;
}
public int getValue(){
return value;
}
}
The code for Connector Test is as follows:
/* Test Case for Connector */
import logic.*;
class ConnectorTest{
public static void main (String[] args){
logic.Connector myConnector = new logic.Connector();
System.out.println("initial value: "+myConnector.getValue());
myConnector.setValue(1);
System.out.println("Set value: "+myConnector.getValue());
}
}
Connector.java compiles without error. When I try to compile ConnectorTest.java I get the following from the compiler:
C:\Users\ruthm\Documents\java\logic>javac ConnectorTest.java
ConnectorTest.java:4: error: package logic does not exist
import logic.*;
^
ConnectorTest.java:9: error: package logic does not exist
logic.Connector myConnector = new logic.Connector();
^
ConnectorTest.java:9: error: package logic does not exist
logic.Connector myConnector = new logic.Connector();
^
3 errors
C:\Users\ruthm\Documents\java\logic>
I have been following guides on directory structure and packages to try and solve this but I am clearly not understanding something.
I get the same errors if I declare ConnectorTest to be in package logic as well.
Can someone handhold me and show me where I am going wrong?
My project code is in the directory:
C:\Users\ruthm\Documents\java\logic
This means that to avoid trouble, you'd be better off using same package directive to them both.
So if you add this:
package logic;
to your ConnectorTest class, it should be ok, given you don't have any other issues.
If you want to keep your ConnectorTest class in default package, you could move your ConnectorTest.java file to C:\Users\ruthm\Documents\java directory, but keep your Connector class in the logic directory.