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);
        }

    }
}
public void AccelerateSpeed (int speed)
{
speed = speed + 5;
}

This code takes the value of the parameter "speed",adds 5 to it, and returns.
It has no effect on the "speed" defined in the class because the parameter has the same name and "masks" the class variable.
What ois the intended behaviour of this method - should it just add 5 to the car's speed, or should it use the parameter in some way?

Hey Nikki,

There's the problem is on your logic.

myCar.AccelerateSpeed(speed);

You are accelerating the speed that you want EVERYTIME you call it.
So, in your actual class the results go like this.

Send 5 as speed.
When it enters the Accelerate method of your Car class,

public void AccelerateSpeed (int s) //notice I changed your argument variable
{
speed = s + 5;

}

The speed value will not increase because each time you add, it stores the result in the speed variable. Ex. speedInput = 2.

Each time you iterate it's always 2+5 and store it in speed.


Here's a tip. Before you even enter the for loop in your myNewCarClass, you need to "set" the speed variable of the Car class. When you call upon the accelerate method in your for loop (in myNewCarClass), you shouldn't pass any arguments on that method. Instead, in your Car class, change your operation in the "Accelerate method", make it so it adds 5 to the speed variable of the Car class.

I made the suggested change. I set the speed variable. The speed is still not changing. I also did not pass an argument through myCar.AccelerateSpeed(). I got an error.."required: int found: no argument. I give up. I don't see why isn't not working. Thanks for your assistance.

If you define a method as taking an int parameter (which is how your AccelerateSpeed() method is defined) you need to pass it an int when you call the method. And if you re-read James' post, he was saying that you have a variable 'speed' declared at the class level and then you have a different variable 'speed' declared at the method level (the name of your parameter is speed). If you want to refer to the 'speed' variable you declared at the class level (which you do) then use "this.speed".

this.speed = speed + 5;

http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html

If none of this is making sense to you, I suggest going through the beginner java tutorials at the top of the site and coming back with some specific questions about concepts you don't understand.

To me James and Haranaboy are saying two different things. It is clear that I didn't see my error and I still don't. I know you don't do the work for us and it should be that way. Using "this.speed = speed + 5;" didn't accelerate the speed. Since I cannot see my error, I quit. Thanks.

try

public int AccelerateSpeed()
    {
        return speed = (speed += 5);
    }

for the accelerate method which will add 5 to the speed.
I'll let you work on the braking method. : )

hey Jeff, welcome to the forum.
do understand that the posts are over two years old. I doubt that the OP is still looking.

Thanks for the welcome! Yeah I know, but I am just learning Java and I was working on the same problem and hit the same roadblock. My search lead me to the forum (and the unanswered question). So, after a little work I came up with a solution. Just thought I would post it in case any one, like myself, wanders across the post in a search.
Thanks again for the welcome.

Even if this is an old posting, I just figured this out...sooo happy

    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 + i);
       System.out.println("Your Speed now is: " + (speed + i));
    }

    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+i);
       System.out.println("Your Speed now is: " + (speed - i));
    }
}
public class JavaApplication9HW1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


       Car car1 = new Car(89 , "Tempo");

       for (int x = 0; x < 5; x++){

           car1.accelerate();
           System.out.println("The " + (car1.getMake()) + " is traveling @ " + (car1.getSpeed()) + " MPH.");
       }

       for (int x = 0; x < 5; x++){
           car1.brake();
           System.out.println("The " + (car1.getMake()) + " is traveling @ " + (car1.getSpeed()) + " MPH.");
       }
    }

}
class Car{
    int yrModel, speed;
    String make;

    Car(int newYr ,String newMake){
        speed = 0;
        yrModel = newYr;
        make = newMake;
    }

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

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

    public String getMake(){
        return make;
    }

    public int getSpeed(){
         return speed;
     }

    public int getYr(){
        return yrModel;
    }


}

this much trouble for an issue that was solved four years ago ? you didn't even bother to follow basic naming conventions. nice one.

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.