Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Inheritance and Composition The focus of this practical is to use inheritance, create base classes, and how to create corresponding derived classes. Create a


imageimageimage

Inheritance and Composition The focus of this practical is to use inheritance, create base classes, and how to create corresponding derived classes. Create a Python class for the following UML class diagram. This diagram captures three classes: Vehicle, Bus, and Engine. Bus is a child (subclass) of Vehicle and therefore inherits its members. Vehicle becomes the parent (superclass) of Bus. Engine is composite to vehicle. Steps 1. Vehicle + wheelCount: int #doorCount: int VIN: str 4. 5. - getVIN(): str + setVIN(vin: str) + showEngineDetails(): str Bus - seatCount: int + getSeatCount(): int + setSeatCount(seatCount: int) 1 Engine Task 1: Vehicle and Buss Classes (1 mark) You are required to create these classes in Python - correctly representing the concept of the relationships. In addition a property, VIN should be created for Vehicle that is using the getter and setter method for the private VIN (vehicle identification number) attribute. A seatCount property should also be created for Bus. -type: str + setType(type: str) + showDetails(): str Create the Vehicle class including an initialiser method. Assign values to the attributes, wheelCount, door Count, and VIN. Note: Correctly apply the attributes' visibilities in code. 2. Implement a getter method which returns the VIN attribute. 3. Implement a setter method which takes a new vin variable as a parameter. Assign the objects attribute VIN to this parameter. Create a property called vin which utilises the getter and setter methods for the attribute VIN. Create the Bus class including an initialiser method. Assign values to the attribute seatcount. Note: Remember to override the parent initialiser. Check Tutorial 3.2 Slide 13 for information on how to do this. 6. Implement a getter method which returns the seatCount attribute. 7. Implement a setter method which takes a new seatCount variable as a parameter. Assign the objects attribute seatCount to this parameter. This method should only set the count if the number is between 0 and 200, otherwise it will set it to 0 by default. 8. Create a property called seatCount which utilises the getter and setter methods for the attribute seatCount. 9. Test your code with the following: vehicle Vehicle(4, 4, "ZE1D4") Bus (8, 2, "BOTW", 40) bus = print("VIN number of Vehicle: " + vehicle.vin) print("VIN number of Bus: + bus.vin) 11 print("Seat Count of Bus: + str (bus.seatCount)) Your output must match the following: VIN number of Vehicle: ZE1D4 VIN number of Bus: BOTW Seat Count of Bus: 40 Task 3: Encapsulation (1 mark) Steps 1. Test and execute the following code. Note down in a commented line what happened and why. print(vehicle._VIN) print (bus._doorCount) Task 4: Engine Class (1 mark) Steps 1. Create the Engine class including an initialiser method. Assign values to the attribute type. 2. Implement a setter method which takes a new type variable as a parameter. Assign the objects attribute type to this parameter. 3. Implement a method showDetails that returns a string detailing the type of engine. 4. Test your code with the following: engine Engine("Diesel") = print("-Engine Details- " + engine.showDetails()) Your output must match the following: -Engine Details- The engine is type: Diesel Task 5: Refining Classes (1 mark) Modify the Vehicle and Bus classes to correctly implement the composition relationship. Steps 1. Modify the Vehicle class to implement the relationship correctly. 2. You should include an engine attribute. This should be instantiated as a new engine with empty quotes ("") as the parameter. (le. No engine was set) 3. Bus should set the type of engine to be "Diesel". 4. Modify the Vehicle class by adding a new method, showEngine Details that calls the showDetails method of the Engine to obtain its information. 5. Test your code with the following: vehicle bus = = Vehicle(4, 4, "ZE1D4") Bus (8, 2, "BOTW", 40) print("Engine details of vehicle. + vehicle.showEngineDetails()) 11 print("Engine details of bus. + bus.showEngineDetails()) Your output must match the following: Engine details of vehicle. The engine is type: Engine details of bus. The engine is type: Diesel

Step by Step Solution

There are 3 Steps involved in it

Step: 1

class Vehicle def initself wheelCount doorCount VIN selfwheelCount wheelCount selfd... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intermediate Accounting

Authors: J. David Spiceland, James Sepe, Mark Nelson

6th edition

978-0077328894, 71313974, 9780077395810, 77328892, 9780071313971, 77395816, 978-0077400163

More Books

Students also viewed these Programming questions

Question

A 300N F 30% d 2 m Answered: 1 week ago

Answered: 1 week ago