Reputation: 9003
Yesterday I have started learning Java tutorials from Oracle site and I have a problem with the first program (Bicycle :). I have made project with only one class (class Bicycle
), then open new project and wrote the class that creates two Bicycle
objects and invokes their methods. When I try to build project receive error message:
"error: cannot find symbol
Bicycle bike1=new Bicycle();
symbol: class Bicycle
location: class BicycleDemo"
.
I tried right click on Libraries and add Project - didn't work, tried to create new class in current project (with same content) - didn't work. What to do?
package bicycledemo;
/**
*
* App witch simulates using of Bicyle class.
*/
public class BicycleDemo {
import Bicycle;
public static void main(String[] args) {
Bicycle bike1=new Bicycle();
Bicycle bike2=new Bicycle();
bike1.changeCadence(34);
bike1.increaseSpeed(3);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(3);
bike2.increseSpeed(12);
bike2.printStates();
}
}
And I also have whole C:\Users\nojo\Documents\NetBeansProjects\Bicycle file in Libraries of project BicycleDemo. Code of Bicycle.java:
public class Bicycle {
int cadence=0;
int speed=0;
int gear=1;
void changeCadence(int newValue){
cadence=newValue;
}
void increaseSpeed(int increase){
speed=speed+increase;
}
void applyBreaks(int decrease){
speed=speed-decrease;
}
void changeGear(int gearNumber){
gear=gearNumber;
}
void printStates(){
System.out.println("cadence:" + cadence + "speed:" + speed +
"gear:" + gear);
}
}
Upvotes: 1
Views: 12266
Reputation: 2503
Looks like your import statement is in the wrong location. It should be below the package name and before the the beginning of the clas definition.
package bicycledemo;
import <yourpackagename>.Bicycle;
You can do this where you are currently declaring bike1 but you have to use thepackage name and class name when you do.
<yourpackagename>.Bicycle bike1 = new Bicycle();
What you're reading is a tutorial on the "concepts" of OO programming and not an in depth tutorial, Packages are explained further along in the tutorial.
Your problem is probably you made two projects, one has the bicycle class and one has the bicycledemo class, correct? If that is correct then in both projects your class is in the default package, which is bad. To fix your problem, create a new project with both classes in the same project.
Upvotes: 2