# CSci 1133 Lab Exercise 11 Introduction to Objects

\$30.00

## Description

Object-oriented programming is a powerful computational paradigm in which we approach problem
solving from the perspective of how objects (Abstract Data Types) interact rather than the step-by-step
procedural approach we’ve been exploring so far. Our exploration begins with the construction of simple
representations (classes) that Python uses as “templates” or “recipes” for instantiating objects.
Primer on Printing Objects
Let’s create an object and explore it a bit. Using your IDE editor, enter the following class:
class rational:
def __init__(self,num=0,den=1):
self.numerator = num
if den == 0:
self.denominator = 1
else:
self.denominator = den

This defines a class of objects that will represent rational numbers, i.e., those that can be expressed as the
ratio of two integers. Now, using the command line Python environment, let’s instantiate two rational
objects:
>>> num1 = rational(3,4) [ENTER]
>>> num2 = rational(1,3) [ENTER]
Let’s see what they look like:
>>> num1 [ENTER]
Hmmm… that’s not what we wanted. Python is telling us that the “value” of num1 is a
main.rational object. Perhaps if we print the value…
>>> print(num1) [ENTER]
…still not what we wanted. How do we actually display the value of the object??! Since this is a thing
we’ve created ourselves, Python has no idea what we mean by “printing” a rational object. Like
nearly everything in object-land, we have to tell Python explicitly what we mean. It turns out many
functions in Python have anticipated that we might be creating weird objects of our own design, so
polymorphism (we’ll learn about it later) is employed to provide a “hook” into the future…
For now, you need to know that if some Python function needs a “value” to represent your object, it will
obtain a string representation by calling the __str__() method of your class.
If we want built-in functions such as print to display the “value” of a rational object, we must
supply a method named __str__() in our class definition. Add the following method to the rational
class definition:
def __str__(self):
return str(self.numerator) + ‘/’ + str(self.denominator)
Now let’s try it again:
>>> num1 = rational(3,4) [ENTER]
>>> print(num1) [ENTER]
Voila! Anytime we use print to display a rational number, it will display the current value as a
“fraction”
Warm-up
1). Modify the __str__() method for the rational class to display fractions such as 3/1 and 4/1 as
“whole” numbers rather than fractions. Also, if the numerator is zero, display 0 rather than 0/n:
Stretch
1). Construct a class named measure that will keep track of measured distances in feet and inches.
Include a constructor that will initialize two instance variables to hold integer values for the feet and
inches respectively. Your constructor should allow the following initialization forms:
measure() zero feet, zero inches
measure(4,5) four feet, five inches
measure(48) forty-eight inches ( or four feet )