Question

In: Computer Science

Car Class Write a class named Car that has the following member variables: • year. An...

Car Class
Write a class named Car that has the following member variables:
• year. An int that holds the car’s model year.
• make. A string object 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 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 member variable to 0.
• Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object’s year, make, and speed member variables.
• accelerate. The accelerate function should add 5 to the speed member variable each time it is called.
• brake. The brake 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 five times. After each call to the brake function, get the current speed of the car and display it.

Mimir Requirement: The file name must be: Homework6A.cpp

Test Case:

1967 Ford Mustang - current speed : 5
1967 Ford Mustang - current speed : 10
1967 Ford Mustang - current speed : 15
1967 Ford Mustang - current speed : 20
1967 Ford Mustang - current speed : 25

1967 Ford Mustang - current speed : 20
1967 Ford Mustang - current speed : 15
1967 Ford Mustang - current speed : 10
1967 Ford Mustang - current speed : 5
1967 Ford Mustang - current speed : 0

Solutions

Expert Solution

Code

public class Car{
    // member variables
    private String make;
    private int year,speed;
    // constructor
    public Car(String mak,int y){
        make = mak;
        year = y;
        speed = 0;
    }
    // accessor to access values
    public String getMake(){
        return make;
    }
    public int getYear(){
        return year;
    }
    public int getSpeed(){
        return speed;
    }
    // accelerate function 
    public void accelerate(){
        speed = speed + 5; // add 5 to speed
    }
    // brake function
    public void brake(){
        speed = speed - 5; // subtract 5 from speed
    }

    public static void main(String []args){
        // create a car object
        Car car = new Car("Ford Mustang",1967);
        // call accelerate 5 times and show details
        for(int i=0;i<5;i++){
            car.accelerate();
            System.out.println(car.getYear()+" "+car.getMake()+" - current speed : "+car.getSpeed());
        }
        // call brake 5 times
        for(int i=0;i<5;i++){
            car.brake();
            System.out.println(car.getYear()+" "+car.getMake()+" - current speed : "+car.getSpeed());
        }
        
    }
}

Code screenshot:

Code output:

=========================

The code for the class Car along with methods mentioned have been implemented above. Comments have been added to help you understand.


Related Solutions

write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName...
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData, which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool. It will...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions Complex(); Complex(double, double); Complex add(Complex num1, Complex num2); Complex subtract(Complex num1, Complex num2); string printComplexNumber(); Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to...
Problem Statement Design a class named Car that has the following fields: year: The year field...
Problem Statement Design a class named Car that has the following fields: year: The year field is an integer that holds the car’s year. make: the make field is a String that holds the make of the car. speed: the speed field is an integer 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 parameters. These values should be...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
/*Design and code a class Calculator that has the following * tow integer member variables, num1...
/*Design and code a class Calculator that has the following * tow integer member variables, num1 and num2. * - a method to display the sum of the two integers * - a method to display the product * - a method to display the difference * - a method to display the quotient * - a method to display the modulo (num1%num2) * - a method toString() to return num1 and num2 in a string * - Test your...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT