CS21 Lab 11: Objects
Due Saturday, December 9, before midnight
Goals
The goals for this lab assignment are:
-
Gain experience writing classes
-
Gain experience reading and using classes
1. Requirements
The following are the main requirements of this lab:
-
You must write and use an Animal class (i.e. don’t try to find a workaround using some other language feature)
-
The primary logic controlling the behavior of individual Animals should be in the class
-
The logic of managing the collection of Animals (e.g., on a farm) should be outside the class. You must write a separate program, which should maintain a list of Animal objects and a interactive menu for updating Animals.
2. Testing
-
As always, you should test your program as you go
-
Since you’re working with two files, it’s a good idea to put the tests for your class in the class file
-
Like we saw in class, this can be done by writing a
main()
method, and then calling it at the bottom using:if __name__ == "__main__": main()
-
This way your testing code will get run when you run
python3 animal.py
on the command line, but it won’t get run when youimport
from animal infarm.py
3. Farm
For this lab, you’ll make a simple class to represent a farm animal or pet;
we’ll call this an Animal. Your program will consist of two files:
animal.py
will contain the class definition, and farm.py
will contain a
program to let the user manage a collection of animals.
3.1. farm.py
The program in farm.py
should implement a menu loop similar to previous
labs, with options for managing a collection of animals. Internally, this
should be a list of Animal objects, which will initially be empty when the
program starts. The menu should allow the user to perform one of several
actions on the collection of animals:
Main Menu:
1. List all animals
2. Add new animal
3. Feed one animal
4. Bed time
0. Quit
As with previous labs, you should handle invalid input so the program doesn’t crash or get stuck; see the Section 4 for some examples of what this looks like.
Each of the menu options should let you interact with the list of animals:
-
List all animals
-
Choosing this option should print the description of each animal. You can do this by calling
get_description()
on each animal object and printing the result, assuming you write theget_description()
method correctly in your Animal class.
-
-
Add new animal
-
Choosing this option should prompt the user for information needed to construct a new Animal object. Your program should add this new animal to the list of all animals.
-
-
Feed one animal
-
Choosing this option should prompt the user to select an animal from the list. You should then call the
feed()
method on the selected animal. -
Be sure to validate input so the program doesn’t crash. That is, your program must force the user to keep entering values until they select a valid animal. (Be careful: what happpens if you try to feed an animal but there are no animals in your list?)
-
-
Bed time
-
Choosing this option should call the
sleep()
method on each animal in the list. -
You should not prompt the user for which animal should sleep, since you will have all of the animals sleep.
-
-
Quit
-
Choosing this option should print a goodbye message and end the program.
-
3.2. animal.py
The animal.py
file should contain a class definition for a class called
Animal
. You will need to import this class into your farm.py
program
by typing from animal import Animal
at the top of farm.py
. As long as
animal.py
is in the same directory, this import
statement will be enough
for python to find it and import the class.
Even though your file is called animal.py , you import from animal .
Python adds the .py automatically when looking for the file.
|
The Animal class should have at minimum the following methods:
-
Constructor (
__init()__
)-
The constructor should take in the animal’s species and a short description (e.g., color, temperment). For example
Animal('donkey', 'grey mini')
would create an grey mini donkey. -
Each of the constructor parameters should be stored in an instance variable.
-
The constructor should also initialize an instance variable,
fed
, which will store how well-fed the animal is. This should be initialized to 0 in the constructor.
-
-
get_description()
-
This method should take no parameters (other than
self
) and should return a string displaying the description, species, and information about how well-fed the animal is. For example, "The grey mini donkey is very hungry". -
The animal’s hunger status should be "very hungry" if the instance variable
fed
is 0; "a little hungry" if it is 1; and "well fed" if it is is 2.
-
-
__str__()
-
This should take no parameters (other than
self
), and should return a string with a short description of the animal, excluding hunger status. e.g. "grey mini donkey" for the example above.
-
-
feed()
-
This method should take no parameters (other than
self
), and should each update the animal’s state and print a message about what’s happening. -
For example, if you feed an animal, you choose to print "nom nom nom".
-
Typically feeding an animal increases the
fed
variable by 1; however, an animal cannot have the variablefed
exceed 2. If you try to feed an animal too many times (it’sfed
count is already 2), the animal should not want to eat anymore. In the case where the animal is over-full, just print an appropriate message and don’t incrementfed
.
-
-
sleep()
-
This method should print an appropriate message saying that this animal is going to sleep.
-
This method should also decrease the
fed
variable by 1, though you should make sure that it never goes below 0.
-
4. Example Output
5. Answer the Questionnaire
Each lab will have a short questionnaire at the end. Please edit
the Questions-11.txt
file in your cs21/labs/11
directory
and answer the questions in that file.
Once you’re done with that, you should run handin21
again.
Submitting lab assignments
Remember to run handin21
to turn in your lab files! You may run handin21
as many times as you want. Each time it will turn in any new work. We
recommend running handin21
after you complete each program or after you
complete significant work on any one program.
Logging out
When you’re done working in the lab, you should log out of the computer you’re using.
First quit any applications you are running, like the browser and the terminal. Then click on the logout icon ( or ) and choose "log out".
If you plan to leave the lab for just a few minutes, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock icon. PLEASE do not leave a session locked for a long period of time. Power may go out, someone might reboot the machine, etc. You don’t want to lose any work!