CS51 - Fall 2009 - Lecture 5
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
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
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
- ++, --
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(0, 255);
ColorScribbler demo
time permitting, "What is computer science?"