For C++ -- Create a class called Auto. Class Auto has three private member varia
ID: 3739141 • Letter: F
Question
For C++ --
Create a class called Auto. Class Auto has three private member variables. string make, string model and double price. Class Auto has a parametrized constructor that accepts arguments for make, model and price and sets the values of the three member variables, make, model and price. Class Auto has three accessor member functions getmake(), getmodel() and getprice() that return the values of make, model and price respectively.
Class Car inherits from class Auto. Class car has one private member, int doors. Car class has a parametrized constructor that accepts arguments for make, model, price and doors and passes make, model, price to base class constructor and sets the value of member variable doors. Class Car has one public member function getdoors(0 that returns the number of doors the car has. It also has a public function double bookvalue(int years), that accepts an argument for number of years the car is old and returns the depreciated value (book value) of the car by reducing price of the car by 7% for each year the car has been in existence.
Class Truck inherits from class Auto and has one private member variable, capacity. It has a parametrized constructor that accepts arguments for make, model, price and capacity and passes make, model, price to base class constructor and sets the value of member variable capacity (measured in tons). Class Truck has a public member function getcapacity() that returns the capacity of the truck.
In a client program, create a car object and a truck object. Display the make, model, price (and doors and book value for car object) and (capacity for the truck object).
Explanation / Answer
Auto.java
public class Auto {
//Declaring instance variables
private String make;
private String model;
private double price;
//Parameterized constructor
public Auto(String make, String model, double price) {
this.make = make;
this.model = model;
this.price = price;
}
// getters and setters
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Make = " + make + ", Model = " + model + ", price = " + price;
}
}
_____________________
Car.java
public class Car extends Auto {
//Declaring instance variables
private int doors;
//Parameterized constructor
public Car(String make, String model, double price, int doors) {
super(make, model, price);
this.doors = doors;
}
// getters and setters
public int getDoors() {
return doors;
}
public void setDoors(int doors) {
this.doors = doors;
}
public double bookvalue(int years) {
return getPrice()-(getPrice() * years * 0.07);
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Car "+super.toString()+"Doors = " + doors;
}
}
___________________
Truck.java
public class Truck extends Auto {
//Declaring instance variables
private int capacity;
//Parameterized constructor
public Truck(String make, String model, double price, int capacity) {
super(make, model, price);
this.capacity = capacity;
}
//getters and setters
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Truck "+super.toString()+" Capacity=" + capacity;
}
}
______________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an Instance of Car class
Car c=new Car("Tata","Nano",2000,4);
//Creating an Instance of Truck class
Truck t=new Truck("Tata","Ace",5000,5);
System.out.println(c);
System.out.println(t);
}
}
__________________
Output:
Car Make = Tata, Model = Nano, price = 2000.0Doors = 4
Truck Make = Tata, Model = Ace, price = 5000.0 Capacity=5
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.