views:

125

answers:

5

Here's my updated 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;

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

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

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

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

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

}

class CarDemo{
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();
}

} } }

The application will not display the output. I have no idea what to do. Any advice?

+3  A: 

Java, like most programming languages, is case-sensitive. Class is not the same thing as class.

Tyler McHenry
I added that and still got two errors: reached end of file while parsing } and Compile failed; see the compiler error output for details. BUILD FAILED (total time: 0 seconds)
Arcadia
Looks like you're missing a `}` character at the end of the `CarDemo` class. This is why you should adopt a consistent indentation style rather than the haphazard one that you're using above. It would have been obvious that this class was not closed if you had indented `main` properly.
Tyler McHenry
My code is updated
Arcadia
A: 

I can spot several problems, some of which you may have already fixed:

  1. Class needs to be class (lowercase) in the second class's definition.
  2. Car is not an interface, so you must extend it rather than implement it.
  3. HondaCivic is not abstract or an interface, so it must have method bodies for each of its methods; alternatively, leave the methods out of HondaCivic, in which case Car's methods will be used instead.

In your current class layout, it would make much more sense if made a Honda Civic object rather than a class, as it has no new functionality:

Car hondaCivic = new Car();
R. Bemrose
+1  A: 

Java is case-sensitive:

Class HondaCivic implements Car {

is not the same as the legal syntax:

class HondaCivic implements Car {
Kirk Woll
Fixed that and still got two errors: reached end of file while parsing } and Compile failed; see the compiler error output for details. BUILD FAILED (total time: 0 seconds)
Arcadia
+1  A: 

An interface needs to implement ALL the methods from its parent. You're implementing everything except for

printStates()

Also, check case sensitivity on your class declaration.

edit: nvm its not declared as abstract

Scott
I added that and still got two errors: reached end of file while parsing } and Compile failed; see the compiler error output for details.BUILD FAILED (total time: 0 seconds)
Arcadia
can you post your updated code?
Scott
Yeah, code is updated along with the new errors im getting
Arcadia
+1  A: 

You code has many issues.

First make Car an interface like interface Car

Second Move all the code from HondaCivic to Car and all the code from Car to HondaCivic i.e., swap the code because interface can only have method declarations and variables and not implementation. The class implementing the interface needs to provide the implementation of ALL of these methods.

Lastly in the main method write this code instead of what you have for making instances of Car

Car car1 = new HondaCivic();
Car car2 = new HondaCivic();

Then it will compile and run.

Faisal Feroz
I edited my first post. I tried to do what you said but I can't really understand it. HondaCivic isn't needed for my code to function, I only put it in there because I thought I had to implement it in the interface or something.
Arcadia
A class should have the method body of all of its methods. If you don't have the method body then you will have to declare the method as abstract and as a result also declare the class as abstract as well. Also you cannot create objects of an abstract class. So if all you are trying to do is have a class and create its objects and call some methods of it then you dont have to declare any interface. Just make a class, make sure all the methods have their body and then your code will work fine.
Faisal Feroz
Alright I got the code to compile finally with no errors. One of the main problems was not adding public before the methods. I forgot that you can't compile interface methods that aren't public. However, when I go to run the application it says: java.lang.NoClassDefFoundError: car/Main Caused by: java.lang.ClassNotFoundException: car.Main Could not find the main class: car.Main. Program will exit.Exception in thread "main" Java Result: 1BUILD SUCCESSFUL (total time: 0 seconds) Why is it saying this??
Arcadia
That has to do with your java runtime environment. Make sure you include it in the classpath your IDE is compiling from. If your not using an IDE, make sure JRE is in your classpath.
Scott
I got that error to go away. Now it doesn't want to display the output. Any ideas? I've tried everything I could think of.
Arcadia
Move all the code from CarDemo's main method to the Main class's main method. Then compile your code and run the Main class. the signature of the main method should be `public static void main(String[] args)`. If you can change that in CarDemo then you don't have to move the code. In that case running the CarDemo class should work as well.
Faisal Feroz