Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4 . Please add comments in the program detailing your solution for each problem. Your comments should contain a very brief ( maximum 4 lines
Please add comments in the program detailing your solution for each problem. Your
comments should contain a very brief maximum lines description of each solution.
All programs will be tested for completeness on three different test cases.
Unless otherwise specified please do not use predefined libraries or functions.
Write a program that explores the concepts of inheritance and polymorphism to represent
different types of cars in JAVA. Instead of creating a class for each type of car, we will try to
capture the inherent relationships among them to create custom, flexible representations using
objectoriented programming principles.
In this assignment, you will create five classes a base class, three child classes and a tester
class.
Part : Base class
The base class will be a Vehicle class that captures some relationships that are common to
all cars such as
Make String
Model String
Year int
License Plate Number String
Miles Driven int
Therefore, each car can be represented as a base class Vehicle that contains these
attributes. Create a base class that has the above attributes. We will have constructors, a
parameter constructor all instance data are initialized to unknown or and one which
receives a parameter for each instance data and assigns them all appropriately.
Part : Child Classes
Cars can be powered using gas, electricity, or both! You should create three child classes that
extend the Vehicle class to denote each type of car. The GasCar class will have instance
variables: tank capacity float and fuel type String to denote gas vs diesel, the
ElectricCar class will have instance variable: battery capacity float and the
HybridCar class will have instance variables: battery capacity float tank capacity float
and fuel type String Similar to the base class, using function overloading, create
constructors.
Part : Polymorphism
Implement a function getFuelEfficiency that computes the fuel efficiency of each car.
The function should take in parameter: newmiles. For the GasCar class, it should return
the miles per gallon MPG computed as
For the ElectricCar class, it should return the miles per gallon equivalent MPGe
computed as You can set cost of kWh $
For hybrid, you should return Finally, it should add the parameter
newMiles to the attribute Miles Driven.
Part : Tester class
Create a tester class with a main function that creates an instance of each child class and
computes fuel efficiency of each instance. You should verify your implementation by creating
instances using the two different constructors. Also verify your implementation of the
getFuelEfficiency function for each instance.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started