CS 115 Pre-Lab 13 Instructions

Deadline: Tu 4/28/2015 at 7 AM

Refer to the Week 14 reading (there is no new Week 15 reading) or other resources as necessary to complete this assignment.


Background

Object-oriented programming is all about defining our own data types and the operations they support. However, there are some limitations! Consider the code:

print(5)

Python knows exactly how to print this integer. More precisely, it knows how to internally convert that integer into its string representation, and then print that.

However, if you were to write something like:

s = Student('Example McStudent', [90, 100, 95])  # using the Student class from Lab 12
print(s)

...how is Python supposed to know how you would like this printed? It doesn't know anything about the Student data type beyond what you've provided in the class definition. In Lab 12, we got around this by defining our own print method for the class, so we could do s.print() to print the student in exactly the format we desired. In Lab 13, we'll solve this problem differently: by defining a special method of our class that will cooperate with Python's provided print function.

Printing isn't the only problem! Again, with Python's provided data types, Python knows exactly what to do with something like

if 100 > 87:

or

if 'Los Angeles' < 'San Diego'

because it knows how to compare integers and strings. But if what if you tried to compare two Student objects? Python will throw up its hands: how is it supposed to know what makes one student "less than" another? In Lab 13, we'll define another special method that tells Python how the < operator works if it's comparing two objects of our new class. There are similar methods for other operators, but we won't cover those in this lab.

Lab Overview

In this lab, we'll be defining a City class that stores a city's name and population. The class will only have three methods, and they'll all be special:

Answer Question 1 in the pre-lab.

Submit the pre-lab (due Tu at 7 AM)

Review your answers, and then click the Next button at the bottom of the Moodle quiz. Once you do that, you should see something similar to this:
Quiz attempt summary

Click the Submit all and finish button. You MUST do this so that your pre-lab can be graded! Once you have submitted your quiz, you should see something similar to this at the top of your Moodle window. The important part is that the State shows up as Finished.
Quiz confirmation