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?
Java, like most programming languages, is case-sensitive. Class
is not the same thing as class
.
Java is case-sensitive:
Class HondaCivic implements Car {
is not the same as the legal syntax:
class HondaCivic implements Car {
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
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.
I can spot several problems, some of which you may have already fixed:
Class
needs to be class
(lowercase) in the second class's definition.
Car
is not an interface, so you must extend
it rather than implement it.
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();