Assignment, Conditional Statements

Tom Kelliher, CS 116

Oct. 25, 2000

Administrivia

Announcements

Friday's quiz: identifying the type, value, and validity of expressions involving the operators we've seen. A precedence/associativity table will be provided.

Assignment

Nothing new.

From Last Time

Logical operations

Outline

  1. Assignment operators.

  2. if statement

  3. Lab 5.

Coming Up

Continuation of Lab 5.

Assignment Operators

  1. A common problem:
    if (a = 3)
       // ...
    

  2. Simple assignment. =: binary operator, right associative.

    LHS must be a variable. RHS must be an expression of appropriate type.

  3. Assignment with operation. +=, -=, *=, etc.

    Ok, name three ways of increasing i by one.

Primitive Variables vs. Class Variables

Primitive variables hold values. Class variables hold references (memory addresses). You don't get what you expect when you assign class variables.

Examples. Diagram (memory allocation) the following:

  1. Primitive variables
    int a = 3;
    int b = 5;
    
    b = a;
    b++;
    
    What are a's and b's values?

  2. Class variables
    Point a = new Point(3, 3);
    Point b = new Point(5, 5);
    
    b = a;
    b.x++;
    b.y++;
    
    What are a's and b's values?

if Statement

  1. Sequential execution.

  2. Conditional execution:
    if some condition is true
       execute statements
    

  3. Syntax:
    if (<boolean expression>)
       one_statement;
    
    if (<boolean expression>)
    {
       statement_1;
       statement_2;
       // ...
       statement_n;
    }
    

  4. Examples:
    if (count > 0)
       average = (double) sumScores / count;
    
    if (83.0 <= average && average < 87.0)
       gradeLabel.setText("B");
    

  5. Avoid the following:
    if (n < 0);
       count++;
    
    What's the problem?

Applet Example

Write an applet which sums positive integer inputs. The applet should ignore non-positive inputs. The sum of the integers should be displayed.

//======================================================================
//      Project:    CS 116, if statement example
//      Author:     Tom Kelliher
//      File:       SumPos.java
//      Purpose:    This applet demonstrates the use of the if
//                  statement.  User input consists of a TextField and
//                  an "add" button.  Positive integers input through
//                  the TextField are added to a sum when the add button
//                  is pressed.  The sum is displayed.  Non-Positive
//                  integers are ignored.
//======================================================================


import java.applet.*;
import java.awt.*;
import java.awt.event.*;


//======================================================================
//      Class:      SumPos
//      Purpose:    See purpose above.
//======================================================================

public class SumPos extends Applet implements ActionListener
{
   Label title = new Label("Sum Positive Integers");

   // Input panel.
   Panel inputP = new Panel(new GridLayout(1, 2));
   Label inputL = new Label("Enter integers:");
   TextField input = new TextField(15);

   // Sum output.
   Label sumL = new Label();

   // Pressing this will cause the input to be read and processed.
   Button addB = new Button("Add");

   int sum = 0;


   //===================================================================
   //   Method:     init
   //   Purpose:    Add the widgets to their containers, set the initial
   //               sum, and activate the button.
   //===================================================================

   public void init()
   {
      setLayout(new GridLayout(4, 1));

      add(title);

      add(inputP);
      inputP.add(inputL);
      inputP.add(input);

      add(sumL);
      sumL.setText("sum: " + sum);

      add(addB);
      addB.addActionListener(this);
   }


   //===================================================================
   //   Method:     actionPerformed
   //   Purpose:    Handle button presses.
   //   Input:      e, an event.  We can safely ignore this for there
   //               is only one possible event -- an add button press.
   //===================================================================

   public void actionPerformed(ActionEvent e)
   {
      int inputI;

      // Read the input integer.
      inputI = Integer.parseInt(input.getText());

      // Ignore non-positive inputs.
      if (inputI > 0)
      {
         sum += inputI;
         sumL.setText("sum: " + sum);
      }
   }
}



Thomas P. Kelliher
Tue Oct 24 13:55:59 EDT 2000
Tom Kelliher