Question: Task 1 : Understanding Common Issues! roblem Statement mplement the following class diagram in Python code. Before doing so , we must be sure to

Task 1: Understanding Common Issues! roblem Statement
mplement the following class diagram in Python code. Before doing so, we must be sure to understand the requirements and the issues with a design like this one (see below). Some of these modules have been provided to you. Utilise them in your design but do not modify them.
Dinosaur
Figure 1- Dinosaur Class Diagram
Learning Item: Lazy Imports
Let's consider this design and how it might work. Dinosaur is the root class in this structure. You can see, it has a method for lay_egg which will (as shown in the diagram) return an Egg. The egg then hatches (through the hatch method), into a dinosaur of different types. This theoretically makes sense as a concept and is programmable in a single file, however, if you attempt to split this, you will get a circular import error. A circular import is warning you that both modules will use each other in the code. A way we can handle this is through a form of importing called a "lazy
import" to encapsulate the import line inside a method, rather than the top of a script. There are both benefits and detriments to doing this.
Learning Item: Diamond Problem
This diagram proposes that the diamond problem will occur during run time. This, however, can e avoided. Looking at the diagram, we have Dinosaur, Carnivore, Herbivore and Omnivore. However, in code, it would not be useful to instantiate these objects (which leads to the diamond problem. Instead, we create Tyrannosaurus Rex, Triceratops and Oviraptor, which will be the classes we instantiate and use. This is a concept we will learn more about next week \ Yippee.
Task 2: Dinosaur
Create a
dinosaurs.py module. Usually, it is advised to separate all classes into their own module but there is not enough code to do this.
Add in
egg.py and do not modify it
With your pair programming partner, identify how this code works and utilise this understanding in your solution.
Create the Dinosaur class with a constructor containing the attributes of name, sex, age and egg (you can set egg to None or an empty list by default - This depends on your implementation)
Write the lay egg method which should set the egg attribute to a new Egg object if the dinosaur is female and above the age of 4. Otherwise, no egg can be laid.
Write the warm_egg method which, if an egg exists in, it should hatch it! Otherwise it should let the user know there are no eggs to "sit on".
Write the name_baby method which takes as parameters a dinosaur and a name. It will assign that name to the dinosaur.
Copy and paste this string conversion method into your Dinosaur class.
(self):
f self.__name
return f" self..} is a {self.__sex}, age {self.__age}.In"???
f"They are named {self.__name}."
return f"{self..} is a {self.__sex}, age {self.__age}.
"???
f"They haven't been named, yet!"
Task 3: Herbivore and Carnivore
Create two new classes: Herbivore and Carnivore. They should inherit from Dinosaur.
. Write a method for forage in Herbivore which returns a string based on foraging.
Write a method for hunt in Carnivore which returns a string based on hunting.
a. Both methods should include the dinosaur's data type.
Note: Neither of these classes have additional code including a constructor.
Task 4: Omnivore
Create the Omnivore class which inherits from Herbivore and Carnivore
Note: Although this looks like it will create the diamond problem, we are not instantiating this class and will not cause the common diamond problem error.
Task 5: Different Dinosaurs
2. Create the specific dinosaur classes. i.e., Tyrannosaurus Rex, Triceratops and Oviraptor.
3. Feel free to include additional types of dinosaurs but you will need to then modify egg to
hatch them.
4. Check the inheritance structure to see who they inherit from!
Note: None of these classes have additional code including a constructor. Include pass after the class definition to avoid any runtime errors.
Hint
Any R&R attendees will have seen a similar diagram before. Feel free to use your solution to get you started. The trick here is to implement the Multiple Inheritance now (3)
2. You won't need to separate each dinosaur into its own module. Although common practice, there isn't enough code to warrant that.
Output
Instantiate at least 3 different types of Dinosaurs. Make sure they are not direct instances of Herbivore, Carnivore or Omnivore (We want actual types of dinosaurs.)
Call their methods and create new baby dinos!
Task 1 : Understanding Common Issues! roblem

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!