Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class named Car that has the following member variables: yearModel: An i

ID: 3841024 • Letter: W

Question

Write a class named Car that has the following member variables: yearModel: An int that holds the car's year model.

make: A string that holds the make of the car.

speed: An int that holds the car's current speed.

In addition, the class should have the following constructor and other member functions.

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 member variables. The constructor should also assign 0 to the speed member variables.

Accessor. Appropriate accessor functions to get the values stored in an object's yearModel, make, and speed member variables.

accelerate. The accelerate function should add 5 to the speed member variable each time it is called.

break. The break function should subtract 5 from the speed member variable each time it is called.

Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function file times. After each call to the break function, get the current speed of the car and display it.

Sample template: // Car class declaration

class Car { private:

public:

// TODO: Constructor

// TODO: Other member functions } int main() {

// TODO: Create a car object.

// TODO: Display the current speed.

// TODO: Accelerate five times.

// TODO: Brake five times.

return 0; }

Kindly, answer the code according to the template given.

Explanation / Answer

Please find my implementation.

######### Car.java #######

public class Car {

   private int yearModel;

   private String make;

   private double speed;

   //constructor

   public Car(int yearModel, String make) {

       this.yearModel = yearModel;

       this.make = make;

       this.speed = 0;

   }

   //Accessors methods

   public int getYearModel() {

       return yearModel;

   }

   public String getMake() {

       return make;

   }

   public double getSpeed() {

       return speed;

   }

   //accelerator

   public void accelerate(){

       speed+=5;

   }

   //break

   public void brake(){

       speed-=5;

   }

}

############# CarDemo.java #########

public class CarDemo {

   public static void main(String[] args) {

       Car c = new Car(2015, "Car-345");

       System.out.println("Current status of the car: ");

       System.out.println("Year: "+c.getYearModel());

       System.out.println("Make: "+c.getMake());

       System.out.println("Speed: "+c.getSpeed());

       System.out.println();

      

       System.out.println(c.getYearModel()+" "+c.getMake()+" Accelerating...");

       c.accelerate();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.accelerate();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.accelerate();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.accelerate();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.accelerate();

       System.out.println("Now the speed is: "+c.getSpeed());

       System.out.println();

       System.out.println(c.getYearModel()+" "+c.getMake()+" Braking...");

       c.brake();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.brake();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.brake();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.brake();

       System.out.println("Now the speed is: "+c.getSpeed());

       c.brake();

       System.out.println("Now the speed is: "+c.getSpeed());

   }

}

/*

Sample run:

Current status of the car:

Year: 2015

Make: Car-345

Speed: 0.0

2015 Car-345 Accelerating...

Now the speed is: 5.0

Now the speed is: 10.0

Now the speed is: 15.0

Now the speed is: 20.0

Now the speed is: 25.0

2015 Car-345 Braking...

Now the speed is: 20.0

Now the speed is: 15.0

Now the speed is: 10.0

Now the speed is: 5.0

Now the speed is: 0.0

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote