CS 051 | Fall 2011 |
Instance Varibles are declared in the class file before any of the methods. All of the
examples we have seen so far have been labeled "private." These variables can be "seen"
anywhere in the class file. This is referred to as their scope.
Parameters are used to pass information to other methods. These other methods can even
be in other classes. Parameters come in pairs -- formal parameters are declared in the
header of the method to be called. This gives us a variable name to use in the method to refer
to them. Actual parameters are the actual values that are passed to the method.
Pretend we have written a program SortMyShirts. Below is a snippet of code from the
begin method of our program:
   public void begin( )
   {
       Tshirt myShirt = new Tshirt( 100, 100, canvas );
       myShirt.moveTo( 200, START_Y + OFFSET );
       ...
we are calling the "moveTo" method of the object named "myShirt." We are making this call from the
begin method of the SortMyShirts class. We are passing the new x position (i.e., 200) and the
new y position (i.e., START_Y + OFFSET) that we want moveTo to use to move myShirt.
200 and START_Y + OFFSET are the actual parameters.
The moveTo method is defined in the Tshirt class.
   public void moveTo( double newX, double newY )
   {
       ...
   {
The method header of the moveTo method in the Tshirt class defines two formal
parameters, newX and newY. Both of these variables are declared as type double.
When the call to moveTo is made, Java will associate the actual parameters with the formal
paramaters. You can think of this association as being done by executing the following statements
just before the call to the moveTo method:
   double newX = 200;
   double newY = START_Y + OFFSET;
Formal parameters are variables that are declared in a method header. Their scope is the whole
method, but nowhere else. That is, we can use the names of the formal parameters anywhere inside
the method that declared them, but no other methods or classes can use them.
Finally, local variables can be declared inside the body of any method. Like instance variables
and formal parameters, they require a type and a name for their declaration. Like formal parameters
(and unlike instance variables), they do not use the "private" label. They are used to hold values
that are only needed in the method, such as intermediate values used for some calculation.
Local variables have a scope that is defined by their enclosing curly braces (i.e., { and }).
Since all methods have enclosing curly braces, this means that local variables can never be used outside
of the method where they are defined. It does mean that it is possible to define them in such a way
(such as inside an "if" statement) so that they cannot even be used in parts of the method. You should
use great caution when defining your local variables.
For example:
   int next = colorSelector.nextValue();
   if(next == 0) {
       colorChoice = Color.RED;
   } else if (colorChoice == 1) {
       colorChoice = Color.ORANGE;
   } else if (colorChoice == 2) {
       colorChoice = Color.YELLOW;
   } else if (colorChoice == 3) {
       colorChoice = Color.GREEN;
   } else if (colorChoice == 4) {
       colorChoice = Color.BLUE;
   } else if (colorChoice == 5) {
       colorChoice = Color.CYAN;
   }
can be rewritten using the switch statement as follows:
   switch( colorSelector.nextValue() )
       case 0:
           colorChoice = Color.RED;
           break;
       case 1:
           colorChoice = Color.ORANGE;
           break;
       case 2:
           colorChoice = Color.YELLOW;
           break;
       case 3:
           colorChoice = Color.GREEN;
           break;
       case 4:
           colorChoice = Color.BLUE;
           break;
       case 5:
           colorChoice = Color.CYAN
           break;
   while( condition ) {
     ...
   }
The behavior is slightly different. An if statement checks the value of condition and then
executes the statements in its body if the condition is true. So, the statements in the body of the if
statement execute at most one time (and possibly not at all).
A while loop will check the value of condition and then execute the statements in in its
body if the condition is true. Then it will check the value of the condition again! If the condition
is true the second time, it will execute the statements in its body again. This process will continue until
the condition has a value of false.
This means that it is possible to have somthing called an infinite loop. And infinite loop occurs when
the condition never changes to false, and the loop body never stops executing. Thus, we must be
careful to make sure that some part of the body of the while loop can change the value of the
condition.
We looked at a very simple example in class called
Railroad that created the ties of a railroad
track each time we clicked the mouse, until all the ties were drawn. This accomplished our task, but
required a lot of input from the user.
Next, we changed the program to
use a while loop,
so that the computer would continue drawing
the ties until it was done.
Finally, we looked at a
basket drawing example where we were able to change the value
of two different variables by different amounts inside the body of the while loop to create
a basket on the screen.