CS51 - Spring 2010 - Lecture 2
http://xkcd.com/350/
Exercise 1.6.3
Write a complete program that will display ÒIÕm insideÓ when the mouse is inside the programÕs window and ÒIÕm outsideÓ when the mouse is outside the window. The screen should be blank when the program Þrst begins to execute and should stay blank until the mouse is moved in or out of the window.
Specifically:
what methods will we need in the class?
what will be inside those methods?
Note, there is more than one way to do this...
A few comments about the information I got from you guys last time
- For those with some previous programming experience, the first few weeks will be review, but I promise we'll get into some more interesting/challenging material soon
- Although we'll be learning in Java, the general skills you learn will be applicable to a variety of other languages. Learning new languages is often mostly a matter of learning a bit of new syntax.
- CS is a great compliment to almost all majors!
- most fields are becoming more and more data driven and use computers regularly
- CS is also about problem solving skills
A few notes about the lab
- hopefully we've worked out most of the login, etc. kinks and we can just focus on the labs here on out
- the reading is useful! Make sure to keep up with it for the labs.
- The labs can be a bit verbose at times, but make sure you read it and follow the directions closely.
- talk about lab process going forward.
Quick recap
- comments // or /* */
- ; end statements
- {} denote chunks of code
- import objectdraw.*;
- Classes a collection of instance variables and methods that have some related purpose
- methods are contiguous chunk of code that accomplishes a small task
- instance variables are a way to save information and pass information between methods
- Classes/objects we've seen so far:
- FramedRect
- FilledRect
- FramedOval
- FilledOval
- Text
- Line
- Location
- methods we've seen so far
- begin() method
- Mouse handling methods
- onMousePress(Location p)
- onMouseRelease(Location p)
- onMouseClick(Location p)
- onMouseMove(Location p)
- onMouseDrag(Location p)
- onMouseEnter(Location p)
- onMouseExit(Location p)
- class specific methods
- setColor
- getX
- getY
- hide
- show
- constructors (i.e. new FramedRect() or new Text())
- instance variables: three steps for using a variable
- declaration - private <type> <variable_name> (e.g. "private Text message;")
- assignment - "message = new Text(...);"
- use/reference to that variable/object - "message.setColor(Color.RED);"
show
Spirograph demo
- What's going on?
show
Spirograph code
- the Line constructor (new Line(...)) is overloaded. Note that it takes a different set of parameters this time:
- new Line(number, number, number, number, canvas); vs.
- new Line(Location, Location, canvas);
- how does onDrag work?
show
Scribble code
- what is different here from Spirograph?
- what will this do?
show
Scribble demo
Show
ClickCounterWithoutVars demo
- what do we need (discuss in groups)?
- text object in the middle
- numerical counter
- methods
- begin
- set counter to 0
- create the initial object with the text
- onMouseClick
- increment counter
- update text
Show
ClickCounterWithoutVars code
- built-in data types for numbers
- int (integers)
- double (decimal numbers)
- use what makes sense
- built-in data type variables vs. class variables
- classes have associated methods (message.setColor(...), point.getX(...))
- built-in data types have "operators"
- '+' operator
- what other operators do you think we have?
- '-' subtract
- '*' multiply
- '/' divide
- '%' modulus (remainder)
- we can combine operators in arbitrarily complex statements
- what is the value of: "2 * 3 + 4"?
- order of evaluation?
- %, /, *
- +, -
- () take precedence over everything, so use them wherever appropriate
- setText method
- What if we want to add "Click again" underneath the counter?
- What if we want to move both texts up?
- we would have to change both text objects :(
- any better way?
Show
ClickCounterWithVars code
- use private instance variables
- 150, 200, etc. are called literals
- functionality is the same
- easier to update
- just need to change the private variables in one place
- Is there any difference between displayX(Y) and count?
- displayX(Y) don't change
- they're called constants
- to indicate this "static final"
- all caps naming convention
- avoid having literals in code.... use constants!
show
ClickCounter code
- use constants!
- private static final
- all uppercase
show
Basketball demo
- what do we have (remember, we're going to try and use constants)?
- instance variables
- hoop constants (x, y, height, width)
- display location constants (x, y)
- oval (instance variable???)
- text (instance variable???)
- counter for the score
- begin
- score to 0
- set text
- set hoop
- onMouseClick
- if inside the circle... (how do we check this?)
- increment score by 2
- update the text
- when do we need instance variables?
- when we need to share information between methods
show
Basketball code
- setFontSize
- if statement: allows us to execute a section of code if a condition is true
if( <condition> ){
// execute this code if the condition is true
}
// then continue here (this code is always executed)
- again, {} indicate a contiguous unit of code