Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Exam Review Fall 2006: Working with classes

Post your questions here:


(a) You will see a red square (10px by 10px) with its top-leftmost pixel at the location (10,10) on a black 400px by 200px background.

(b)
def setColor(self, color):
    self.color = color


(c)
def setSize(self, number):
    self.size = number


(d)
def setPosition(self, x, y):
    self.position  = (x, y)


(e)You will see a blue square (10px by 10px) with its top-leftmost pixel at the location (10,10) on a black 400px by 200px background.

David Poore

Your part D would be correct, if the question said take in separate x and y coordinates. However, it asks you to take in a list or a tuple. Sweta Vajjhala



Would this be correct?
(d)
 def setPosition(self, list):
self.position=(list[0], list[1])


Why the heck is self.size listed twice in addRectFilled?!

self.size is listed twice because when creating a filled rectangele you have to set the width and the height . . . the first self.size sets the width to 10, the second sets the height to 10.

I tried the second solution posted above, but the code does not change anything on the canvas. here is the code i typed in, i got the same solutions for the other methods. everything else works, but not the setPosition

>>> john = Box()
>>> john.setColor(green)
>>> john.setSize(50)
>>> john.setPosition([30, 50])
>>> john.draw(canvas)
>>> show(canvas)
>>> john.setPosition([120, 150])
>>> repaint(canvas)
>>> show(canvas)
When you set the position, you are just updating x and y values. The canvas doesn't get updated until you draw. You need to add a call to draw before the second repaint. Colin Potts

would (d) be
def setPosition(self, list):
self.position=list
?
That's right. - Bobby Mathew


the position never changed on the first one. It was still set at (10,10). Which doesn't make sense to me.
How did you change the position and did you call that method that updated your position? - Bobby Mathew

Ok I tried using:

class Box:
  def __init__(self):
    self.setDefaultColor()
    self.size=10
    self.position=(10,10)
  def setDefaultColor(self):
    self.color = red
  def draw(self,canvas):
    addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
  def setColor(self, color):
    self.color = color
  def setSize(size,number):
    self.size=number

then
>>>joe = Box()
and if I try
>>>joe.setColor(blue)
or
>>>joe.setSize(20)
it returns "You are trying to access a part of the object that doesn't exist."

Whats the deal? Am I calling it wrong?

the variables for "setSize" are wrong.



Link to this Page