- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
# Defining Classes (P.333)
class Bozo:
def __init__(self, value):
print("Creating a Bozo from:", value)
self.value = 2 * value
def clown(self, x):
print("Clowning:", x)
print(x * self.value)
return x + self.value
def main():
print("Clowning around now.")
c1 = Bozo(3)
c2 = Bozo(4)
print( c1.clown(3) )
print( c2.clown(c1.clown(2)) )
main()
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
# Useful List-specific Methods (P.345)
aList = [3, 1, 4, 1, 5, 9]
aList.insert(5, 8)
aList.insert(7, 8)
print(aList)
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
# Sorting a List (P.351--355)
class MyPoint:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return '(' + str(self.x) + ',' + str(self.y) + ')'
def descX(self):
return -self.x
def showPoints(aList):
for p in aList:
print( p )
def main():
points = [ MyPoint(1, 2), MyPoint(2, 7), MyPoint(3, 4),
MyPoint(12, 5), MyPoint(6, 8) ]
points.sort(key=MyPoint.descX)
showPoints(points)
main()