CS51 - Spring 2010 - Lecture 4
Exercise 4.2.2
a. What would happen if you clicked the mouse inside square given the method below? Assume X_OFFSET is 60.
public void onMouseClick(Location point){
if( square.contains(point) ){
square.move( X_OFFSET, 0);
}
square.move( -X_OFFSET/2, 0);
}
b. outside square, given the method below?
public void onMouseClick(Location point){
if( square.contains(point) ){
square.move( X_OFFSET, 0);
}else{
square.move( -X_OFFSET, 0);
}
}
The code for 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
- in onMouseDrag, just have to check if we're carrying the ball
show
EvenBetterBasketball code
- functionality doesn't change, but makes it a bit easier to read
- nested if statements
- this seems awkward? What question are we asking here?
- AND ('&&') and OR ('||')
- show truth tables
What is an expression? A statement? What is the difference?
- An expression is a phrase that describes an object or a value
- literals
- 17
- true
- variables
- counter
- carryingBall
- display
- hoop
- object constructors
- new Text(...)
- some method invocations (e.g. accessor methods)
- point.getX()
- box.contains(point)
- A statement instructs Java to perform some action
- some method invocations (e.g. mutator methods)
- rect.move();
- display.setText(...);
- variable assignment
- counter = 17;
- control statements
- if( ... ){}
- object constructors
- new Text(...)
Where can we use statements and expressions?
- our code consists of a series of standalone statements (think commands)
- it doesn't make sense to have an expression by itself
- 17;
- carryingBall;
- expressions
- assigning a value to a variable
- rect = new Text(...);
- as parameters to method invocations
- rect = new FramedRect(X_OFFSET, Y_OFFSET, ...)
- if and if-else statements
- if( rect.contains(point) )
- if( true )
- if( carryingBall )
Are all expressions the same? Can we use them interchangeably?
- No. Java is what is called a "strongly" typed language
- each expression has a "type" associated with it
- For the expressions above, what are the types?
- For the following phrases, what are the types we can use:
- new FramedRect(?)
- Text display;
- display = ?
- int count;
- count = ?
- if( ? )
- ? && ?
- ? + ?
- For all of these we can substitute ANY expression that represents that has the expected type
- boolean
- if( true )
- if( rect.contains(point) )
- if( carryingBall )
- int: "private int count;"
- count = 17;
- count = count + 1;
- count = count * 20;
- count = count + 2 * count;
- count = rect.getWidth();
What types have we seen so far?
- objects
- FramedRect (FilledRect)
- FramedOval (FilledOval)
- Location
- Line
- Text
- Color
- primitive types
- int
- boolean
- What are the differences between object variables and built-in?
- rect = new FramedRect(...) vs. counter = 16
- no methods associated with primitive types (counter.?)
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
show
Better3PointBasketball demo
- what do we need to add?
- variables
- lines
- length
- positions
- keep track of if we've crossed either of the 3 point lines (what type of variable?)
- begin
- add lines
- onMousePress
- set crossed 3 point line to not crossed
- onMouseDrag
- check if we've crossed the 3 point lines
- if so, set the boolean to true
- onMouseRelease
- 2 cases for scoring points
- if-else statement
show
Better3PointBasketball code
- variables: we can define constants with respect to other variables
- onMouseDrag: > and <
- what other types of math inequalities might we ask? >, <, <=, >=, ==, !=
- onMouseRelease
- if-else statement
- += operator
- ++, --
show
ColorScribbler code
- what does this do?
- another shortcut: we can define a variable and set it to an object in one statement!
- private RandomIntGenerator colorPicker = new RandomIntGenerator(1, 4);
show
ColorScribbler demo