I have an assignment doing a constructor that accelerates and brakes the speed of the car (among other non-troublesome things) but when I run the program, the speed stays constant and spits out whatever the original number is rather than raising and lowering by 5 like I want it to. I will be eternally grateful to whomever can help me see the error of my ways

Oops, I attached by code but it doesn't seem to show. Let's try this
Inline Code Example Here

public class Car
{
    private int yearModel; 
    private String make;
    private int speed;

    public Car (int year, String m, int s) {

        yearModel = year;
        make = m;
        speed = s;
    }
    public int getYearModel(){

        return yearModel;
    }
    public String getMake(){
        return make;
    }
      public int getSpeed(){
        return speed;
    }

    public void accelerate(int speed){
        speed += 5;
    }
    public void brake(int speed){
        speed -= 5;
    }

}
`Inline Code Example Here`
import java.util.Scanner;
       public class ChangeSpeed {
           public static void main(String[] args) {
               int yearModel = 0; 
               String make = null;
               int speed = 0;
               Car car = new Car(yearModel, make, speed);
               Scanner keyboard = new Scanner(System.in);

               System.out.println("What is the year of the car?");
               yearModel = keyboard.nextInt();

               System.out.println("What is the make of the car?");
               make = keyboard.next();

               System.out.println("What speed is the car going?");
               speed = keyboard.nextInt();

               for (int i = 0; i <5; i++){
                   car.accelerate(speed);
                   System.out.println("The car is going " + speed + " MPH.");
                }
                for (int i = 0; i < 5; i++){
                    car.brake(speed);
                    System.out.println("The car is going " + speed + " MPH.");            
               }
    }

}

To me, you have too many speed variables. When you created the new Car in line 39 you passed in the initial speed but I see what looks to be some error in thinking as you have speed in your main, another in your class Car.

So at line 52 it falls apart since the speed is not what's in the Car class. Think it over. No reason to pass in the speed on car.brake or car.accelerate. Also you ignore your class function getSpeed so there's that as well which should have been used in lines 53 and 57.

Thank you rproffitt. You are a life saver

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.