CS51 - Spring 2010 - Lecture 3
http://xkcd.com/292/
Exercise 2.7.7
- Look at the two pieces of code. Do they produce different outcomes? If the outcomes are different, explain the difference.
a. public void onMouseClick(Location point){
new Text("Hello.", point, canvas);
}
public void onMouseClick(Location clickPoint){
new Text("Hello.", clickPoint, canvas);
}
b. Assume clickPoint is an instance variable of type Location that has been initialized in a begin method
public void onMouseClick(Location point){
point = clickPoint;
new Text("Hi", clickPoint, canvas);
}
public void onMouseClick(Location point){
clickPoint = point;
new Text("Hi", clickPoint, canvas);
}
Notes on style
- use indenting
- if you select all of your code and then type CMD+i (CMD = the apple key), Eclipse automatically indents for you!
- use whitespace
- put comments inside your code
show
SimpleBallMove code
- what will this do?
- move method: move(int x_amount, y_amount);
- moves an object relative to current position
- mutator, accessor or constuctor method?
- other moving method: moveTo
- moveTo(int x, int y)
- moveTo(Location point)
show
SimpleBallMove demo
show
BallDrag demo
- how can we do this (hint: think about scribble)?
- onMousePress
- keep track of where we pressed the mouse
- onMouseDrag
- if where we pressed the mouse was inside the ball
- move it by the difference between where it was and where we are now
look at
BallDrag code
- why couldn't we just use "ball.moveTo(point)" in onMouseDrag?
show
BetterBasketball demo
- we can combine BasketBall and BallMove. What do we need to change?
- onMouseRelease
- check if we're carrying the ball
- check if we're inside the hoop
- if both true
- update score
- update display
- move the position of the ball back to the starting point
BetterBasketball is pretty good, but I find "if( ball.contains(lastMouse))" to be a bit confusing. What are we really asking here?
- Are we carrying the ball or not?
- We can save this with a "boolean" variable. Another built-in data type (like int or double). It has two values it can take:
- true
- false
- How can we change our code to use a boolean instance variable?
- add the boolean instance variable
- in onMousePress, see if we clicked inside the ball. If so, then we're carrying the ball. Set the boolean instance variable accordingly.
- in onMouseDrag, just have to check if we're carrying the ball, using the boolean instance variable.
show
EvenBetterBasketball code
- functionality doesn't change, but makes it a bit easier to read
- notice that ball.contains(points) returns/gives us a boolean
- we can just assign the value of carryingBall
- nested if statements
- AND ('&&') and OR ('||') can be used to combine boolean expressions (they should feel natural)
- show truth tables
show
Color4Scribbler demo
- what is the difference between this version and scribbler?
- onMousePress
- pick a random color from 4 options
- keep track of which color we're current using with an instance variable (what will be the type?)
- onMouseDrag
- set the color of the line to the current color we're using
show
Color4Scribbler code
- RandomIntGenerator class
- specify range inclusive in constructor
- nextValue() method gives you a random integer in range
- a shortcut: we can define a variable and set it to an object in one statement!
- private RandomIntGenerator colorPicker = new RandomIntGenerator(1, 4);
- if-else statement?
- just like an if statement except we add a block of code to execute if the condition is NOT true (that is, false)
- notice that we can chain them together
- when will the last "else" block get executed?
- only if all of the previous if statements are false
- == checks for equality
- another shortcut: notice that we can create the line and then call a method on the object. Why?
- the constructor returns an object
- notice that we can't chain them, i.e. "new Line(...).setColor(currentColor).moveTo(...)"
- setColor does NOT return a Line object
black ball is kind of boring: how can we change the color?
- in begin: ball.setColor(Color.ORANGE)
- the orange is a little off and what about arbitrary colors?
- how can we specify a color? RGB
- specify the amount of color between 0 and 255
- anyone guess why 0 to 255?
- ball.setColor(new Color(250,115,10))
- new Color object
- change code
look at
EvenBetterColorBasketball code
show
ColorScribbler demo
- why only 4 colors... why not many, many more :)
- how can we do this?
- RandomIntGenerator(0, 255)
- create a new Color with 3 random values
show
ColorScribbler code
- actually simplifies the code
Discuss lab 2
- you'll need to bring a design to the lab before hand (but only for part 1)
- program designs
- classes
- instance variables
- constants
- methods
- including constructors
- comments about what the method should do
- NO CODE in the methods
- be careful with your if/if-else statements
- should be able to leverage code from the examples we've seen this week
- identifying the correct basket
- it's ok to have two variables reference the same object
- just like you may have multiple ways that people my refer to you... first name, nickname, lastname, pet name, etc.