Inheritence Task
Inheritence Task
Task 1:
String make
String model
int year
int speed
A constructor that takes make, model, year as parameters and sets the corresponding fields.
Speed is set to 0.
A toString method that returns the make, model, year, and speed of the vehicle.
Task 2:
int numberOfDoors
Task 3:
int payloadCapacity
Create a copy constructor for the Car class. The copy constructor should take an existing Car object and
create a new Car object with the same make, model, year, speed, and numberOfDoors.
5. Create a method that calculates the total cost of ownership of a vehicle, including the purchase
price, fuel costs, and maintenance costs. Override this method in the derived class to include
additional costs specific to that type of vehicle.
6. Create a method that displays information about a vehicle, including the make, model, year, and
cost of ownership. Override this method in the derived class to display additional information
specific to that type of vehicle.
7. Add an additional derived class that represents a specific type of vehicle, such as a sports car or
an SUV.
8. Implement a static method that calculates the average cost of ownership for all vehicles of a
specific type.
9. Create an array of vehicles and use polymorphism to call the display information method for
each vehicle in the array, regardless of its specific type.
10. Create a method that sorts the array of vehicles based on the cost of ownership, and display the
sorted list of vehicles.
Classes:
Fields:
make: String
model: String
year: int
numWheels: int
Methods:
getMake()
getModel()
getYear()
getNumWheels()
setMake(String make)
setModel(String model)
setYear(int year)
setNumWheels(int numWheels)
toString()
Fields:
numDoors: int
Methods:
getNumDoors()
setNumDoors(int numDoors)
toString()
Fields:
bedLength: double
Methods:
getBedLength()
setBedLength(double bedLength)
toString()
Fields:
numSeats: int
Methods:
getNumSeats()
setNumSeats(int numSeats)
toString()
Task 1: Create the Motorcycle class as a derived class of Vehicle and add the fields and methods
described above.
Task 2: Override the toString() method in the Motorcycle class to display all information about a
Motorcycle object.
Task 3: Create a copy constructor for the Motorcycle class. The copy constructor should create a new
Motorcycle object with the same information as the original.
Task 4: In the driver class, create multiple Motorcycle objects using the default constructor and the copy
constructor. Display all information about each object using the toString() method.
Task 1: Create an extension class named "ElectricVehicle" from the base class "Vehicle". This class
should have the following attributes:
chargingSpeed (double) - the speed in kilowatt-hours per hour at which the vehicle can be
charged
A method named "chargeBattery" that increases the batteryLevel by the chargingSpeed for a
given number of hours
Task 3: Create another extension class named "ElectricCar" from the ElectricVehicle class. This class
should have the following attributes:
A constructor that sets the name, weight, batteryCapacity, numberOfSeats, and model
Override the "displayBatteryLevel" method from the ElectricVehicle class to also display the
number of seats and the model of the car
A method named "drive" that decreases the batteryLevel based on the distance driven, the
speed of the car, and the efficiency of the car's battery
Task 5: Create a driver class named "ElectricCarLab" that creates an instance of the ElectricCar class, sets
its attributes, and demonstrates the use of all the methods in the class hierarchy.