hello i use netbeans and i was working on an example program for a class motorcycle
which goes
Motorcycle = new Motorcycle
in this line the program returned an error that said Motorcycle cant be instantiated. pls what does dis mean
Printable View
hello i use netbeans and i was working on an example program for a class motorcycle
which goes
Motorcycle = new Motorcycle
in this line the program returned an error that said Motorcycle cant be instantiated. pls what does dis mean
When you create a new Object, you need two things.
1) a variable name
2) a constructor
So, if the Motorcycle constructor looked like this:
Code java:public Motorcycle() { ... }
You need to say:
In this, motor is our variable name and Motorcycle() is our call to Motorcycle's constructor that I mentioned above.Code java:Motorcycle motor = new Motorcycle();
Tell me if that helps.
Please post the code for the Motorcycle class. There are a variety of reasons why the Motorcycle class can't be instantiated:
1. The class is either an interface or declared abstract
2. You're using the wrong syntax (as suggested by aussiemcgr)
3. The class has no accessible constructors (for example a private or protected constructor)
Really grateful 4 d assistance it really helped but i got anoda issue wich i dont quite undastand anytime i write a code like
i always get illegal start of expression, and also with something like dis
Code java:void showAtts() {
If you're making a method you have to include private or public BEFORE the return.
Code java:public void showAtts(){} //or private void showAtts(){}
Here is how to set up a method:
Code java:
See this website for more information Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
I don't know why the web page is freaking out. Anyways, all of it depends on what you are trying to do. If I were you I would go through some more tutorials. Here is a good place to start: http://download.oracle.com/javase/tutorial/
I'm assuming what you're trying to do, so please correct me if I am going in the wrong direction.
Code java:public void showAtts(int motorcycleNumber){ if (motorcycleNumber == 1) { //do motorcycle 1 things } if (motorcycleNumber == 2) { //do motorcycle 2 things } //ETC... }
Thanks very much this program is from a text book am reading and it is part of the test programs but each time i write the entire program i dont make any head out of it, i always get error replies. for the showatts i already created the variables indicating the make and color of the motorcycle as follows
Code java:public void showAtts() { System.out.println("This motorcycle is a " + color +" " + make); if (engineState == true) System.out.println("The engine is on."); else System.out.println("The engine is off."); public static void main (String args[]) { Motorcycle motor = new Motorcycle(); motor.make = "Yamaha RZ350"; motor.color = "Yellow"; System.out.println("Calling showAtts..."); motor.showAtts(); System.out.println("........"); System.out.println("Starting engine..."); motor.startEngine(); System.out.println("........"); System.out.println("Calling showAtts..."); motor.showAtts(); System.out.println("........"); System.out.println("Starting engine..."); motor.startEngine(); } }
now for each step i get illegal start of expression and in the case of the showAtts i get symbol not found
i hope this would give u a beta idea of my problem. really appreciate the time
This should work. I strongly suggest that you go through the tutorials in the link I posted earlier.
Code java:public class motorcycle { static String make,color; static boolean engineState; public static void startEngine(){ engineState = true; } public static void showAtts() { System.out.println("This motorcycle is a " + color +" " + make); if (engineState == true) System.out.println("The engine is on."); else System.out.println("The engine is off."); } public static void main (String args[]) { make = "Yamaha RZ350"; color = "Yellow"; System.out.println("Calling showAtts..."); motorcycle.showAtts(); System.out.println("........"); System.out.println("Starting engine..."); motorcycle.startEngine(); System.out.println("........"); System.out.println("Calling showAtts..."); motorcycle.showAtts(); System.out.println("........"); System.out.println("Starting engine..."); motorcycle.startEngine(); } }
REALLY GRATEFUL