Final Exam Review Spring 2004: Working with Classes
Link back to Sp2004 Final Exam Review
Questions and Answers?
which section is this covered in?
I'm guessing that (a) would generate a 10x10, red, filled rectangle positioned at (10,10) in the 400x200 canvas.
Any hints on (b),(c),(d)?
Sun Moon Kim
b)
class Box:
def __init__(self):
self.color(newColor)
self.size=10
self.position=(10,10)
def setColor(self, newColor):
self.color=newColor
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position
[1],self.size,self.size,self.color)
then call in "joe" as joe.setColor=newcolor
c)
class Box:
def __init__(self):
self.setDefaultColor()
self.size(newSize)
self.position=(10,10)
def selfSize(self, newSize):
self.size=newSize
def setDefaultColor(self):
self.color = red
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
d)
class Box:
def __init__(self):
self.setDefaultColor()
self.size=10
self.position=(newPosition)
def setDefaultColor(self):
self.color = red
def setPosition(self, positionList):
self.position=positionList
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
e) SadBox is a subclass of Box. It redefines the color to blue, but everything else about the box is the same.
Karin Bowman
| That's not what I'm looking for on (b) – actually write the method, like in (c). But (c) has the wrong name of the method. Mark Guzdial |
class Box:
def __init__(self):
self.setColor()
self.size=10
self.position=(newPosition)
b.def setColor(self, newColor):
self.color = newColor
c.def setSize(self, newSize):
self.size=newSize
d.def setPosition(self, positionList):
self.position=positionList
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
?
_
opps my bad i forgot to format it
| Can you show me what the whole class might look like? Mark Guzdial |
whole class?
you can go to "edit this page" to format your code after you post it.
In part d) what is a "tuple"? I'm having a hard time finding it in the book.
Kelli Webb
| It's not in the book, but it came up in some of the discussions here. (1,2) is a tuple – it's like a list, but defined with () instead of []. There are some technical differences. By "whole class", I meant the class with all its methods. Mark Guzdial |
So one question in part b when it says take a color as input, is it ok put more than just a color as input? Amanda Cook
| Why would you want to take more than color as an input? Mark Guzdial |
b)
class Box:
def __init__(self):
self.Setcolor(newColor)
self.size=10
self.position=(10,10)
def setColor(self, newColor):
self.color=newColor
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position
[1],self.size,self.size,self.color)
c)
class Box:
def __init__(self):
self.setDefaultColor()
self.Setsize(newSize)
self.position=(10,10)
def setSize(self, newSize):
self.size=newSize
def setDefaultColor(self):
self.color = red
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
d)
class Box:
def __init__(self):
self.setDefaultColor()
self.size=10
self.setPosition=(newPosition)
def setDefaultColor(self):
self.color = red
def setPosition(self, positionList):
self.position=positionList
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
I am a little confused about this one. Is this close at all? Is positionLIst the right thing to use in part d?
Heather Symon
| It looks quite nice, Heather, but you could put all the methods into ONE class, not three separate ones. Mark Guzdial |
When you say you can put it into one class, do you mean:
class Box:
def__init__(self):
self.setColor(colorInput)
self.size(number)
self.position(list)
def setColor(self, colorInput):
self.color=colorInput
def setSize(self, number):
self.size=number
def setPosition(self, list):
self.position=list
def draw(self, canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.color)
Does it matter that there are no default settings,as there were in the original class?
Thanks.
Jennifer Blake
| This won't work – what is "number" and "colorInput" and "list"? Mark Guzdial |
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
What does the self.position[0] and self.position[1] do?
self.position[0] takes the first item in the list as the x-coordinate and self.position[1] takes the next item in the list as the y-coordinate. So if if you wanted to draw a rect at (100, 250) you would pass that in as 'list', and self.position[0] then would take 100 as the x-coordinate and self.position[1] would take 250 as they y-coordinate.
That's my understanding of it.
Jonathan Laing
This is what I have for b,c, and d, but I'm not sure how to fix the problem that you mentioned in Heather's code.
class Box:
def __init__(self):
self.setColor(newColor)
self.setSize(size)
self.setPosition(List)
def setColor(self, newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, List):
self.setPosition=List
def draw(self,canvas):
addRectFilled(canvas,self.setPosition[0],self.setPosition[1],self.setSize,self.setSize,self.setColor)
Jonathan Laing
| Your understanding of how the position works is right, Jonathan, but your class definition won't work at all. You've indented all the other methods inside of __init__, which means that they basically don't exist. Why are you changing __init__ at all? It worked just fine – leave it alone. Just add the other methods that you're defining. Mark Guzdial |
How about this?
class Box:
def __init__(self):
self.setDefaultColor()
self.size=10
self.position=(10,10)
def setDefaultColor(self):
self.color = red
def setColor(self,newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, List):
self.setPosition=List
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
Wouldn't you need to add self.setColor(newColor), self.setSize(size), and self.setPosition(List) in the __init__ function? Like this:
class Box:
def __init__(self):
self.setDefaultColor()
self.size=10
self.position=(10,10)
self.setColor(newColor)
self.setSize(size)
self.setPosition(List)
def setDefaultColor(self):
self.color = red
def setColor(self,newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, List):
self.setPosition=List
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
What do you think? Am I totally off base, or is this correct? Brittany Copeland
Okay, so the identing is not right at all....it looks right when I edit the page but then shows up like this. each def should be indented within the class and all the self stuff within each def should be indented. None of the functions should be nested within each other. You get the idea...sorry, not sure how to fix it.
Brittany Copeland
How's this, Prof. Guzdial? I tried to take out the indentations of the new def statements.
class Box:
def __init__(self):
self.setColor(newColor)
self.setSize(size)
self.setPosition(List)
def setColor(self, newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, List):
self.setPosition=List
def draw(self,canvas):
addRectFilled(canvas,self.setPosition[0],self.setPosition[1],self.setSize,self.setSize,self.setColor)
Jonathan Laing
I think I may have just erased someone's posting with fixed indentations. I'm sorry!
Jonathan Laing
| Jonathan, you have all these undefined variables in your __init__ that won't work. Brittany's and the one above it look really good. Brittany, the idea of USING the new methods is a GREAT idea, but you don't have to set the variables and THEN call them. Just all the new methods! Mark Guzdial |
So I should take out the first 3 lines of def__init__ and just have the three methods with their inputs? Brittany Copeland
Well, I just read Jonathan's posting again and if you were to do what I suggest in the above posting my code would look just like his...soooo by using the new methods instead of setting the variables and then calling them what exactly do you mean? I guess I just am confused about why newColor, size, and List are considered undefined and how you would fix that. Thanks! Brittany Copeland
| No, Brittany, you NEED to have __init__. You can call your methods to set the instance variables to their correct values, but the code you post leaves undefinded newColor, size, and List. Yes, you have them as parameter variables, but then they don't exist in __init__. Mark Guzdial |
class Box:
def __init__(self):
self.setColor(red)
self.setSize(10)
self.setPosition([10,10])
def setColor(self, newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, List):
self.setPosition=List
def draw(self,canvas):
addRectFilled(canvas,self.setPosition[0],self.setPosition[1],self.setSize,self.setSize,self.setColor)
class Box:
def __init__(self):
self.setColor(newColor)
self.Setsize(newSize)
self.Setposition(newPosition)
def setColor(self, newColor):
self.color=newColor
def setSize(self, newSize):
self.size=newSize
def setPosition(self, positionList):
self.position=positionList
def draw(self,canvas):
addRectFilled(canvas,self.position[0],self.position[1],self.size,self.size,self.color)
So if I put them all into one class it would look like this.
Heather Symon
Prof. Guzdial, I can't figure out how I need to define these variables. I can't see where I'm different than the other codes you mentioned-how are they defining the variables? I thought you just pass in a number, or a list ex [10, 10], or a pre-defined color for each of those variables.
How does one define the variables??
Thanks for all the help. Jonathan Laing
Link to this Page