CS51 - Spring 2010 - Lecture 6

  • http://www.xkcd.com/793/

  • Admin
       - I will drop the lowest quiz score
       - Any issues with the TAs, grading or the office hours?
       - CS lunch tomorrow in Frary North
       - A few comments about the labs
          - use the in class examples
          - make sure you're caught up with the reading
          - if there's something you're confused about, come see me or Prof Bruce before the lab
          - spend plenty of time looking at the lab beforehand
          - designs
             - constants
             - instance variables (with type)
             - constructor (with arguments)
             - methods (with arguments and return types)
             - comments describing what the methods will do
             - NO other code (i.e. NO code inside the methods!)

  • local variables
       - why do we call them "private instance variables"?
       - what does "scope" refer to, with respect to variables?
       - what is the scope of a private instance variable?
          - the entire class
       - does it always make sense to have the scope of a variable be the entire class?
             - for example, we recently added card access for all of you to get into the Edmunds building.
                - do you think we allowed you access to all the buildings on campus?
                - of course not, because you only need access to Edmunds
             - scope has a similar concept. If you only need to use a variable in one method, then why have it accessible throughout the whole class
                - it can be confusing (the variables are all the way at the top of the class)
                - you might accidentally change it in another class, even though you didn't intend to
       - a "local" variable is a variable you define within a method, whose scope is only within the method
          public void onMouseRelease(Location point){
             int redNum;
             int blueNum;
             int greenNum;
       - they are declared INSIDE the method
       - unlike instance variables, they do not need private in front of them

  • show Railroad demo
       - each time we click, we add a new railroad tie
       - what methods will we have and what will happen in each of these methods?
          - begin
             - create the background
             - create the two rails
          - onMouseClick
             - each time we click, we want to create a new rail ADJACENT TO the old rail
             - how can we do this? what needs to change each time we click?
                - only thing that changes is the x-position of the tie
                - keep a private instance variable for this
                   - increment it each time
                   - make sure to initialize it somewhere!

  • look at Railroad code
       - a bunch of constants at the top (may be slightly overkill, but does make the code very easy to read and modify)
       - private instance variables
          - double tiePosition // to keep track of the x-position of the next tie
          - FilledRects for the rails (why do we need these at all?)
       - begin
          - create the background
          - create the two rails
          - initialize the starting tiePosition. How else could we have done this?
             private double tiePosition = FIRST_TIE_X;
       - onMouseClick
          - what does the if-statement check?
             - only creates ties as long as we're still creating them on the screen
          - 3 things:
             - create the tie using the tiePosition variable
             - send the rails to front (why do we need this?)
             - update tiePosition

  • show WhileRailroad demo
       - what if I wanted this to happen all at once without the user having to click?
       - very similar to what we did before
       - describe what we want to have happen in English...
          - as long as (while) we're not off the edge of the screen, create another tie

  • while loops
       while( <condition> ){
          // do these statements as long as <condition> is true
       }

       - has a similar structure to an if-statement, but it continues to "loop", that is, repeatedly execute the statements in the body, as long as <condition> is true
       - what would happen if I did the following?
          while( true ){
             // do something
          }
          
          - loop would never end... be careful!
          - make sure that inside the loop something will eventually change to make <condition> false

  • could this help us for our RailRoad problem?
       - we're going to want a while-loop in the begin method
       - try and write the while-loop...

  • look at WhileRailroad code
       - only have the begin method
       - 3 steps:
          - create the background
          - create all of the ties using a while-loop
             - as long as (while) tiePosition is less than the screen width, keep creating ties
             - just like before, create a new tie and then update tiePosition
             - why will this end?
                - tiePosition will start out as 5
                - each loop, we add 40
                - create ties at: 5, 45, 85, 125, ...
                - eventually, tiePosition will be greater than SCREEN_WIDTH
          - last thing: create the rails (why at the end?)
       - notice that I've made tiePosition a local variable
          - why is this ok?
             - only need it in the begin method
          - in general, if you only need a variable in one method, make it a local variable!

  • look at Knitting code
       - notice again the use of local variables
          - why is this ok?
       - what will happen when I click the mouse?
          - two key variables: numRows and numCols
          - nested while loops
             - what does the inner while loop do?
                - creates a row of FramedOvals
                - while numCols is less than SCARF_WIDTH
                   - create a new FramedOval
                   - set the color
                   - why do we have two variables to update?
                      - x keeps track of the x position
                      - numCols keeps track of the number of stitches drawn
             - after we exit the inner while loop what happens?
                - reset x so that the next row will start at 0
                   - what would happen if I didn't do this?
                - update the y variable to be down a row
                - reset numCols to 0
                   - what would happen if I didn't do this?
                - increment the number of rows that we've drawn (numRows)

  • show Knitting demo

  • Active objects
       - animation
          - how many people have ever drawn an "animation" in the corner of a book or with a stack of papers?
          - how do videos/movies work?
             - a succession of slightly different images are displayed
          - we can do the same thing with a while loop to create animations
             - update the display slightly
             - wait for some amount of time
             - repeat
       - we'll create a new class to do animations
          - the class should "extend" ActiveObject
          - will have at least two methods:
             - constructor
                - in the constructor, the last line should be "start()", which tells the animation to start running
             - public void run()
                - specifies what the animation should do
                - the "pause(...)" method which takes an integer representing the time in milliseconds to delay/pause
                - make sure to put in a pause, otherwise it will happen too quickly!

  • look at FallingBall class in PatheticPong code
       - what does the class do?
          - when a new FallingBall is created (i.e. new FallingBall(canvas)) creates a new ball in the middle of the screen at
       - note that we cannot execute this class by itself because it does NOT extend WindowController and therefore does not have a begin() method
       - two methods
          - constructor:
             - creates a new FilledOval for the ball
             - calls the start() method. Don't forget this!
          - run method:
             - what is the condition for the while loop?
                - keep looping as long as the ball is above the bottom of the screen
                

  • show PatheticPong demo
       - when I click the ball, I get a new FallingBall
          - what will the code look like for this?
       - how do I get the paddle to move?
          - notice that it just follows where the mouse is on the X axis
          - onMouseMove method!
             - move the x axis of the paddle as long as it's within the playing area
                - if it's outside the playing area:
                   - either don't move it (though this doesn't quite work right)
                   - move it right to the edge of the playing area wall

  • look at PatheticPong code
       - begin method
          - create the playing area
          - create the paddle
       - onMouseClick
          - create a new FallingBall
       - onMouseMove
          - three different cases
             - left of the playing area
             - right of the playing area
             - inside the playing area

  • public vs. private

  • look at Lab 4!