Exam II Review

Tom Kelliher, CS 116

Nov. 17, 2000

Administrivia

Announcements

Assignment

Finish GalaEvents part of Lab 6 by start of class, Monday after Thanksgiving. Don't submit anything yet.

From Last Time

Lab 6.

Outline

  1. Exam review.

  2. Continuation of Lab 6.

Coming Up

Exam.

Exam Review

Four concept questions: events, boolean and comparison expressions, variables and scope, parameters and return values.

Two code writing questions: write a class (similar to last quiz), write a small applet (event handling).

  1. Methods: Parameter passing and return values.
    1. Formal and actual parameters.

    2. The return statement. A method's return type.

    3. Example:
      public void caller()
      {
         int answer,  b = 5,  c = 12;
      
         answer = callee(b, c / b + 5);
      }
      
      public int callee(int i, int j)
      {
         if (i > j)
            return i - j;
         else if (i < j)
            return return j - i;
         else
            return -1;
      }
      
      Assume that execution begins in caller(). What values are assigned to i, j, and answer? Answer: 5, 7, and 2. Try with different values of b and c.

  2. Variables: Local variables, instance variables, scope.
    1. Variable types: primitive, classes.

    2. When to use local and instance variables. Access to instance variables.

    3. Scope of variables. What about parameter scope?

    4. Example:
      // Instance variables:
      int a = 1,  b = 2,  c = 3;
      
      public void method1()
      {
         int a = 0,  b = 4;
      
         a = method2(b, a * c);
      }
      
      public int method2(int a, int b)
      {
         int c = 5;
      
         return a + b + c;
      {
      
      Assume that execution begins in method1(). What value is assigned to a in method1()? Determining the values assigned to method2()'s formal parameters will help.

  3. Boolean and comparison operators: evaluation, precedence, associativity.
    1. Unary vs. binary operators. Operand types.

    2. Boolean operators: !, && ||.

    3. Comparison operators: <, <=, >, >=, ==, !=.

    4. Precedence (memorize):
      1. Unary +, -, ++, --, !, (type) (right)
      2. *, /, % (left)
      3. Binary +, - (left)
      4. <, <=, >, >= (left)
      5. ==, != (left)
      6. && (left)
      7. || (left)

  4. The if statement.

  5. Events: The four things an applet must have for handling events:
    1. import java.awt.event.*;

    2. A listener class: implements XXXListener

    3. Event sources must register: source.addXXXListener(listener);

    4. The listener class must implement the interface:
      public void actionPerformed(ActionEvent e)
      {
         // Code to deal with each source's event must be included
         // here.  The code will have to determine which source generated
         // the event.
      }
      

    Example: See EtchASketch.java
    ( http://phoenix.goucher.edu/~kelliher/f2000/cs116/applets/EtchASketch.java).

  6. Classes/Interfaces, components, methods to be familiar with:
    1. Applet, ActionListener, String, ActionEvent.

    2. Button, Label

    3. setBackground(), setForeground(), setText().

    4. add(), addActionListener(), actionPerformed(), getActionCommand().

    5. equals().

Continuation of Lab 6



Thomas P. Kelliher
Thu Nov 16 17:22:53 EST 2000
Tom Kelliher