Week 13: Classes

Announcements

  • Lab 10 due Monday night

  • Lab 11 is available now

  • Quiz 5 on Friday

  • Hackathon coming up on campus this weekend

Week 13 Topics

  • name == "main"

  • importing classes

  • The Fish class

Testing a Class

Just as with top-down design, you should practice incremental development when defining a class. Usually, this involves writing a function, thoroughly testing it, moving onto the next function, etc. There are several options for how to test, including using a separate main program. However, a common strategy is to include some test code at the bottom of your class definition file. This way, the test code can always be run in the future when updates are made. To do so, remember to include the following lines at the bottom of your file:

if __name__ == `__main__`:
  #Include testing code here

The first line ensures that the testing code does not run when you import the file, but rather only when someone calls the file from the command line:

$ python3 pizza.py

Exercises: Implement Pizza class and test

  1. Take some time to implement and test the addTopping() method. For now, only allow one topping per pizza. Also, make sure the pizza hasn’t yet been cooked.

  2. Analyze and test the serveSlice() function.

  3. Implement and test a removeTopping() function, which removes the topping if the pizza hasn’t yet been made.

  4. If you have time, modify the Pizza class to allow for multiple toppings. How does this change the data in the Pizza class? How would this change the methods?

Objects, scope, and internal method calls

Inside a class definition, the object is referred to as self. If we want to access its data, we use dot notation e.g., self.topping is the topping of the Pizza object. This means that the variable’s scope is the object itself, not the function that it was created.

It should be noted that the definition of an object includes data and methods. So, if we want to refer to a call a method we need to use dot notation since the method belongs to the object e.g., pizza.serveSlice(). But what if we want to call a method within another method? That is, within pizza.py, we wanted to call a method? Well, since self is the object and the method we want to call belongs to self, we can call it e.g., self.serveSlice()

As an example, take a look at the serveSlice() metehod in pizza_sol.py which calls self.makePizza().

Exercise: Team class

Implement a class called Team that stores and manipulates information about a competitive team (e.g., soccer, debate, baseball). Each team keeps track of its name, year, city, wins, and losses. In addition:

  • The constructor should take parameters for the name, year, and city. It should initializes the number of wins and losses to 0.

  • Write a method to create a string summarizing the team’s data (e.g., "2018 Steelers: 12 wins, 4 losses")

  • Include a getter for each data member

  • Have a method wonGame that increments the team’s number of wins by 1.

  • Similarly, have a lostGame that updates the number of losses.

  • Write a method, getWinPercent that returns the percentage of games won out of the total number of games played. If a team has played 0 games, return a winning percentage of 0.0. If a team has won 3 out of 4 games, return a winning percentage of 0.75

Exercise: Fish class

Implement and test a Fish class to build an aquarium.