CS51 - Spring 2010 - Lecture 5

  • quiz (Exercise 4.8.6)

  • read the book!

  • Mixing doubles and ints
       - What is the type of?
          - 2
          - 2.5
       - What do you think will happen if I do the following?
          - private double d = 2;
             - java automatically type casts (changes) the int 2 to a double
          - private int i = 2.5;
             - java will complain. Why?
             - we can force it by manually type casting it to an int
                - private int i = (int)2.5
                - what do you think the value of i will be?
          - 1 + 1.0
             - Java will try to convert both to the other type
             - if one doesn't result in an error, then it will do that one
       - Math.round(double num)
          - Math.sqrt(double num)
          - Math.sin(double num)
          - Math.abs(double num)
          - ... http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html
       - What is the type and answer to?
          - 2 + 2
          - 2 + 2.0
          - 3/4
          - 50 * 2.0
          - 9/4
          - 9/4.0
          - 9/4.5
          - 9/(int)4.5

  • String type
       - I've said everything in java has a type
       - What is the type of "Score = "?
          - String
       - We've seen that we can use the '+' operator with Strings
          - "Score = " + score
          - String s = "this is a ";
             - s = s + "String";
       - String is actually a class
          - it's the only class in java that uses operators and literals
             - "this is a String literal"

  • checking equality
       - for built in types use "=="
          int num1 = 10;
          int num2 = 10;
          int num3 = 20;

          - what would the the value of the following expressions?
             - num1 == num2
                - true
             - num1 == num3
                - false
       - for classes, use the equals method
          Location loc1 = new Location(10, 10);
          Location loc2 = new Location(10, 10);
          Location loc3 = new Location(20, 20);

          - What would be the value of the following expressions?
             - loc1 == loc2
                - false
                - loc1 and loc2 are two different variables and are there for not '=='
             - loc1 == loc3
                - false
             - loc1.equals(loc2);
                - true
             - loc2.equals(loc1);
                - true
             - loc1.equals(loc3);
                - true

  • 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

  • show DragAShirt demo
       - similar to BallDrag demo except now we're dragging a shirt
       - do have the added functionality that it resets the location when we enter the window
       - how are we going to do this?
          - design a Tshirt class

  • Tshirt design
       - constants
          - size and location of the various objects
       - instance variables
          - body, neck, sleeves
       - constructor
          - double x, y locations
          - canvas (very often we'll need the canvas variable)
       - methods
          - move
             - moves everything
          - contains
             - need to check if the point is contained in any of the t-shirt parts
          - moveTo
             - again, we can leverage our move method

  • look at Tshirt class in DragAShirt code
       - note the contain methods checks the three different shirt parts

  • look at DragAShirt class in DragAShirt code
       - again, we can just create an instance variable of type Tshirt and then use it like we would other objects, because we have implemented that functionality

  • show Drag2Shirts demo
       - what do we need to do to get two shirts?
          - one of the biggest benefits of creating your own new classes is that we can reuse the functionality. Just need to created a second Tshirt object. If we'd done this all in one class, we'd have to rewrite a second set of variables, etc.
       - we have added some functionality to the Tshirt class. We can just go in and add some new methods:
          - added setColor method
             - return type?
                - void (it's a mutator method)
             - parameters?
                - Color
       - how are we going to handle dragging two different shirts? Before we had just used a boolean to check if we're dragging it or not.
          - could have a boolean for each shirt, i.e. draggingShirt1 and draggingShirt2
             - the if-else logic gets complicated if you do it this way
          - a better way is to have two Tshirt variables, selectedShirt and otherShirt and then one dragging variable whether or not we're dragging the "selected" shirt

  • show Drag2Shirts code
       - the major change is in the onMousePress method
          - if it's the selected shirt, just set dragging to true
          - if it's the other shirt, what do we need to do?
             - swap selected and other
             - set dragging to true
             - we'll bring the selected on to the front, so that the one being dragged is always on top
          - if it's neither, then dragging = false
       - note though that this keeps our onMouseDrag method simple
       - note that we can define our own methods (in this case reset) for the Tshirt class

  • Lab 3
       - design
          - it's important not only to lay things out, but to think about how things will interact
       - read the handout very carefully
          - look at the Pole class and make sure you understand it

  • other Admin
       - I'm out of town from Th-T
       - Professor Chen will be covering class on Th and T as well as the lab
       - I'll still be checking e-mail semi-regularly, so you can reach me that way
       - No office hours on Mon or Tue