Write a class named Car that has the following fields:
yearModel. The yearModel field is an int that holds the car's year model
make. The make field references a String object that holds the make of the car
speed. The speed field is an int that holds the car's current speed
In addition, the class should have the following constructor and other methods:
Constructor. The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to the speed field.
Accessors. Appropriate accessor methods should get the values stored in an object's yearModel,make, and speed field.
Accelerate. The accelerate method should add 5 to the speed field each time it is called
Brake. The brake method should subtract 5 from the sped field each time it is called.
Demonstrate the class in a program that creates a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.
I have looked at these codes several times but do not see my error. My car continuously goes 65...there is not accelerating nor braking.
public class Car {
private int yearModel;
private String make;
private int speed;
// The constructor accept the car's year model and make as argument
// The constuctor should assign 0 to the speed field
public Car (int yrModel, String carMake)
{
yearModel = yrModel;
make = carMake;
speed = 0;
}
public void setyearModel(int yrModel)
{
yearModel = yrModel;
}
public void setMake (String carMake)
{
make = carMake;
}
public void setSpeed(int carSpeed)
{
speed = carSpeed;
}
public int getYearModel()
{
return yearModel;
}
public String getMake ()
{
return make;
}
public int getSpeed ()
{
return speed;
}
public void AccelerateSpeed (int speed)
{
speed = speed + 5;
}
public void BrakeSpeed (int speed)
{
speed = speed - 5;
}
}
]
[/import javax.swing.JOptionPane;
public class MyNewCar {
public static void main(String[] args) {
Car myCar = new Car (2010, "Honda");
int speed = myCar.getSpeed();
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int i = 0; i < 5; i++)
{
System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
" " + "is going " );
myCar.AccelerateSpeed(speed);
System.out.println("Your Speed now is: " + speed);
}
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int i = 0; i < 5; i++)
{
System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
" " + "is going " );
myCar.BrakeSpeed(speed);
System.out.println("Your Speed now is: " + speed);
}
}
}