views:

91

answers:

5

Possible Duplicate:
Java application doesn't display output

I am learning the basics of Java development so that I can get into Android development. In this very simple application I'm running into some problems. It's supposed to give me an output of the cars speed, gears, rpm and so forth. I can compile the application no problem but when I go to run it it doesn't display the output. What could the problem be? Here's the code:

package car1;

public class Main {



    public static void main(String[] args) {

            class HondaCivic implements car1 {

                int speed = 0;
                int rpm = 0;
                int gear = 1;

                void speedUp(int Increment) {
                    speed = speed + Increment;}

                void applyBrakes(int Decrement) {
                    speed = speed - Decrement;}

                void changeRpm(int NewValue) {
                    rpm = NewValue;}

                void changeGear(int NewValue) {
                    gear = NewValue;}

                void printStates() {
                    System.out.println("speed:"+speed+" rpm:"+rpm+" gear:"+gear);
                }
            }


            class Car1{
                public void main(String[] args) {
                    // Two different Cars
                    HondaCivic car1 = new HondaCivic();
                    HondaCivic car2 = new HondaCivic();
                    // Methods for cars
                    car1.speedUp(30);
                    car1.changeGear(3);
                    car1.changeRpm(3000);
                    car1.applyBrakes(15);
                    car1.printStates();

                    car2.speedUp(30);
                    car2.changeGear(3);
                    car2.changeRpm(2000);
                    car2.applyBrakes(15);
                    car2.speedUp(5);
                    car2.changeGear(1);
                    car2.printStates();
                }

            }
        }
    }
}
A: 

You can't compile.

Your HondaCivic class implements car1 which doesn't exists. It could be Car1 but Car1 is a class and should be extended.

Plus having two classes declared in your main method isn't really a good idea. You can't access them from outside the method.

And in the end you have two main(String...) method. I suppose that it's the Main.main(String...) that is called. This method does nothing except declaring two classes.

Colin Hebert
Compiling actually works.
Arcadia
What do you mean it should be extended? I have the Car1 interface in another java file. I can compile, it says build successful.
Arcadia
Well My bad, I supposed that your `car1` class in `main()` was the interface you were refering to. But still read the last part about the `main()` called.
Colin Hebert
If I don't have both main() instances the file won't compile for some reason and has errors everywhere. How do I get this to display the output?
Arcadia
Get your classes out of the main() method. And get the content of your second main method into the first one. Then start your application with `java car1.Main`.
Colin Hebert
A: 

HondaCivic in this case should not be a class, it should be an instance of a Car or any subclass of car. The Car Class should have the make, year, model etc of the car. Now if there is different behavior for say a HondaCivic, you would add any information that would help define this, for example max_speed, or like you did the state of the cars systems such as current_speed, brakes, gear, etc.

Now if you want to go further in the design you could create a class for the different parts of a car and you start seeing how this fits nicely in object oriented design.

mrjohn
Thanks but I'm not trying to create a huge application. I'm just trying to get the basics down and for some reason this application won't display the output which is the printStates(). Any idea? The code is all good I just can't get the numbers to come up in the output.
Arcadia
`The code is all good` No it's not. That's the worst code I've ever seen. You have main implementing a class that implements main? Holy crap that's awful. and `but I'm not trying to create a huge application` of course not, but you should at least write correct code. Don't use interfaces, external classes, nested classes, or anything else that's too complicated.
Falmarri
@Arcadia I agree with Falmarri, please do not think we are to critical, but this is a great place to learn from each other. Simple also mean making it understandable and a focus on using the right approach. If you develop with that in mind you will also make better decisions when the application is really large.
mrjohn
I took almost the exact same code found from this tutorial: http://download.oracle.com/javase/tutorial/java/concepts/class.html At the end of the tutorial it tells me to make my own application based off of that lesson. It's almost exactly like the code they posted. Even when I take their code and try to run it the output doesn't come up, but in the lesson they claim it does. How am I supposed to implement my code? Should it not be in a main class?
Arcadia
I got it to work, thanks for the help guys.
Arcadia
A: 

This is what your classes should look like

HondaCivic.java


public class HondaCivic  {

                int speed = 0;
                int rpm = 0;
                int gear = 1;

                void speedUp(int Increment) {
                    speed = speed + Increment;}

                void applyBrakes(int Decrement) {
                    speed = speed - Decrement;}

                void changeRpm(int NewValue) {
                    rpm = NewValue;}

                void changeGear(int NewValue) {
                    gear = NewValue;}

                void printStates() {
                    System.out.println("speed:"+speed+" rpm:"+rpm+" gear:"+gear);
                }
            }

Main.java


public class Main {

    public static void main(String[] args) {


    car1 = new HondaCivic();
    car2 = new HondaCivic();
    car1.speedUp(30);
    car1.changeGear(3);
    car1.changeRpm(3000);
    car1.applyBrakes(15);
    car1.printStates();

    car2.speedUp(30);
    car2.changeGear(3);
    car2.changeRpm(2000);
    car2.applyBrakes(15);
    car2.speedUp(5);
    car2.changeGear(1);
    car2.printStates();

     }
}
Falmarri
I'm so mad man. I spent like over an hour on something so simple lol. I just don't have a full understanding on how things should be laid out and formatted. Your code worked, but I had to add packages of course. And the public before class isn't needed for some reason.
Arcadia
The public isn't necessary if the classes are in the same package. The default modifier is `protected` which means it's accessible from any class in your package. This is the same for variables and such too. You should find a good ground up tutorial of java since you're clearly missing a LOT of the basics
Falmarri
Well I actually only began studying Java 2 days ago. :P I'm reading the Java tutorials from the Java website. I've barely scratched the surface. As I keep advancing I'm sure I'll be able to grasp these concepts better.
Arcadia
Find a tutorial for java for new programmers. The tutorials from the java site are probably geared towards existing programmers trying to learn java
Falmarri
They're actually built for people who have no idea what the heck Java is: http://download.oracle.com/javase/tutorial/ They start from the absolute beginning as if the reader doesn't know what Java is. Apparently it's the same stuff found in the book called "The Java Tutorial". Hopefully after I finish all of these lessons I'll have enough to continue Android development.
Arcadia
What's with the -1? Also Arcadia: if this answer was helpful to you, you might want to consider accepting it =]
Falmarri
A: 

I don't know, but I think you could try using the Log to test your output

Just replace

System.out.println("speed:"+speed+" rpm:"+rpm+" gear:"+gear);

by

Log.i("OUTPUT", "speed:"+speed+" rpm:"+rpm+" gear:"+gear);

And look at the LogCat window

That is, if you are using Eclipse.

+1  A: 

Your Main.main method doesn't contain any actual executable code. All it has is an embedded class definition, HondaCivic. It looks like your intent is that the code inside the Car1.main method is what should be inside the Main.main method -- that looks like what you really want to run. There's also no apparent reason for HondaCivic to be an embedded class. Clean that up and you basically have what Falmarri suggests.

Jay