![]() | |
Teachers Assignments Assignments Other Pages | Project 13: Assigned: Wed Dec 04 2024 In this project, you'll implement classes with inheritance and polymorphism to automatically use the correct methods. Part 0, 1 points: Create a new class, >>> fuji_location = Location(GeographicCoordinate('N', 35, 21, 29), GeographicCoordinate('E', 138, 42, 52))
>>> fuji = Volcano('Mount Fuji', 3776, fuji_location)
>> print(fuji)
Mount Fuji (3776.24m tall @(35°21'29"N, 138°43'52"E))
>>> kilauea_loc = Location(GeographicCoordinate('N', 19, 25, 16), GeographicCoordinate('W', 155, 17, 12))
>>> kilauea = Volcano('Kilauea', 1247, kilauea_loc)
>>> print(kilauea)
Kilauea (1247m tall @(19°25'16"N, 155°17'12"W)) Tester.Part 1, 1 points: Create a new class, >>> lamborghini = Car(1987, 'red', 'countach')
>>> print(lamborghini)
red 1987 countach Tester.Part 2, 1 points: Add a method to the Car class, >>> lamborghini = Car(1987, 'red', 'countach')
>>> lamborghini.get_age(2022)
35 Tester.Part 3, 0 points: Create two new subclasses of Car, >>> honda = Coupe(1997, 'silver', 'civic')
>>> honda.get_num_doors()
2 Part 4, 2 points: Let's add the capability of modeling cars with hatchbacks. One way to do this would be to modify the constructor to include another parameter. Instead of doing that, let's add another method to the Car class, >>> honda = Coupe(1997, 'silver', 'civic')
>>> honda.set_has_hatch(True) You can test whether it's working by checking whatever field you're using. (I used has_hatch , but you can name yours anything.)>>> honda = Coupe(1997, 'silver', 'civic')
>>> honda.has_hatch
False
>>> honda.set_has_hatch(True)
>>> honda.has_hatch
True This counts as a door, so you also need to modify get_num_doors to take the hatchback into account (in both subclasses):>>> honda = Coupe(1997, 'silver', 'civic')
>>> honda.set_has_hatch(True)
>>> honda.get_num_doors()
3 You'll also need to modify the car constructor to make the default hatchback setting (it should be false).>>> honda = Coupe(1997, 'silver', 'civic')
>>> honda.get_num_doors()
2
>>> honda.set_has_hatch(True)
>>> honda.get_num_doors()
3 Tester.Part 5, 0 points: Sedans can also have hatchbacks. Do the same for the Sedan class. Part 6, 1 points: Let's return to our volcanoes for a bit. The >>> loa_location = Location(GeographicCoordinate('N', 19, 28, 46.3), GeographicCoordinate('W', 155, 36, 9.6))
>>> loa = ActiveVolcano('Mauna Loa', 4169, loa_location)
>>> print(loa)
Mauna Loa (4169m tall @(19°28'46.3"N, 155°36'9.6"W)) is an active volcano Tester.Part 7, 1 points: Now do the same for >>> kea_location = Location(GeographicCoordinate('N', 19, 49, 14), GeographicCoordinate('W', 155, 28, 5))
>>> kea = DormantVolcano('Mauna Kea', 4207, kea_location)
>>> print(kea)
Mauna Kea (4207m tall @(19°49'14"N, 155°28'5"W)) is a dormant volcano Tester.Part 8, 1 points: Now we can write two super simple methods, >>> loa_location = Location(GeographicCoordinate('N', 19, 28, 46.3), GeographicCoordinate('W', 155, 36, 9.6))
>>> loa = ActiveVolcano('Mauna Loa', 4169, loa_location)
>>> kea_location = Location(GeographicCoordinate('N', 19, 49, 14), GeographicCoordinate('W', 155, 28, 5))
>>> kea = DormantVolcano('Mauna Kea', 4207, kea_location)
>>> kea.is_active()
False
>>> loa.is_active()
True Tester.Part 9, 2 points: Let's combine objects and lists and write a function, >>> loa_location = Location(GeographicCoordinate('N', 19, 28, 46.3), GeographicCoordinate('W', 155, 36, 9.6))
>>> loa = ActiveVolcano('Mauna Loa', 4169, loa_location)
>>> kea_location = Location(GeographicCoordinate('N', 19, 49, 14), GeographicCoordinate('W', 155, 28, 5))
>>> kea = DormantVolcano('Mauna Kea', 4207, kea_location)
>>> fuji_location = Location(GeographicCoordinate('N', 35, 21, 29), GeographicCoordinate('E', 138, 42, 52))
>>> fuji = DormantVolcano('Mount Fuji', 3776, fuji_location)
>>> kilauea_loc = Location(GeographicCoordinate('N', 19, 25, 16), GeographicCoordinate('W', 155, 17, 12))
>>> kilauea = ActiveVolcano('Kilauea', 1247, kilauea_loc)
>>> volcanoes = [loa, kea, kilauea, fuji]
>>> active = get_active(volcanoes)
>>> for volcano in active:
print(volcano)
Mauna Loa (4169m tall @(19°28'46.3"N, 155°36'9.6"W)) is an active volcano
Kilauea (1247m tall @(19°25'16"N, 155°17'12"W) is an active volcano Tester.Part 10, 1 points: Let's do another one that combines lists and objects! Write a function, >>> loa_location = Location(GeographicCoordinate('N', 19, 28, 46.3), GeographicCoordinate('W', 155, 36, 9.6))
>>> loa = ActiveVolcano('Mauna Loa', 4169, loa_location)
>>> kea_location = Location(GeographicCoordinate('N', 19, 49, 14), GeographicCoordinate('W', 155, 28, 5))
>>> kea = DormantVolcano('Mauna Kea', 4207, kea_location)
>>> fuji_location = Location(GeographicCoordinate('N', 35, 21, 29), GeographicCoordinate('E', 138, 42, 52))
>>> fuji = DormantVolcano('Mount Fuji', 3776, fuji_location)
>>> kilauea_loc = Location(GeographicCoordinate('N', 19, 25, 16), GeographicCoordinate('W', 155, 17, 12))
>>> kilauea = ActiveVolcano('Kilauea', 1247, kilauea_loc)
>>> volcanoes = [kea, kilauea, fuji]
>>> big = highest_active(volcanoes)
>>> print(big)
Kilauea (1247m tall @(19°25'16"N, 155°17'12"W) is an active volcano Tester.Part 11, 0 points: Start a new class, Part 12, 1 points: Add a Part 13, 1 points: Add a method to the class, Part 14, 0 points: You should now be able to do things like this: >>> die = Die()
>>> die.roll()
3
>>> die.roll()
6
>>> die.roll()
5 Part 15, 0 points: Update the constructor to add an attribute (field): Part 16, 0 points: Modify your Part 17, 1 points: Create a new class, Part 18, 0 points: Add an appropriate Part 19, 1 points: What happens if you try to call the Part 20, 0 points: Physical dice are a bit limited in the number of sides they can have, but we are not as limited with our virtual dice! Create another subclass of Part 21, 1 points: Try out the >>> die = AnySidedDie(300)
>>> die.roll()
42
>>> die.roll()
271
>>> die.roll()
215 Tester.Submitting your Project: Make sure all your code is in a file labelled with your user name (everything before the @ in your school email address) followed by |