aspose file tools*
The moose likes Beginning Java and the fly likes It compiled but couldn't  run,help me make it run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "It compiled but couldn Watch "It compiled but couldn New topic
Author

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

Abbey Samuel
Greenhorn

Joined: May 02, 2007
Posts: 15
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

Joined: Oct 02, 2003
Posts: 11418
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dinesh Arora
Greenhorn

Joined: Apr 18, 2007
Posts: 21
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

Joined: May 02, 2007
Posts: 15
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: It compiled but couldn't run,help me make it run