CS51 - Spring 2010 - Lecture 4

  • quiz (Exercise 4.8.6)

  • A few admin things:
       - Assignment 2 due this evening
       - Assignment 3 will be out today (look at it before class on Wed!)
       - Halona trip

  • Quick terminology review
       - class
          - describes a class or collection of objects. All of the demos that we have seen have been classes. All of the object types we've used (like FramedRect, Location, etc.) are classes.
       - methods
          - Classes contains methods. Methods describe the functionality of the class, that is, what the class objects can do. Methods are how things happen!
       - variables
          - Variables reference/represent things. In Java, we can either have variables that reference objects or variables that store built-in data types (like int, double, boolean).
          - what are the three basic steps for using a variable?
             - declare
             - assign
             - use
          - what is the "scope" of a variable?
             - where we can use it. Generally, this is the "block" of code in which it is declared.
          - what are the 2-3 different kinds of variables we've seen?
             - private instance variables
             - constants
             - member variables

  • 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")

  • What is an expression? A statement? What is the difference?
       - An expression is a phrase that describes an object or a value
          - literals
             - 17
             - true
          - variables
             - counter
             - carryingBall
             - display
             - hoop
          - object constructors
             - new Text(...)
          - some method invocations (e.g. accessor methods)
             - point.getX()
             - box.contains(point)
       - A statement instructs Java to perform some action
          - some method invocations (e.g. mutator methods)
             - rect.move();
             - display.setText(...);
          - variable assignment
             - counter = 17;
          - control statements
             - if( ... ){}
          - object constructors
             - new Text(...)

  • Where can we use statements and expressions?
       - our code consists of a series of standalone statements (think commands)
          - it doesn't make sense to have an expression by itself
             - 17;
             - carryingBall;
       - expressions
          - assigning a value to a variable
             - rect = new Text(...);
          - as parameters to method invocations
             - rect = new FramedRect(X_OFFSET, Y_OFFSET, ...)
          - if and if-else statements
             - if( rect.contains(point) )
             - if( true )
             - if( carryingBall )

  • Are all expressions the same? Can we use them interchangeably?
       - No. Java is what is called a "strongly" typed language
       - each expression has a "type" associated with it
       - For the expressions above, what are the types?
       - For the following phrases, what are the types we can use:
          - new FramedRect(?)
          - Text display;
             - display = ?
          - int count;
             - count = ?
          - if( ? )
          - ? && ?
          - ? + ?
       - For all of these we can substitute ANY expression that represents that has the expected type
          - boolean
             - if( true )
             - if( rect.contains(point) )
             - if( carryingBall )
          - int: "private int count;"
             - count = 17;
             - count = count + 1;
             - count = count * 20;
             - count = count + 2 * count;
             - count = rect.getWidth();

  • What types have we seen so far?
       - objects
          - FramedRect (FilledRect)
          - FramedOval (FilledOval)
          - Location
          - Line
          - Text
          - Color
       - primitive types
          - int
          - boolean
       - What are the differences between object variables and built-in?
          - rect = new FramedRect(...) vs. counter = 16
          - no methods associated with primitive types (counter.?)

  • show Better3PointBasketball demo
       - what do we need to add?
          - variables
             - constants for the lines
                - length
                - positions
             - lines
             - keep track of if we've crossed either of the 3 point lines (what type of variable?)
          - begin
             - add lines
          - onMousePress
             - set crossed 3 point line to not crossed
          - onMouseDrag
             - check if we've crossed the 3 point lines
             - if so, set the boolean to true
          - onMouseRelease
             - 2 cases for scoring points
             - if-else statement

  • show Better3PointBasketball code
       - variables: we can define constants with respect to other variables
       - onMouseDrag: > and <
          - what other types of math inequalities might we ask? >, <, <=, >=, ==, !=
       - onMouseRelease
          - if-else statement
       - += operator
       - ++, --

  • show ClassyBasketball demo
       - What would we need to change from our old EvenBetterBasketball code ?
          - begin()
             - add all of the new lines and the frame around the ball
          - onMouseDrag()
             - move calls for all of the lines
          - onMouseRelease()
             - moveTo calls for all of the lines

  • There's a better way to do this: create our own class
       public class NameOfClass{
          // constants
          
          // instance variables

          // constructors

          // methods
       }

       - By convention, class names will start with a capital letter
       - Each class must go in a file by itself

  • Two things to think about when you're developing a new class
       - design/functionality: what functionality are we going to need/require?
       - implementation: how are we going to actually accomplish this?

  • When you're designing a new class you need to think about what functionality the class needs
       - What information do you need to create a new object of that class? This information will need to be parameters for the constructor.
       - What methods do we need? The methods add functionality to the class.
          - A method header has four key parts:
             public <return_type> methodName(<parameters>)

             - public
             - return type of the method
             - the name of the method (i.e. what do we need to type to call it)
             - parameters of the methods, which is a comma separated list of a type (like double, Location or FramedRect) followed by the variable name


  • BBall class design
       - class header
       - constants
          - Color of the ball
          - information about the shape of the arcs
       - instance variables
          - oval and lines
       - constructor
          - double x, y location where is should be created
          - double size
       - move method
          - return type?
             - mutator methods often don't have a return type, which we write as "void"
          - parameters
             - double x, y distance it should be moved
       - contains method
          - return type?
             - boolean (true or false)
          - parameters?
             - Location point
       - moveTo
          - return type?
             - void
          - parameters
             - double x, y

  • BBall implementation: What needs to happen in each method?
       - constructor
          - create all of the objects
       - move
          - move the oval and all of the lines
       - contains
          - just check if that point is contained within the ball
       - moveTo
          - moveTo the oval and all of the lines

  • look at BBall class in ClassyBasketball code
       - Looks like the other classes we've seen except doesn't extend WindowController
       - we have all of our constants and instance variables, just like our other classes we've seen
       - constructor
          - The constructor doesn't have a return type and has the same name as the class name
          - we need the canvas so that we can create new objects using it
          - will be called when someone says "new BBall(...)"
       - moveTo method
          - we could call moveTo on the oval and all of the lines
          - we could also just use our move method by calling (this.move(...)) and save ourselves some typing
             - "this" refers to the current object (i.e. the methods within this class)
       - We actually have two moveTo methods. What's the difference?
          - one takes two doubles as parameters and one takes a location
          - this is called an "overloaded" method
             - why can we do this?
                - java is strongly typed, so it knows based on the parameters being passed which version of moveTo is being called
       - contains method
          - "return" is the special keyword in Java to tell the method that this is the value that will be returned when the method is called

  • look at ClassyBasketBall class in ClassyBasketball code
       - only change we need to make from EvenBetterBasketball code is to change the variable for the ball from FramedOval to BBall