0% found this document useful (0 votes)
3 views

Inheritence Task

Uploaded by

fwtn4bgpcr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Inheritence Task

Uploaded by

fwtn4bgpcr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Inheritance Assignment

Task 1:

Create a base class "Vehicle" with the following fields:

 String make

 String model

 int year

 int speed

And the following methods:

 A constructor that takes make, model, year as parameters and sets the corresponding fields.
Speed is set to 0.

 A method called "accelerate" that increases the speed by 10.

 A method called "brake" that decreases the speed by 7.

 A toString method that returns the make, model, year, and speed of the vehicle.

Task 2:

Derive a class "Car" from the Vehicle class. Add a field:

 int numberOfDoors

And override the toString method to include the number of doors.

Task 3:

Derive a class "Truck" from the Vehicle class. Add a field:

 int payloadCapacity

And override the toString method to include the payload capacity.


Task 4:

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.

Vehicle Lab Scenario (Part 2) (Task5)


Add the following to your existing classes

Classes:

Vehicle: Base Class

 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()

Car: Derived Class from Vehicle

 Fields:

 numDoors: int

 Methods:

 getNumDoors()

 setNumDoors(int numDoors)

 toString()

Truck: Derived Class from Vehicle

 Fields:

 bedLength: double

 Methods:

 getBedLength()

 setBedLength(double bedLength)

 toString()

Class: Motorcycle: Derived Class from Vehicle

 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.

PART 3 (Task 5 & 6)

Task 1: Create an extension class named "ElectricVehicle" from the base class "Vehicle". This class
should have the following attributes:

 batteryCapacity (double) - the capacity of the vehicle's battery in kilowatt-hours

 batteryLevel (double) - the current level of the battery in kilowatt-hours

 chargingSpeed (double) - the speed in kilowatt-hours per hour at which the vehicle can be
charged

Task 2: Implement the following methods in the ElectricVehicle class:

 A constructor that sets the name, weight, and batteryCapacity

 A method named "chargeBattery" that increases the batteryLevel by the chargingSpeed for a
given number of hours

 A method named "useBattery" that decreases the batteryLevel by a given amount

 A method named "displayBatteryLevel" that returns the current batteryLevel as a percentage of


the batteryCapacity

Task 3: Create another extension class named "ElectricCar" from the ElectricVehicle class. This class
should have the following attributes:

 numberOfSeats (int) - the number of seats in the car

 model (String) - the model of the car

Task 4: Implement the following methods in the ElectricCar class:

 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.

You might also like