CS51 - Fall 2009 - Lecture 3

  • Spirograph example
       - What's going on?
       - Look at code
          - why do we need to use an instance variable?
             - share data between function
             - remember history
          - how does onDrag work?

  • Look at scribble code
       - what is the difference between the spirograph code?
          - rather than keeping the line start point the same, we set it to the end point of the last line
       - show scribble demo

  • Exercises
       - Scribble: remove all lines when you exit the window

  • ClickCounter demo
       - what do we need?
          - text object in the middle
          - counter!
          - methods
             - begin
                - set counter to 0
                - display text
             - on click
                - increment counter
                - update text
       - look at code
          - built in data types for numbers
             - int (integers)
             - double (decimal numbers)
             - use what makes sense
          - '+' operator
             - depends on input
             - defined by the left-most parameter
       - What if we want to add "Click again" underneath the counter?
          - add it
       - What if we want to move both texts up?
          - we would have to change both text objects :(
          - any better way?
             - instance variables
             - literals values (50, 100, ...)
       - look at ClickCounterWithVars code
          - functionality is the same
          - easier to update
       - Is there any difference between counterX(Y) and count?
          - counterX(Y) don't change
          - they're called constants
          - constants: to indicate this
             - "static final"
             - all caps naming convention
          - avoid having literals in code.... use constants!
       - look at ClickCounterWithConstants code
  • We saw '+', what other math functions do you think we have?
       - +, -, *, /
       - order of operation evaluation?
       - ++, --

  • basketball demo
       - what do we have (remember, we're going to try and use constants)?
          - Instance variables
             - hoop (x,y,height, width)
             - display location (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
  • basketball code
       - setFontSize
       - if statement: allows us to execute a section of code if a condition is true