In-class Assignment: We have a Zoo! (part 2)#
Day 7#
CMSE 202#
✅ Put your name here.#
✅ Put your group member names here.
Learning Goals:#
By the end of this assignment you should be able to:
Create a custom class from scratch
Practice using inheritance and composition using custom classes
Setting/getting values of attributes from objects stored in combined object
Agenda#
Agenda of today’s in-class assignment
Additional assignment (time permitting):
Assignment instructions#
Instructions for submitting this assignment are at the end of the notebook. The assignment is due at the end of class.
Download additional files#
There are a couple extra file that you need for this assignment named “Animals.py” and “Zoo.py”. Make sure you download those files and put them in the same location as this Jupyter notebook. Theses file contains the definition of the classes you will use in the assignment. You can get them here:
https://raw.githubusercontent.com/msu-cmse-courses/cmse202-supplemental-data/main/code_samples/Animals.py
https://raw.githubusercontent.com/msu-cmse-courses/cmse202-supplemental-data/main/code_samples/Zoo.py
✅ Do This: After you have the new files, run this cell to make sure all of your objects import correctly!
# import animal and zoo class; Animal file contains definitions of all required animal classes
from Animals import Animal, Penguin, Zebra, Lion
from Zoo import Zoo
# import usual classes
import numpy as np
import matplotlib.pyplot as plt
# The magic command below tells Jupyter Notebook to automatically load classes and methods from external files
# in case they have changed from last load time;
# This is needed when you change the Animal.py file
%reload_ext autoreload
%autoreload 2
1: Creating a Person
object#
The objective of the first part of the assignment is to create a class that will allow us to add personnel to the Zoo object. To practice creating and using custom (not built-in) classes in Python, we will create a Person
class based on list of requirements that object needs to provide.
The Person
object’s attributes and methods, that is, the “functionality” of the object, is as follows:
attributes in the class (information that can be stored in the class):
person’s name
kind of animal they are specialized for (are trained to take care of)
how many animals can the person take care of
methods that the class has to provide:
initialization of the class: the method should accept one parameter - person’s name
the initialization should also define initial values for all attributes of the class
set_name
: set person’s nameget_name
: get person’s nameset_specialty
: set specialty of the person by defining which animal the person can be responsible for and how many of them they are capable of taking care ofparameters given to this method are: animal kind, number of animals
get_kind
: get animal ‘kind’ for which the person is responsible forget_number_animals
: get number of animals the person can be responsible forget_specialty
: get person’s specialty (animal kind and number of them)the method should return two values: animal kind, number of animals
info
: prints person’s information: name, animal kind, number of animals
✅ Do This: Write all necessary code in the cell below and test your code regularly. We recommend writing pseudo code, using a virtual document/whiteboard or one of the ones in the classroom, and discussing with group members as necessary. If it takes longer that you would expect, no worries! The process of creating the class is important and take practice to get used to.
Hint: When writing from scratch, make small changes/additions to the code and test the code often! Doing many small steps is much better than few huge steps.
# Create your Person class code here
1.1 Test your code!#
✅ Do This: Use the code in the cell below to test your Person
class. The code creates two objects as zookeepers and prints their information, provided by method Person.info()
. This short code tests that attributes are defined and stored correctly, and that one can retrieve attributed from class. The printout should look similar to this, but will vary based on how you formatted your print statements:
Jack is a zookeeper and can take care of maximum 1 lion(s).
Suzy is a zookeeper and can take care of maximum 3 penguin(s).
If that’s the case, you are good to move on!
# test your class with following code
jack = Person('Jack')
jack.set_specialty('lion', 1)
jack.info()
suzy = Person('Suzy')
suzy.set_specialty('penguin', 3)
suzy.info()
2: Assemble a Zoo#
The class for Zoo personnel is now prepared and tested. With this in hand, we can create a Zoo with all classes we have at our disposal: Zoo
, Animal
and all derived animal classes (Lion
, Zebra
, Penguin
), and your newly created Person
class.
2.1 Creating animal and personnel objects#
First we need to create all objects for animals and zookeepers and store them in the Zoo
object. Create the following objects:
animal objects (you can copy animals from your pre-class assignment or create new ones). Remember: when creating animals you should be using the special Animal classes that inherited the original basic
Animal
class (e.g.Penguin
,Lion
, etc.)personnel objects:
Geoffrey: can take care of 1 lion
Cobie: can take care of 3 penguins
Jai: can take care of 2 penguins
Rui: can take care of 3 zebras
Mallory: can take care of 3 lions
✅ Do This: Write code that will define necessary animal and person objects in the cell below.
# put your code here
2.2 Creating a Zoo#
Finally, let’s create a zoo. The Zoo
class (defined in file Zoo.py
) provides all required attributes and methods to set/get/print information in the zoo. As in the pre-class assignments, we have to “add” our animal and person objects to the Zoo
object (composition!).
We should do the following:
create a
Zoo
object, you can name the zoo anything you’d likeadd your animal objects to the
Zoo
object (they should be created in cells above)add your personnel objects to the
Zoo
object (they should be created in cells above)print the name of all the animal in the zoo
print number of personnel that work for the zoo
Because the Zoo
class is given, you should get familiar with the class first. Check what methods you need to use to achieve the tasks listed above and what are their arguments (parameters). There are many ways to do this, either by opening the file and checking the content, using dir()
to get the list methods (and attributes) in the object, or using ?
to get the description of individual methods.
Hint: to get required parameters for creating an object, you can use ?
for the __init__
method: Animal.__init__?
.
✅ Do This: Write code that will create a Zoo
object, add all animal and personnel objects to your zoo, and then use the appropriate methods to pring the animal names and the number of personnel that work for your zoo.
# put your code here
3: Accessing information about the Zoo#
3.1 Printing basic information about the Zoo#
We will proceed by printing similar information about our zoo as you did in the pre-class assignment. Of course now the zoo is defined as a Zoo
object instead as a list.
✅ Do This: Print out some more (or less) interesting facts about our zoo. Write the code into each cell to print the following information:
number of animals in the zoo
names of people hired by the zoo
number of people you need to hire (round up to next integer number!) to take care of the animals (based on the number of zookeepers required per animal)
total area of the zoo
# print number of animals in the Zoo
# print number of people hired by the Zoo
# print number of people you need to hire (round up to next integer number!) to take care for animals
# print total area of the Zoo
3.2 Printing advanced Zoo information - is the Zoo understaffed or overstaffed?#
✅ Do This: Now we will print some more logistic information about the zoo. Specifically, we will check if the zoo has enough people to take care for all animals in the zoo. For now, we will just check and see how many total zoo keepers are required and how many total animals the zookeepers can take care of, you don’t need to worry about which animals are within the zookeepers specialities. Keep in mind that a single person can take care of more than one animal and single animal may require more that one zookeeper.
You will have 4 numbers you need to look at:
number of zookeepers in the zoo,
required number of zookeepers
number of animals in the zoo
number of animals zookeepers in the zoo can take care of
It might be helpful to print out all four numbers and then decide, based on those values, if the zoo is understaffed or overstaffed.
# Determine if the Zoo is understaffed or overstaffed (just by comparing total number of people needed and available, not by types animal they are trained to care for)
# Note: keep in mind that each person can take care for more than one animal and single animal may require more than one person
⏸ Nice work! You’ve completed all of the important content for today. If you still have time left, try to tackle the next section as well.
4. Checking our Zoo staffing more carefully (time permitting)#
✅ Do This: Try to perform a more detailed check to see if the zoo is understaffed or if they have enough people to take care of all animals in the zoo. This time the check will be done to ensure that there are enough properly training zookeepers to take care of the variety of animals in the Zoo. The code should print all of the different types of animals in the zoo, along with number of available and required zookeepers to care for those animals.
Hint 1: The task is similar to the last task for part 3, but now you need to check on an animal by animal basis. You can use a dictionary for storing information about available and required number of people for each animal.
Hint 2: If you used non-integer numbers for number of animals each zookeeper can handle and/or for number of zookeepers is required for individual animal, do not forget to round the number of people at the end of the loop over animals. The required number of people should be rounded up, available number of people should be rounded down.
# put your code here
Congratulations, you’re done!#
Submit this assignment by uploading it to the course Desire2Learn web page. Go to the “In-class assignments” folder, find the appropriate submission folder, and upload it there.
See you next class!
© Copyright 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University