Getting my Accessors to work when creating a new Class.
I've made a new class for a Car. I have the following code for it here. I don't believe I have any issues with the class. However, when I reference the class, I can't get my accelerator to add 5 mph nor can I get my brake to decelerate 5 mph.
Code:
public class Car {
private int yearModel; // Holds the car's year model
private String make; // Holds the make of the car
private int speed; // Holds the car's current speed
// Constructor for Car
Car(int year, String carMake)
{
yearModel = year;
make = carMake;
speed = 0 ;
}
// setyearModel to the Model year
public void setyearModel(int yModel)
{
yearModel = yModel;
}
// setMake to the make of the car
public void setMake (String carMake)
{
make = carMake;
}
// setSpeed to the speed of the car
public void setSpeed(int carSpeed)
{
speed = carSpeed;
}
// returns the year of the model
public int getYearModel()
{
return yearModel;
}
// returns the make of the model
public String getMake ()
{
return make;
}
// returns the speed of the model
public int getSpeed ()
{
return speed;
}
// increase the speed by 5 when accelerated
public void AccelerateSpeed (int speed)
{
speed = speed +5;
}
// decreases the speed by 5 when braked
public void BrakeSpeed ()
{
speed = speed -5;
}
}
Also, my counter isn't working. I want it do accelerate 5 times, and also decelerate 5 times.
Code:
import javax.swing.JOptionPane;
public class Car_Class_Run {
public static void main(String[] args) {
Car myCar = new Car (2007, "Chevy");
int speed = myCar.getSpeed();
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int count = 0; count < 5; count++)
{
System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
" " + "is going " + speed );
myCar.AccelerateSpeed(speed);
System.out.println("Your Speed now is: " + speed );
}
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int count = 0; count < 5; count++)
{
System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
" " + "is going " );
myCar.BrakeSpeed();
System.out.println("Your Speed now is: " + speed);
}
}
}
I've been playing around trying to plug things in here and there with no success. Staring blankly now after several hours and need some help! I'm a beginner, so take it easy on me please! :)