Day 20 In-class Assignment: We have a ZOO!#

✅ Put your name here.#

✅ Put your group member names here.

Learning Goals:#

By the end of this assignment you should be able to:

  • Review code that uses built-in and/or custom written objects

  • Use objects in connection to other variable types in Python.

Assignment instructions#

Work with your group to complete this assignment. Instructions for submitting this assignment are at the end of the notebook. The assignment is due at the end of class.

Download additional file#

On the website, there is a file named “”. Make sure you download that file and put it in the same folder as this Jupyter notebook. The file contains the definition of the class you will use in assignment.

Part 1: Creating a ZOO using object Animal#

The objective of this assignment is to create a ZOO with various numbers of animals and various species. The Animal object is already prepared and available in file (check that you downloaded the file). Open the file and look how the object is written.

Before creating a ZOO, discuss with your group how to estimate the following values you will need to define for each animal in the ZOO:

  • animals (species) you will have in the ZOO

  • number of zookeepers needed to take care of each animal; can be fraction: 0.2 means one zookeeper can take care of 5 animals

  • the area required in the ZOO for each animal; this is average value per animal (i.e. if 10 zebras require 10,000 m\(^2\), use 1000 for each zebra)

Create one or more dictionaries and store your estimated values into those dictionaries for later use.

Suggestion: Write out the list of animals you decided to “have” in the ZOO and define value for each of them. Use whiteboards if necessary.

# write estimated values
# area['lion'] = 600
# keepers['lion'] = 2
# or the other way (first number of an area, second is number of keepers):
# animals['lion'] = [600, 2]

1.1 Initialization#

Do not forget to run the cell below. The cell imports necessary modules, numpy and matplotlib, and also new module Animal that contains the definition of the class Animal that we will use during the assignment.

import numpy as np
import matplotlib.pyplot as plt
# import animal class from the Python script
from Animal import Animal
# The magic command below instructs 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 file
%reload_ext autoreload
%autoreload 2

1.2 Create a single Animal object#

First create one Animal object to practice creating objects. Store this object as the variable alex. You can use code from pre-class assignment for creating objects or fill-in missing pieces in the code below.

# put yout code here or complete the provided code
# define parameters to method Animal(); look into file or add new cell and use command 'Animal.Animal?' to see the required parameters
alex = Animal(  )
# add the area
alex.set_required_area( )
# add also number of keepers using method `set_number_keepers()`
# test your new object; no error should be printed
print("{0} is a {1} and requires {2} m^2 of space and {3} keepers to be happy.".format(alex.get_name(), alex.get_kind(), alex.get_required_area(), alex.get_number_keepers()))

Part 2: Let’s assemble a ZOO#

Create a list that will contain all the Animal objects that will be stored in the ZOO. First create an object for each animal (re-use the code above for each animal), define all attributes (name, kind, area, keepers) and then add the object to the list (using the function append() function).

# put your code here
# you can use the values you have defined before: e.g. set_required_area(area['lion'])

Test: Animals in the ZOO#

Print out the animals in your ZOO. In the cell below, add the name of the list that you used for your ZOO. Since we are using objects (objects contain all necessary information about object itself), the code should run without errors and print names of animals in the ZOO.

# printing all animals in the ZOO
# !!! add the name of your list variable to the "for" loop!!!
for animal in ___ :
    # print all animals in the ZOO
    print("{0} is a {1}.".format(animal.get_name(), animal.get_kind()))

Part 3: Printing some information about the ZOO#

Now with the ZOO defined, we can 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

  • number of people you need to hire (round up to next integer number!) to take care for animals

  • total area of the ZOO

  • For today’s date, print the average number of days the animals have spent in the ZOO so far

  • print names of animals, together with their kind in alphabetical order (e.g. “Alex, lion”);

# print number of animals in 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
# print the average duration of days the animals are in your ZOO
# print names of animals in alphabetical order, together with their kind (example: Alex, lion
# store all names of animals in a list and user sort method to sort the list

Past 4: Additional assignment - time permitting#

You will use animal objects that you used to create a ZOO, do some additional data manipulation, and plot the following:

  • bar plot of number of animals of each kind (number of lions, zebras, etc.)

Hint(s): There are many ways to do this, two possible ways are:

  • using matplotlib.pyplot: use method bar() for plotting. The method requires two lists: one for animal types (X axis), the other for number of each animal type (Y axis):

    • using dictionary makes it easier to store animal types and their count

    • check methods keys() and values() how to get lists of keys and values from a dictionary

  • using pandas: use method plot(kind='bar'). pandas also has a method value_counts() that might be useful to create data necessary for bar plot.

# put your code there


Check in with an instructor before you leave class!

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 2023, Department of Computational Mathematics, Science and Engineering at Michigan State University