Create a class named ElectricCar and display its model, color, mpg and condition. Note that ElectricCar is a class which inherits from its parent class named Car. In the parent class Car, it has a global variable named condition and its condition is set to ”new” at all time, and has three variables in the constructor ”model, color and mpg”. Furthermore, in the parent class Car, it contains two functions a display car function that displays the car attributes (such as model, color and mpg) and a drive car function that changes the condition to ”used”. Moreover, in the child ElectricCar class, it has one more attribute ”battery type” and it overrides the drive car function by setting the condition to ”like new”. To test your class CreateaElectricCarnamedmyCarbysettingmodel=”DeLorean”,color=”silver”, mpg=88, and battery type=”molten salt”. Display myCar’s condition by ”print(myCar.condition)”. Output is ”new”. Change myCar’s condition to ”like new” by myCar.drive car(). Again, display myCar’s condition by ”print(myCar.condition)”. Output is ”like new”. Display myCar’s information by ”myCar.display car()”. Output is ”This is a silver DeLorean with 88 MPG.”.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
-
Create a class named ElectricCar and display its model, color, mpg and condition. Note that ElectricCar is a class which inherits from its parent class named Car. In the parent class Car, it has a global variable named condition and its condition is set to ”new” at all time, and has three variables in the constructor ”model, color and mpg”. Furthermore, in the parent class Car, it contains two functions a display car function that displays the car attributes (such as model, color and mpg) and a drive car function that changes the condition to ”used”. Moreover, in the child ElectricCar class, it has one more attribute ”battery type” and it overrides the drive car function by setting the condition to ”like new”.
To test your class
-
CreateaElectricCarnamedmyCarbysettingmodel=”DeLorean”,color=”silver”,
mpg=88, and battery type=”molten salt”.
-
Display myCar’s condition by ”print(myCar.condition)”. Output is ”new”.
-
Change myCar’s condition to ”like new” by myCar.drive car(). Again, display myCar’s condition by ”print(myCar.condition)”. Output is ”like new”.
-
Display myCar’s information by ”myCar.display car()”. Output is ”This is a silver DeLorean with 88 MPG.”.
-
![You may need the following hints to complete the question.
• Define a new class named Car. Inside your Car class, define a global variable
condition and give it an initial value of the string new.
• Define the -init-() function of the Car class to take four inputs: self, model,
color, and mpg. Assign the last three inputs to member variables of the same
name by using the self keyword.
Inside the Car class, add a method named display_car to Car that will reference
the Car's member variables to return the string, "This is a [color] [model] with
[mpg] MPG." You can use the str() function to turn your mpg into a string when
creating the display string.
• Inside the Car class, add a method drive_car that sets self.condition to the string
"used".
• Create a class ElectricCar that inherits from Car. Give your new class an init.()
method of that includes a battery_type member variable in addition to the model,
color and mpg.
• Inside ElectricCar add a new method drive_car that changes the car's condition
to the string "like new".](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F57e4a798-c00f-4b47-9a58-02ab3a8911d1%2F53f488db-7abd-4533-a67a-1d699c44eeb8%2Ftnmcbx8_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images









