CS51 - Spring 2010 - Lecture 3

  • Exercise 2.7.7
       - Look at the two pieces of code. Do they produce different outcomes? If the outcomes are different, explain the difference.
          a. public void onMouseClick(Location point){
             new Text("Hello.", point, canvas);

          public void onMouseClick(Location clickPoint){
             new Text("Hello.", clickPoint, canvas);

          b. Assume clickPoint is an instance variable of type Location that has been initialized in a begin method
          public void onMouseClick(Location point){
             point = clickPoint;
             new Text("Hi", clickPoint, canvas);

          public void onMouseClick(Location point){
             clickPoint = point;
             new Text("Hi", clickPoint, canvas);

  • Notes on style
       - use indenting
       - use whitespace
       - put comments inside your code

  • Three basic types of methods
       - accessor methods: get information about the object
          - point.getX()
          - point.getY();
          - rectangle.getColor();
       - mutator methods: change the state of the object
          - message.setColor(Color.RED);
          - message.moveTo(100,100);
          - message.setText("now this is the message");
       - constructors: construct a new object (used with "new")

  • program designs
       - classes
       - instance variables
          - constants
       - methods
          - including constructors
          - comments about what the method should do
       - NO CODE in the methods

  • 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
          - mutator, accessor or constructor method?
       - 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

  • show SimpleBallMove code
       - what will this do?
       - move method: move(int x_amount, y_amount);
          - moves an object relative to current position
          - mutator, accessor or constuctor method?
       - other moving method: moveTo
          - moveTo(int x, int y)
          - moveTo(Location point)

  • show SimpleBallMove demo

  • show BallDrag demo
       - how can we do this (hint: think about scribble)?
          - onMousePress
             - keep track of where we pressed the mouse
          - onMouseDrag
             - if where we press the mouse was inside the ball
                - move it by the difference between where it was and where we are now

  • show BetterBasketball demo
       - we can combine BasketBall and BallMove. What do we need to change?
          - onMouseRelease
             - check if we're carrying the ball
             - check if we're inside the hoop
             - if both true
                - update score
                - update display
             - move the position of the ball back to the starting point

  • show Color4Scribbler demo
       - what is the difference between this version and scribbler?
          - randomly picking colors
             - RandomIntGenerator class
                - specify range inclusive in constructor
                - nextValue() method gives you a random integer in range
          - set the color each time we click
             - pick a random integer between 1 and 4
             - if-else statement?
             - == checks for equality
             - keep track of which color we're current using
          - onMouseDrag
             - set the color of the line to the current color we're using
             - we don't actually need an instance variable to create change the color of a line. Again, we can do it all in one line.

  • Discuss lab 2
       - you'll need to bring a design to the lab before hand (but only for part 1)
       - be careful with your if/if-else statements
       - should be able to leverage code from the examples we've seen this week