Tom Kelliher, CS 116
Oct. 25, 2000
Friday's quiz: identifying the type, value, and validity of expressions involving the operators we've seen. A precedence/associativity table will be provided.
Nothing new.
Logical operations
if
statement
Continuation of Lab 5.
if (a = 3) // ...
=
: binary operator, right associative.
LHS must be a variable. RHS must be an expression of appropriate type.
+=
, -=
, *=
, etc.
Ok, name three ways of increasing i
by one.
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:
int a = 3; int b = 5; b = a; b++;What are
a
's and b
's values?
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 some condition is true execute statements
if (<boolean expression>) one_statement; if (<boolean expression>) { statement_1; statement_2; // ... statement_n; }
if (count > 0) average = (double) sumScores / count; if (83.0 <= average && average < 87.0) gradeLabel.setText("B");
if (n < 0); count++;What's the problem?
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); } } }