Old thread but I want to share my work with others it might be useful.
The problem was here:
public int accelerate(int ac)
{
ac = ac + 5;
return ac;
}
Because if we already have private int speed we don't need to use another variable int ac, but we should use private int speed, so that method would be implemented like this:
public int accelerate() {
speed += 5;
return speed;
}
Full code:
public class Car {
private int yearModel;
private String make;
private int speed;
public Car(int model, String m) {
yearModel = model;
make = m;
speed = 0;
}
public int getYearModel() {
return yearModel;
}
public String getMake() {
return make;
}
public int getSpeed() {
return speed;
}
public int accelerate() {
speed += 5;
return speed;
}
public int brake(int b) {
b = b - 5;
return b;
}
}
public class CarDemo {
public static void main(String[] args) {
Car c = new Car(1992, "Mustang");
int s = 0;
s = c.getSpeed();
for(int i = 0; i < 5; i++) {
System.out.println("The " + c.getYearModel() + " " + c.getMake() + "is going: " + s);
s = c.accelerate();
System.out.println("Now the " + c.getYearModel() + " " + c.getMake() + "is going: " + s);
}
}
}