Day 21 In-class Assignment: We have a ZOO! (part 2)#

✅ 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

  • creating a class Person

  • create objects for zookeepers

  • add personnel (zookeepers) to the ZOO (ZOO object is already provided)

  • testing code (personnel)

  • using ZOO object to print information about the ZOO

Additional assignment (time permitting):

  • additional manipulation of ZOO data/numbers

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#

On the website, there are two additional files you will need to finish today’s assignment: “Animal.py” and “ZOO.py”. Note these are updated versions from what we had last time, so you need to download the file again! Make sure you download both files and put them in the same folder as this Jupyter notebook or you could run the cell below to automatically download the files. The files contain definitions of the classes you will use in the assignment.

# Try running this cell
# !curl -O https://raw.githubusercontent.com/msu-cmse-courses/cmse801-supplemental/main/Day-21/Animal.py
# !curl -O https://raw.githubusercontent.com/msu-cmse-courses/cmse801-supplemental/main/Day-21/ZOO.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4620  100  4620    0     0  82714      0 --:--:-- --:--:-- --:--:-- 88846
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2951  100  2951    0     0  76405      0 --:--:-- --:--:-- --:--:-- 89424
# import animal and zoo class; Animal file contains definitions of all required animal classes
from Animal 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

Part 1: Creating an 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):

    • name: person’s name

    • kind: kind of animal they are specialized for (can take care of)

    • number_animals: 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

      • you should define initial values to all attributes of the class even though nothing was passed for them yet.

    • set_name: set person’s name

    • get_name: get person’s name

    • set_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 of

      • parameters to the method are: animal kind, number of animals

    • get_kind: get animal ‘kind’ for which the person is responsible for

    • get_number_animals: get number of animals the person can be responsible for

    • get_specialty: get person’s specialty (animal kind and number of them)

      • the method should return two values: animal kind, number of animals; Hint: try to remember how the “derivatives” functions for solving ODEs were set up to return multiple values.

    • info: prints person’s information: name, animal kind, number of animals

Write all necessary code in the cell below and test the code on regular base. We recommend writing pseudo code, using the whiteboard, and discussing with group members, if necessary. If it takes longer that you would expect, no worries! The process or creating the class is important, not just the class created in the end!

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 step.

# enter your Person class code here

1.1 Test your code!!#

Now use the code in the cell below to test your Person class. The code creates two objects as zookeepers and print 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:

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')
# Note that you haven't set the information about Jack's specialty yet, but this should still run
jack.info()

# Now you'll set the information about Jack's specialty
jack.set_specialty('lion', 1)
jack.info()

print('\n---\n')

suzy = Person('Suzy')
suzy.set_specialty('penguin', 3)
suzy.info()
Jack is a zookeeper and can take care of maximum 0 (s).
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).

Part 2: Let’s assemble a ZOO#

The class for ZOO personnel is now prepared and tested. We can create a ZOO with all classes we have at our disposal: ZOO, Animal and all derived animal classes (Lion, Zebra, Penguin) and 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:

  • I have included a bunch of animal objects (you can add additional animals from the pre-class assignment or create new ones if you’d like)

  • personnel objects:

    • Jack: can take care of 1 lion

    • Mary: can take care of 3 penguins

    • Franck: can take care of 2 penguins

    • Suzy: can take care of 3 zebras

    • Mike: can take care of 3 lions

Write code that will define necessary objects in the cell below.

# --- creating animals---
alex = Lion('Alex')
alex.set_arrive_to_zoo('2017-01-01')

marty = Zebra('Marty')
marty.set_arrive_to_zoo('2013-01-01')

skipper = Penguin('Skipper')
skipper.set_arrive_to_zoo('2017-05-01')

kowalski = Penguin('Kowalski')
kowalski.set_arrive_to_zoo('2017-05-01')

rico = Penguin('Rico')
rico.set_arrive_to_zoo('2017-04-01')

private = Penguin('Private')
private.set_arrive_to_zoo('2019-05-17')


# ---- put your code here to create some zookeepers ---- #

2.2 Creating a ZOO#

Finally, let’s create a zoo. The ZOO class (defined in file ZOO.py) is providing all required attributes and methods to set/get/print information in the zoo. As in the pre-class assignments, we have to “add” our objects to the ZOO object.

We should do the following:

  • create a ZOO object, you can name the zoo as you want

  • add animal objects to the ZOO object (they were already created in the cells above)

  • add personnel objects to the ZOO object (they also were already created in the cells above)

  • print the animal names in the zoo

  • print number of personnel in 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 it, either by opening the file and checking the content, using dir() to get the list methods (and attributes) in the object, and using ? to get the description of individual method.

Hint: to get required parameters for creating an object, you can use ? for __init__ method: Animal.__init__?.

Write code that will create a ZOO object and all all animal and personnel objects to your zoo.

# put your code here

Part 3: Printing some 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 Part 3 of the pre-class assignment. Just this time the zoo is defined as a ZOO object instead as a list. Let’s print out some more (or less) interesting facts about our zoo. Write the code into each cell to print the following information. The goal is to do this only taking information from the ZOO object you created.

  • 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

  • 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?#

Now we will print some logistic information about the zoo. We will check if the zoo has enough people to take care for all animals in the zoo - just this time individually per animal. Keep in mind, single person can take care of more than one animal and single animal can require more that one zookeeper.

You will have 4 numbers:

  • 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.

# print if they are understaffed? (just by comparing total number of people needed and available, not by animal)
# Note: keep in mind that each person can take care of more than one animal and single animal may require more than one person

Part 4: Additional assignment (time permitting)#

This is more detailed checking if the zoo is understaffed or they have enough people to take care of all animals in the zoo. This time the check will be done for each animal individually. The code should print all animals in the zoo, with number of available and required zookeepers.

Hint 1: The task is similar to the last task for Part 3, just separate for each animal. You can use 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 there

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 2019, Michigan State University Board of Trustees