In-class Assignment: We have a Zoo!#
Day 6#
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:
Review code that uses built-in and/or custom written Python objects (classes)
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.
Agenda for today:#
Download an additional file#
There is an extra file that you need for this assignment named “Animal.py”. Make sure you download that file and put it in the same location as this Jupyter notebook. The file contains the definition of the class you will use in the assignment. You can get it here:
https://raw.githubusercontent.com/msu-cmse-courses/cmse202-supplemental-data/main/code_samples/Animal.py
1: Creating a Zoo using the Animal
object#
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 Animal.py
(check that you downloaded the file and put it in the right location).
✅ Do This: Open the file and look how the object is written.
Before creating a Zoo, discuss with your group how you will define some of the parameters you will need for your Zoo and for each animal in the Zoo:
The animals (species) you will have in your Zoo
The number of zookeepers needed to take care of each animal; can be a fraction: 0.2 means one zookeeper can take care of 5 animals
the area required in the Zoo for each animal; this can be an average value per animal (i.e. if 10 zebras require 10,000 m\(^2\), use 1000 for each zebra)
✅ Do This: 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 then define values for each of them. Use a collaborative document or whiteboard as 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 This: Do not forget to run the cell below. The cell imports necessary modules, numpy
and matplotlib
, and also our new Animal
module, which 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 Animal.py 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 your code here or complete the provided code
# define parameters to method Animal(); look into file Animal.py or add new cell and use command '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()`
alex.
✅ Do This: Once you’ve created your alex
animal, run the following cell to test that everything is working as intended.
# test your new object; no error should occur and you should see the values you defined above
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()))
2: Let’s assemble a Zoo#
✅ Do This: 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, arrival date, area, and keepers) and then add the object to the list (using the function append()
function). You should put at least 5 animals in your Zoo.
# put your code here
# This list will be used for storing all of your Zoo animals
zoo = []
# Define your Zoo animals
# Depending on how you set things up, you can use the values you have defined before: e.g. set_required_area(area['lion'])
# Make sure you append each animal to the list after you create it
Test: Who are the animals in the Zoo?#
✅ Do This: Run the code to print out the animals in 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 the animals in the Zoo.
# printing all animals in the ZOO
# !!! add the name of your list variable to the "for" loop!!!
for animal in zoo:
# print all animals in the ZOO
print("{0} is a {1}.".format(animal.get_name(), animal.get_kind()))
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.
✅ Do This: Write the code into each cell to print the following information. Remember to use the Animal
object methods as much as possible!
number of animals in the Zoo
number of people you need to hire (round up to next integer number!) to take care of the animals
total area required for the Zoo
For today’s date, print the average number of days the animals have spent in the Zoo so far (hint: there’s a function that returns how long an individual animal has been in the zoo!)
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. Look at Animal.py to determine if there is a function that can help with this...
# print names of animals in alphabetical order, together with their kind (example: Alex, lion)
# hint: store all names of animals in a list and use the sort method to sort the list
4: Visualizing some information about your Zoo (time permitting)#
✅ Do This: Using the animal objects that you used to create your 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 methodbar()
for plotting. The method requires two lists: one for animal types (X axis), the other for number of each animal type (Y axis):using a dictionary makes it easier to store animal types and their count
check methods
keys()
andvalues()
how to get lists of keys and values from a dictionary
using
pandas
: use methodplot(kind='bar')
. Pandas also has a methodvalue_counts()
that might be useful to create data necessary for bar plot.
# put your code here
🛑 STOP#
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 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University