## Final Exam Review Spring 2004: Working with Classes

REMOVEDnk back to Sp2004 Final Exam Review

which section is this covered in?

 11.4 Mark Guzdial

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)?
Student1002

b)
```class REMOVED:
def __init__(self):
self.color(newColor)
self.size=10
self.position=(10,10)
def setColor(self, newColor):
self.color=newColor
def draw(self,canvas):
[1],self.size,self.size,self.color)
```

then call in "joe" as joe.setColor=newcolor
c)
```class REMOVED:
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):

d)
```class REMOVED:
def __init__(self):
self.setDefaultColor()
self.size=10
self.position=(newPosition)
def setDefaultColor(self):
self.color = red
def setPosition(self, positionREMOVEDst):
self.position=positionREMOVEDst
def draw(self,canvas):
```

e) SadREMOVED is a subclass of REMOVED. It redefines the color to blue, but everything else about the box is the same.
Student1192

 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 REMOVED:
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, positionREMOVEDst):
self.position=positionREMOVEDst
def draw(self,canvas):

?
_```

opps my bad i forgot to format it

 Can you show me what the whole class might look like? Mark Guzdial

whole class?

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 REMOVED than just a color as input? Student940

 Why would you want to take REMOVED than color as an input? Mark Guzdial

```b)
class REMOVED:
def __init__(self):
self.Setcolor(newColor)
self.size=10
self.position=(10,10)
def setColor(self, newColor):
self.color=newColor
def draw(self,canvas):
[1],self.size,self.size,self.color)

c)
class REMOVED:
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):
d)
class REMOVED:
def __init__(self):
self.setDefaultColor()
self.size=10
self.setPosition=(newPosition)
def setDefaultColor(self):
self.color = red
def setPosition(self, positionREMOVEDst):
self.position=positionREMOVEDst
def draw(self,canvas):
```

I am a little confused about this one. Is this close at all? Is positionLIst the right thing to use in part d?
Student966

 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 REMOVED:
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):
```

Does it matter that there are no default settings,as there were in the original class?
Thanks.
Student1207

 This won't work – what is "number" and "colorInput" and "list"? Mark Guzdial

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.
Student1128

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 REMOVED:
def __init__(self):
self.setColor(newColor)
self.setSize(size)
self.setPosition(REMOVEDst)
def setColor(self, newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, REMOVEDst):
self.setPosition=REMOVEDst
def draw(self,canvas):
```

Student1128

 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

```class REMOVED:
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, REMOVEDst):
self.setPosition=REMOVEDst
def draw(self,canvas):
```

Wouldn't you need to add self.setColor(newColor), self.setSize(size), and self.setPosition(REMOVEDst) in the __init__ function? REMOVEDke this:
```class REMOVED:
def __init__(self):
self.setDefaultColor()
self.size=10
self.position=(10,10)
self.setColor(newColor)
self.setSize(size)
self.setPosition(REMOVEDst)
def setDefaultColor(self):
self.color = red
def setColor(self,newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, REMOVEDst):
self.setPosition=REMOVEDst
def draw(self,canvas):
```

What do you think? Am I totally off base, or is this correct? Student1007

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.
Student1007

How's this, Prof. Guzdial? I tried to take out the indentations of the new def statements.
```class REMOVED:
def __init__(self):
self.setColor(newColor)
self.setSize(size)
self.setPosition(REMOVEDst)
def setColor(self, newColor):
self.setColor = newColor
def setSize(self, size):
self.setSize = size
def setPosition(self, REMOVEDst):
self.setPosition=REMOVEDst
def draw(self,canvas):
```

Student1128

I think I may have just erased someone's posting with fixed indentations. I'm sorry!
Student1128

 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? Student1007

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 REMOVEDst are considered undefined and how you would fix that. Thanks! Student1007

 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 REMOVEDst. Yes, you have them as parameter variables, but then they don't exist in __init__. Mark Guzdial

```class REMOVED:
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, REMOVEDst):
self.setPosition=REMOVEDst
def draw(self,canvas):
```

```class REMOVED:
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, positionREMOVEDst):
self.position=positionREMOVEDst
def draw(self,canvas):