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: 3637992 • 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

// the main method public class Driver { public static void main(String[] args) { Car Ford = new Car(2012, "Mustang"); // create a car object of 2012 Ford mustang // lets make this simple :) and loop it for ( int i = 0; i < 5; i++ ) { Ford.Accelerate(); // increment speed by 5 System.out.println("Current speed is: " + Ford.GetSpeed() + "mph At iteration: " + i); } // now lets brake 5 times for ( int j = 0; j < 5; j++ ){ Ford.Brake(); // brake. decrement by 5 System.out.println("Current speed is: " + Ford.GetSpeed() + "mph At iteration: " + j); } // you dont have to loop this. but your prof might be impressed. :) // otherwise you will have to do this one line at a time eg. // Ford.Accelerate(); //+5 // System.out.println(Ford.GetSpeed()); // repeat x4 :) } } // the Car class ////////////////////////////////////////// public class Car { // the global variables private int year; private String make; private int speed; /** * The constructor that takes 2 args * @param yr * @param mke */ public Car(int yr, String mke) { this.year = yr; this.make = mke; this.speed = 0; } /** * Gets the Year of the car * @return */ public int GetYear() { return year; } /** * Sets the year of the car * @param yr */ public void SetYear(int yr) { year = yr; } /** * Gets the make of the car * @return */ public String GetMake() { return make; } /** * Sets the make of the car * @param mke */ public void SetMake(String mke) { make = mke; } /** * Gets the speed of the car * @return */ public int GetSpeed() { return speed; } /** * Sets the speed of the car * @param spd */ public void SetSpeed(int spd) { speed = spd; } /** * increases the speed of the car by 5 */ public void Accelerate() { speed += 5; } /** * decreases the speed of the car by 5 */ public void Brake() { speed -= 5; } }
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