Title: Lecture 7: More on Defining Classes
1Lecture 7 More on Defining Classes
2Another example
- Lets look at another custom graphics object
- The T-shirt
- This could be used to soup up your laundry
program - You could have shirts that look like shirts!
- Currently, all your clothes are square
- Not exactly a fashion statement, or at least not
the one you wanted to make
3T-Shirts
- TShirt is kind of like BBall
- It draws an object composed of six subpieces
- Its constructor creates all the subpieces and
associates names with them - Its move method moves them all
- Its moveTo method depends on its move method.
4T-Shirts
- There are also a few differences
- TShirts contains method can not just call the
contains method of one other object - It has some extra methods (setColor,
sendToFront). - You could pretty easily put this in your laundry
program instead of a FilledRect - Maybe just change the type
5Custom Classes
- Youve seen move() and moveTo() on rectangles and
other shapes - Hopefully this makes it easy to understand the
idea of defining your own methods in a new class - You shouldnt think that you can only define
methods like the existing ones - You should design methods that are appropriate
for the role of the object in your program
6Back to the Robot
- Robot might be one object
- made up of FilledRects for the arms, legs, body,
and head - Or, he might have custom Leg, Arm, Body, and Head
objects! - Each could have methods
- leg.move()
- arm.grab()
7Custom Classes
- You might also want a totally new method on BBall
- In our program, the ball always has to reset to
the starting point - How about a reset() method?
- This would put the ball back in its initial
position
8Adding reset() to BBall
- How would you add reset() to BBall?
- What instance variables would you need?
- How would you change the constructor?
- What would the reset() method look like?
9Adding reset() to BBall
- How would you add reset() to BBall?
- What instance variables would you need?
- How would you change the constructor?
- What would the reset() method look like?
double initX, initY
10Adding reset() to BBall
- How would you add reset() to BBall?
- What instance variables would you need?
- How would you change the constructor?
- What would the reset() method look like?
double initX, initY
initX top initY left
11Adding reset() to BBall
- How would you add reset() to BBall?
- What instance variables would you need?
- How would you change the constructor?
- What would the reset() mutator method look like?
double initX, initY
initX top initY left
public void reset() this.moveTo(initX,
initY)
12Adding reset() to BBall
- Now you can just change onMouseRelease() to call
reset(), in the class that extends
WindowController - Before
- After
public void onMouseRelease(Location
point) if ( hoop.contains(point)
ball.contains(lastMouse) )
score score 2
display.setText("Your score is " score )
ball.moveTo(BALLX,BALLY)
public void onMouseRelease(Location
point) if ( hoop.contains(point)
ball.contains(lastMouse) )
score score 2
display.setText("Your score is " score )
ball.reset()
13Variables vs Parameters
- reset() is a good example of the difference
between instance variables and parameters - In the original code, we construct BBall with its
initial position - And we have to pass the same information to BBall
when we call moveTo().
public void begin() ball new BBall(
BALLX, BALLY, BALLSIZE, canvas)
public void onMouseRelease(Location point)
if ( hoop.contains(point)
ball.contains(lastMouse) )
score score 2
display.setText("Your score is " score )
ball.moveTo(BALLX,BALLY)
14Variables vs Parameters
- Now look at the new version
- We still tell the ball the initial position when
we construct it - But it remembers the position in initX and initY,
so we dont have to tell it again when we call
reset().
public void begin() ball new BBall(
BALLX, BALLY, BALLSIZE, canvas)
public void onMouseRelease(Location point)
if ( hoop.contains(point)
ball.contains(lastMouse) )
score score 2
display.setText("Your score is " score )
ball.reset()
15Variables vs Parameters
public void begin() ball new BBall(
BALLX, BALLY, BALLSIZE, canvas)
- Parameters are used when one method needs to
transmit information to another method that it is
calling. - Instance variables are used when a method needs
access to information that was available at some
earlier point in a method call that is now
complete.
ball.moveTo(BALLX, BALLY)
ball.reset()
16Use assignments
- In reset(), we used instance variables that we
assigned values to in the constructor. - Because we remembered initX and initY, we can use
them later - This is how BBall remembers where it is
public BBall(double top, double left, double
size, DrawingCanvas canvas) . . . initX
top initY left
public void reset() this.moveTo(initX,
initY)
17Why?
public BBall(double top, double left, double
size, DrawingCanvas canvas) . . . initX
top initY left
- Names used for formal parameters are only
available in their method. - When we send an object a message (a method call),
the object receives the information via the
parameters - If you want to use them outside that method, you
have to save them somewhere else - Instance variables like initX and initY stick
with your object, even after the method is
finished executing
18This should be familiar
public void onMousePress(Location point)
dragging ball.contains(point) lastMouse
point
- Weve already done this before
- We had to remember lastMouse in our basketball
program - We needed this to compute the distance to move
the basketball when we dragged it
public void onMouseDrag(Location point) if
(dragging) ball.move(point.getX() -
lastMouse.getX(), point.getY() -
lastMouse.getY()) lastMouse point
19Local variables
- What if you want something like an instance
variable, but you only want to use it in one
method? - You can use local variables
- Your random numbers really should have been
local, but you didnt know how to do that yet. - Lets look at something similar
- Well make a 3-color version of Spirograph
20Local variables
- Well modify Spirograph to make its lines one of
three random colors. - Before, wed declare an extra instance variable
outside the methods, like this
int randomInt
public void onMousePress(Location point)
nextLineStarts point // pick the color
randomInt colorGenerator.nextValue()
if ( randomInt 0 ) lineColor
Color.white else if ( randomInt 1 )
lineColor Color.blue else
lineColor Color.red
21Local variables
- The entire object doesnt need randomInt
- Its only used in onMousePress()
- You can eliminate the instance variable and just
declare the random int inside the method
public void onMousePress(Location point)
int randomInt nextLineStarts point
// pick the color randomInt
colorGenerator.nextValue() if ( randomInt
0 ) lineColor Color.white
else if ( randomInt 1 ) lineColor
Color.blue else lineColor
Color.red
22Local variables
- Note that local variables dont need to be
labeled as public or private - They are only visible inside their own method
- Other objects can never access them
- Only the method uses them
public void onMousePress(Location point)
nextLineStarts point // pick the color
int randomInt colorGenerator.nextValue()
if ( randomInt 0 ) lineColor
Color.white else if ( randomInt 1 )
lineColor Color.blue else
lineColor Color.red