I am to create a class that creates an object (car). As it speeds up I am to display the speed. there is no problem that part works fine. The problem is when it slows down I am to display the speed just as I did when it speed up. However for reason or another It does not go into this loop. Any ideas on what I am doing wrong? Thanks
CODE
// demonstrate the class in a program that creates a car
import java.util.Scanner;
public class classDemoMcKee
{
public static void main(String[] args)
{
carMckee carCreate;
int yr;
String make;
int speed;
int current = 0;
Scanner keyboard = new Scanner(System.in);
// get year
System.out.println("What year is your vehicle?");
yr = keyboard.nextInt();
keyboard.nextLine();
// get make
System.out.println("What is the make of your vehicle?");
make = keyboard.nextLine();
System.out.println();
carCreate = new carMckee(yr, make);
System.out.println("We will now accelerate to 25 mph. Displaying the "
+ "speed every 5 mph.");
System.out.println();
//loop to speed up
for (speed = 0; speed < 5; speed++)
{
System.out.println("Your car's speed is " + carCreate.getSpeed() + " mph.");
System.out.println();
carCreate.accelerate();
}
current = carCreate.getSpeed();
System.out.println("You are currently going " + current + " mph.");
System.out.println();
System.out.println("Let's slow down. I will display your speed as you slow down just "
+ "as I did when you speed up.");
System.out.println();
//loop to slow down
for (speed = 5; speed > 0; speed--)
{
System.out.println("Your car's speed is " + carCreate.getBrake() + " mph.");
System.out.println();
carCreate.getBrake();
}
current = carCreate.getBrake();
System.out.println("Your speed is " + speed + " you should put the vehicle in park.");
System.out.println();
System.out.println("How did you like driving your "
+ yr
+ " "
+ make);
}
}
Here is on file, this is the other.
CODE
public class carMckee
{
private int speed;
private int yearModel;
private int make;
public carMckee(int yr, String mk)
{
yearModel = yr; // year of car
String make = mk; // make of car
speed = 0; // speed of car
}
public int getSpeed()
{
return speed;
}
public int getBrake()
{
return speed;
}
public int yearModel()
{
return yearModel;
}
public int make()
{
return make;
}
public void accelerate()
{
speed += 5;
}
public void slow()
{
speed -= 5;
}
}