CS51 - Spring 2010 - Lecture 2

  • http://googleblog.blogspot.com/2010/01/helping-computers-understand-language.html

  • Admin
       - updated TA office hours
       - if you added the class, go to the course web page and request an account

  • Exercise 1.9.3: Sketch the picture that the following lines of code would produce
       new FramedRect(20, 20, 160, 160, canvas);
       new Line( 20, 180, 100, 20, canvas);
       new Line( 100, 20, 100, 180, canvas);
       new FilledOval( 100, 100, 80, 80, canvas);
       new Line( 180, 100, 100, 100, canvas);

  • 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
          - 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
       - method parameters
          - a way to pass information along to a method
          - defined in the method header:
             - onMousePress(Location point)
          - can have multiple parameters, which are separated by commas
          - or none (i.e "()")
       - formal parameters vs. actual parameters
          - like we saw with setColor, we can pass information to the method
       - 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?
       - Location is a class... what other methods do you think it has?
          - getX();
          - getY();

  • 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!

  • discuss Lab 1 (http://www.cs.pomona.edu/classes/cs051/labs/lab1.noclicking/lab1.html)
       - due Monday at 11pm (will be the general trend)