CS 051 | Fall 2012 |
   public void moveTo( double x, double y )
   {
       this.move ( x - body.getX(), y - body.getY() );
   }
We also saw it in the solution to Homework 6.9.1, when we overloaded our constructor
method. We wrote a constructor that took x and y points to locate our
StickMan. Instead of repeating all that code for a constructor that worked with a
Location object, we simply used:
   public StickMan( Location loc, DrawingCanvas canvas ) {
       this( loc.getX(), loc.getY(), canvas );
   }
This example is very similar to some of the work that needs to be done in this week's Magnet Lab.
Parameters are used to pass information from one objecet (or method) to another. For example,
our call of the move method in onMouseDrag of Drag2Shirts:
selectedShirt.move(point.getX() - lastPoint.getX(), |
point.getY() - lastPoint.getY()); |
The actual parameter refers to the information being sent to the object. In the above example
there are two actual parameters:
   point.getX() - lastPoint.getX()
and
  point.getY() - lastPoint.getY()
These are associate with the formal parameters, xOffset and yOffset of the method:
  public void move(double xOffset, double yOffset) {
This association is done based on order. That is, the first actual parameter is associated with
the first formal parameter, and so on... In our example above, it is as if we are executing the
following assignment statements:
  xOffset = point.getX() - lastPoint.getX()
  yOffset = point.getY() - lastPoint.getY()
Formal parameters are only usable inside the method that defines them.
In this case, we would create a new local variable. Local variables are declared in a similar
way to instance variables and parameters -- we need to give them a name, and tell Java what type
they will be.
Unlike instance variables, they are declared inside a method.
Unlike formal parameters, they are not declared in the method header, but rather they are
declared in the body of the method.
Like formal parameters, they can only be used within the method that defines them, and in some
cases, may be even more limited than that. For this reason, the labels private and
public are not applicable and are never used with local variables.