CS21 Lab 12: Classes and Objects

Due Saturday, April 30, by 11:59pm


Read through this entire lab writeup BEFORE you begin to write any code. Taking the time to thoroughly understand what you are trying to accomplish will allow you to more effectively implement the code.

Goals

  • Gain experience writing classes

  • Gain experience reading and using classes

Requirements

The following are the main requirements of this lab:

  • You must write and use a Critter class (i.e. don’t try to find a workaround using some other language feature)

  • The primary logic controlling the behavior of individual critters should be in the class

  • The logic of managing the collection of them should be outside the class (i.e. in the main program, which should maintain a list of Critter objects)

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 critter.py on the command line, but it won’t get run when you import from critter in menagerie.py

Menagerie

For this lab, you’ll make a simple class to represent a pet; we’ll call this a Critter. Your program will consist of two files: critter.py will contain the class definition, and menagerie.py will contain a program to let the user manage a collection of critters.

menagerie.py

The program in menagerie.py should implement a menu loop similar to previous labs, with options for managing a collection of critters. Internally, this should be a list of Critter 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 critters:

Main Menu:
1. Check on critters
2. Add new critter
3. Feed critter
4. Pet critter
5. Play with critter
6. Go to bed
0. Quit

As with previous labs, you should handle invalid input so the program doesn’t crash or get stuck; see the Example Output for some examples of what this looks like.

Each of the menu options should let you interact with the list of critters:

  • Check on critters

    • should describe the room ("empty" if none are present, "some critters" if 1-4, or "full of critters" if more than 4 are present)

    • should print the description of each critter (just calling print() on each critter object should be sufficient if you write the __str__() method correctly in your critter class)

  • Add new critter

    • should prompt the user for information needed to create a new critter, then should construct a new critter object and add it to the list

  • Feed/Pet/Play with critter

    • should prompt the user to select a critter from the list, and should then call the appropriate method on that critter

    • be sure to validate input so the program doesn’t crash (e.g. if the user asks for critter number -7 or something)

  • Go to bed

    • should print "Goodnight!" and call the sleep method on each critter in the list

    • should not prompt the user for which critter, since it should always apply to all of them.

  • Quit

    • print goodbye message and end program

critter.py

The critter.py file should contain a class definition for a class called Critter. You will need to import this class into your menagerie.py program by doing from critter import *; as long as critter.py is in the same directory, this import statement will be enough for python to find it and pull it in (note that we’re importing from critter, not critter.py; Python knows to add the .py automatically when looking for the file).

The critter class should have at minimum the following methods:

  • Constructor (__init()__)

    • this should take in the critter’s name, a descriptor, two pronouns (personal and posessive), and what the critter eats

    • each of these should be stored in an instance variable

    • it should also initialize instance variables counting how often the critter has been fed, petted, and played with recently; each of these three variables should start out as 0

  • get_name()

    • this should take no parameters (other than self), and should return the critter’s name

  • __str__()

    • this should take no parameters (other than self), and should return a string with a description of the critter and it’s current state

    • it should start with the critter’s name and descriptor, e.g. "<name> is <descriptor>"

    • it should then have a description capturing the critter’s "state" as represented by the counters for how many times it’s been fed, played with, or petted lately, using the critter’s personal pronoun, e.g. "she looks well-fed, playful, and happy"

    • for all states but fed it’s fine to just distinguish between 0 and greater-than-0; for fed you should also report that the critter is overfull if it’s greater than 2.

  • pet()

  • feed()

  • play()

    • these three methods should each take no parameters (other than self), and should each update the critter’s state and print a message about what’s happening.

    • for example, if you pet a critter, it might print "<name> is <descriptor>, and enjoys <possessive pronoun> pets!"

    • each method should normally increase the corresponding counter by 1

    • however, if you feed a critter too many times (i.e. if the fed count is greater than 2), it should be over-full and not want to eat anymore (print an appropriate message and don’t increment fed if the critter is over-full)

    • there’s no such thing as too much playtime or too much petting, though!

  • sleep()

    • should print an appropriate message saying the critter is going to sleep

    • this method should decrease the each of the three counters by one, though none of them should ever go below 0

Example Output

Here are some examples of running the program:

Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-12.txt file in your cs21/labs/12 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.

When Remotely logged in

When you are ssh’ed into the CS labs, first quit any applications you are running, like vim, then simply type exit at the prompt in your terminal window to disconnect.

When Physically logged in

When you are in a CS lab logged into a CS machine. First quit any applications you are running, like the browser and the terminal. Then click on the logout icon (logout icon or other logout icon) 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 xlock 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!

Resources

Programming Tips

As you write programs, use good programming practices:

  • Use a comment at the top of the file to describe the purpose of the program (see example).

  • All programs should have a main() function (see example).

  • Use variable names that describe the contents of the variables.

  • Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

  • Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.

  • Avoid writing any lines of code that exceed 80 columns.

    • Always work in a terminal window that is 80 characters wide (resize it to be this wide)

    • In vim, at the bottom left in the window, there is an indication of both the line and the column of the cursor.

Function Comments

All functions should have a top-level comment! Please see our function example page if you are confused about writing function comments.

Are your files in the correct place?

Make sure all programs are saved to your cs21/labs/12 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/12
$ pwd
/home/username/cs21/labs/12
$ ls
Questions-12.txt
(should see your program files here)