Final Exam Review Fall 2004: Working with classes
a) a filled, red rectangle on a canvas of 400X200 at position 10,10 and of 10 pixels in length on each side.
e) same as a, but a blue rectangle instead of red
(b)Is this right? I do not exactly understand this part.
def setColor(self,colorchange):
self.color=colorchange
for (C) I would do the same thing only have self.size instead right?
| Seems ok to me. Have you tried to run it in JES? Kelly Lyons |
(D) I would have it take in (self,x,y) and then say self.position=(x,y). I do not quiet understand how this works.
| You are not supposed to take in an x and a y. Take in one argument that is a list of x,y so (self,list). To get at the first element of the list (i.e. x) you would want to do list[0] Hope that helps. Kelly Lyons |
b)Class Box:
def setColor(self,newcolor):
self.color=newcolor
D) i think it is suppose to look like this
def setPosition(self,position)
self.position[0]=position[0]
self.position[1]=position[1]
farnaz
Can anybody give me a hint on Farnaz's code? What's wrong with it, etc..
| Have you tried running it in JES? What if any errors is it giving you? Kelly Lyons |
does it need to re-specify the [0] and the [1] in the code? isnt that already implied in the "draw" function? what about this:
def setPosition(self,list):
self.position=list
I have entered these in Jes, and I can't get the methods to work
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 setSize(self,number):
self.size=number
def setColor(self,color):
self.color=color
def setPosition(self,list):
self.position=(list[0],list[1])
it says:You are trying to access a part of the object that doesn't exist.
AttributeError: instance of 'Box' has no attribute 'setSize'
| What are you typing when you can't get these methods to work? It just worked fine when I tried to run it. Kelly Lyons |
can someone please post some guidance on how to execute programs like this? I wrote my answers to the problem and they are very similar to the code above, but I don't know how to execute it. Thanks
you'll do:
Make the canvas and set a variable for the class box as with the example, then
>>>variable.setSize(whatever size)
>>>variable.setColor(whatever color)
>>>coordinates=[whatever coordinate, whatever coordinate]
>>>variable.setPosition(coordinates)
then you can draw the box as the example
Does that help? Summer McWilliams
b.
class Box:
(put in below setDefaultColor(self):)
def setColor(self, newcolor):
self.color = newcolor
c. put in same place as above
def setSize(self, newsize):
self.size = newsize
d. put in same place or below other two
def setPosition(self, newposition):
self.position = newposition
I keep getting this error when i try this problem:
You are trying to access a part of the object that doesn't exist.
I keep getting that same error as well...WHY?!?!
| That means that you are trying to call a method or set a variable that doesn't exist. What method are you trying to invoke? Is it properly indented so that is contained inside the class? Kelly Lyons |
I'm getting the same thing. It is properly indented and when i call the method I am calling "joe.setSize(15)" or "joe.setColor(blue)"
when i type in the new defaults, nothing changes, it still reads the first one (the box stays red, instead of changing colors). do you get rid of the old defualt for color or is that still written in the program?
yes, i told it to override the old one when it asked
I had the same problem (You are trying to access a part of the object that doesn't exist). After you've updated your code, try making a new instance of Box and all the changes you've made should work.
ok, so i didn't touch the code at all, but i shut down jes and when i started it again and executed "joe.setSize(100)" it worked this time–no error message
Link to this Page