CS51 - Spring 2010 - Lecture 5

  • robot soccer!
       http://www.youtube.com/watch?v=ICgL1OWsn58

  • Exercise 5.7.6

  • 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

  • what is the difference between a built-in data type (like int, double, boolean) and a class?
       - built-in data types have operators
          - +, -, *, ...
       - classes have methods
          - FramedRect rect = new FramedRect(...)
          rect.move(...)
          rect.setColor(...)
       - classes represent objects and therefore have underlying information
       - how we view variables of these types are slightly differently
          - a class variable is a reference to an object
          - a built-in data type variable actually holds that information directly

  • 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

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


  • 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 contains 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
       - let's walk through the basic design process
          - how many classes will we have?
             - Magnet class (representing the magnets), like our Tshirt class above
             - MagnetGame class, that extends WindowController to handle the mouse events
             - Pole class, to properly handle attracting and repelling (we give this to you)
          - design of the Magnet class
             - constants
                - locations, sizes, distances
             - variables: what makes up a magnet?
                - outer rectangle
                - two poles
             - constructor: what do we need to know to create a new magnet?
                - location
                - canvas

                public Magnet(Location upperLeft, DrawingCanvas canvas)

             - methods
                - move
                - moveTo
                - contains
                - getLocation
                - ...
          - design of the MagnetController class
             - constants
             - variables
                - magnet(s)
             - NO constructor: the class that extends the WindowController class does NOT have a constructor
             - methods
                - begin
                - onMousePress
                - onMouseDrag
       - notice that you create a Pole as follows:
          Pole myPole = new Pole (this, xLoc, yLoc, "N", canvas);
       
          - what does "this" refer to?
             - remember "this" refers to the current object
             - if we're in the magnet class, then this will be a magnet
          - what would the constructor to Pole look like (note, we've implemented the Pole class for you):

             public Pole(Magnet magnet, double x, double y, String label, DrawingCanvas canvas)
       
       - look at grading guidelines
          - only 8/20 for functionality!
          - design (will become increasingly more points)
          - style/organization are half of the points