• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

It compiled but couldn't run,help me make it run

 
Abbey Samuel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know wha i can do or what to add to it to make it run,please help me to make it run.
import java.lang.*;
class Bicycle{
int cadence=0;
int speed=0;
int gear=1;
void changeCadence(int newValue) {
cadence=newValue;
}
void changeGear(int newValue) {
gear=newValue;
}
void speedUp(int increment) {
speed=speed+increment;
}
void applyBrakes(int decrement) {
speed=speed-decrement;
}
void printStates() {
System.out.println("cadence:"+cadence+"speed:"+speed+"gear:"+gear);
}
}
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you have here is a class. you wrote a class Bicycle, that has several member variables (cadence, speed, gear). you have several methods that can be called to changs those variables (changeCadence(), changeGear(), speedUp(), applyBrakes(). And you have a method to print all those member variables (printStates()).

the question is, what do you WANT your program to do? What is your goal?

What you don't have is a program to DO anything. Generally, you would write a main() method. in that method, you would create a Bicycle object, and assign it to a reference. you could then call your various methods to show they are working.

do you know how to do that?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abbey:
I want to know wha i can do or what to add to it to make it run,please help me to make it run...

You need to run the BicycleDemo class. This contains the main method that Fred describes. Or as the tutorial explains...
...it's not a complete application; it's just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.

[ June 06, 2007: Message edited by: marc weber ]
 
Dinesh Arora
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You have created and implemented correctly. The code would compile but when you run you won't get anything, since there is no main method. Whenever you try to run something JVM looks for public static void main(String[] args){}. Unless you implement that, you won't get anything.

Add this as well,

public class RunBicycle{
public static void main (String [] args){
Bicycle b = new Bicycle();
// Now givimg new values to its class variables
b.changeCadence(5);
b.changeGear(6);
b.speedUp(7);
b.applyBrakes(3);

// Now printing the values
b.printStates();
}
}

NOTE: Save the file as RunBicycle.java and compile it as RunBicycle.java and execute as java RunBicycle.


Thanks,
Dinesh
 
Abbey Samuel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the BicycleDemo too is not compiling here is the code and below it is the error it brought forward
import java.lang.*;
class BicycleDemo {
public static void main(String[] args) {

// Create two different Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();

// Invoke methods on those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();

bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}


C:\Documents and Settings\User\My Documents\BicycleDemo.java:6: cannot find symbol
symbol : class Bicycle
location: class BicycleDemo
Bicycle bike1 = new Bicycle();
^
C:\Documents and Settings\User\My Documents\BicycleDemo.java:6: cannot find symbol
symbol : class Bicycle
location: class BicycleDemo
Bicycle bike1 = new Bicycle();
^
C:\Documents and Settings\User\My Documents\BicycleDemo.java:7: cannot find symbol
symbol : class Bicycle
location: class BicycleDemo
Bicycle bike2 = new Bicycle();
^
C:\Documents and Settings\User\My Documents\BicycleDemo.java:7: cannot find symbol
symbol : class Bicycle
location: class BicycleDemo
Bicycle bike2 = new Bicycle();
^
4 errors

Tool completed with exit code 1
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Documents and Settings\User\My Documents\BicycleDemo.java:6: cannot find symbol
symbol : class Bicycle


Please read the error message and try to understand what it says and why it's happening. This error message tells you that Java can't find class Bicycle. You should have Bicycle.java in the same directory as BicycleDemo.java. Do you have both files in the directory C:\Documents and Settings\User\My Documents?
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As suggested in the above post, you need to have both the files in the same directory,in Java terms, in the same package.
If you dont have that already, or if you dont wish to have it that way, then you need to set the classpath variable to the appropriate path.

Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic