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

Write a class names Car that has the following private member variables: •year:

ID: 3637994 • Letter: W

Question


Write a class names Car that has the following private member variables:
•year: An int that holds the car's model year .
•make: A string that holds the make of the car .
•speed: An int that holds the car's current speed.

The class will also have the following public member functions:
•constructor: The constructor should accept the car's year and make as arguments and assign these values to the object's year and make member variables. The constructor should initialize the speed of the car to 0. .
•Accessors: Appropriate accessor functions should be created to allow values to be retrieved from the object's year, make and speed member variables. .
•accelerate: The accelerate function should add 5 to speed member variable each time it is called. .
•brake: the brake function should be subtract 5 from the speed member variable each time it is called..

Demonstrate the class in a program that creates a Car object called Ford, 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 five times. After each call to the brake function, get the current speed of the car and display it.

Explanation / Answer

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 Accelerate() { speed = speed + 5; } public void Brake() { 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); } } }
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