Download presentation
Presentation is loading. Please wait.
1
UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 7 Java Fundamentals Operators and Expressions IntroductoryAWT, Swing Control Structures Wed. 9/20/00
2
Java Fundamentals Operators and Expressions
3
Some Java Operators [Deitel, p. 271; complete list is in Appendix C] Precedence: Operator higher up the chart is evaluated before operator lower down the chart Associativity: Order of evaluation for operators of equal precedence
4
Expressions ä Sequence of operators and operands that specifies a computation ä May result in a value (e.g., 123, true) ä May cause side-effects (e.g., change a value) ä Compound expressions (e.g., (a*b)+c)) ä Includes literals (numbers or strings)
5
Java Expression BNF expression ::= numeric_expression | testing_expression | logical_expression | string_expression | bit_expression | casting_expression | creating_expression | literal_expression | "null" | "super" | "this" | identifier | ( "(" expression ")" ) | ( expression ( ( "(" [ arglist ] ")" ) | ( "[" expression "]" ) | ( "." expression ) | ( "," expression ) | ( "instanceof" ( class_name | interface_name ) ) ) ) From http://cuiwww.unige.ch/db-research/ Enseignement/analyseinfo/BNFweb.html
6
Statements ä Smallest “executable” unit ä Declaration statements ä Control statements ä Assignment statements ä Method invocation ä Compound statement (block) ä Semicolon-separated list of statements ä Enclosed in “curly” brackets { } ä Deitel calls it a ‘block’ only if it has declarations of its own
7
Java Fundamentals IntroductoryAWT, Swing
8
Java Abstract Windowing Toolkit GUI Components (from java.awt package) ä java.awt.Graphics (see list on p. 530-531) ä Given an object g of Graphics class: ä g.setColor( Color.red ); // Sets current drawing color to red ä g.drawString(“hello”, 200, 25); // Draws String starting at (200,25) ä g.drawLine(20, 28, 40, 10 ); // Draws line from (20,28) to (40,10) ä g.fillRect(100, 5, 20, 15); // Draws filled rectangle whose upper left // corner is at (100, 5). Width = 20. Height = 15 // corner is at (100, 5). Width = 20. Height = 15 ä g.drawOval(60, 9, 20, 13); // Draws oval whose bounding box upper left // corner is at (60, 9). Width = 20. Height = 13 // corner is at (60, 9). Width = 20. Height = 13 (0,0)x y hello usescurrentcolor
9
Java Swing GUI Components (from javax.swing package) ä javax.swing.JOptionPane ä Dialog box ä message ä error ä information ä warning ä question ä plain ä input ä javax.swing.JTextArea ä javax.swing.JScrollPane
10
Homework #2 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 HW# Assigned Due Content Homework is due at the start of lecture on the due date.
11
Java Fundamentals Control Structures
12
Program Control ä Sequential execution ä Instructions executed one after another ä Transfer of control ä Something other than the next instruction is executed
13
A Caution ä Flow of control has to be carefully thought out ä “spaghetti” code is hard to maintain ä No “goto” in Java ä goto is a keyword, but is not used ä Single-entry/single-exit programming
14
Single-entry/single-exit Programming ä Every logical “module” has a single defined entry point and a single defined exit point ä Control passes from the exit point of one “module” to the entry point of another ä Control structure stacking ä Control structure nesting
15
Selection (branching) ä if ä if/else ä switch
16
The “if” Structure ä Single selection ä If condition is true, do some special action ä If condition is false, continue
17
“if” Syntax ä boolean expression ä An expression that evaluates to true or false ä statement ä An arbitrary Java statement ä E.g., could be another if statement if (boolean expression) statement;
18
“if” Examples int j = 2; int a = 0; if (j < 3) { a = 4; } int j = 2; int a = 0; if ((j 10)) { a = 4; j = 55; if (a+j > 100) { // do something }
19
The “if/else” Structure ä Double selection ä If condition is true, do some special action ä If condition is false, do some other special action ä Then continue
20
“if/else” Syntax ä statement2 can be its own “if-statement” resulting in “if/else-if/else” nested logic ä An ‘else’ is always paired with a preceding ‘if’ ä The most recent previous one unless bracketed otherwise ä Similar to conditional operator (?:) ä boolean expression ? if-true-statement : if-false-statement ä It is good form to ALWAYS bracket statement1 and statement 2 (even when they are single-line statements) (even when they are single-line statements) if (boolean expression) statement1; else statement2;
21
“if/else” Examples int j = 2; int a = 0; if (j < 3) { a = 4; } else { a = 5; } int j = 2; int a = 0; if (j < 3) { a = 4; } else if (j = 3 { a = 5; } else // j >= 10 { a = 6; }
22
The “switch” Structure ä The switch structure embodies multiple selection logic (like a cascading if) ä Do one of an arbitrary number of actions based on the value of an expression ä NOTE: With object-oriented programming, we can use derived classes and polymorphism to get multiple selection behavior more elegantly ä Select on object type (class) ä Invoke the same named method of whatever class is involved at runtime ä More later in the course
23
“switch” Syntax switch (integer-expression) { case constant-expression : statement; … default : statement; } Requires condition to be expressable as an int (or convert to an int - e.g., byte, char, short) Uses labeled cases (also integers) Use break to ensure only one case is executed at a time Includes support for the C/C++ default label Can be nested
24
“switch” Examples int n = 10; final int ONE = 1, THREE = 3, FOUR = 4; switch (n) { case ONE : Sytem.out.println(“Value is 1”); break; case THREE : case FOUR : Sytem.out.println(“Value is 3 or 4); break; default : Sytem.out.println(“Value is not 1, 3, or 4”); break; // not strictly needed, but good form }
25
Repetition (iteration, or loops) ä for ä while ä do/while
26
The “for” structure ä Basic test-compute-increment control flow ä Includes “built-in” initialization and increment elements
27
“for” Syntax ä initialization is executed once upon entry ä test is evaluated each time through (including first time) ä If test passes, statement block is executed ä Else, execution continues after for statement ä increment is executed after each time through loop, then test is evaluated as above (“increment” can actually be a “decrement”) ä action-statement can be a single statement (or even an empty statement/no-op) ä Both initialization and increment can be comma-separated multiple statements ä E.g., for (i = 0, j = 0; i+j < 100; i++, j +=2) { // do something } ä loop variables can be declared in the initialization ä Unless you use a comma separated multiple-statement initialization ä For good form, ALWAYS bracket the action-statement, even if it is a single- line statement for (initialization; boolean test; increment) action-statement
28
“for” Examples for (int i = 10000; i < 0; --i); for (int i = 1; i < 10; i++) { System.out.println(“i = “ + i); } for (int i = 1; i < 10; i+=2) { System.out.println(“i = “ + i); } for (int i = 10000; i > 0; --i);
29
The “while” Structure ä Basic test-compute control flow ä Does not have built-in initialization or increment ä while loop “compute” statement(s) not guaranteed to execute
30
“while” Syntax ä boolean expression is evaluated ä if true, ä do statement (could be a compound statement) ä control flow then returns to boolean expression (for reevaluation) ä else, control flows to next statement after while statement ä No built-in increment!! ä For good form, ALWAYS bracket the statement, even if it is a single-line statement while (boolean expression) statement
31
“while” Examples int n = 10; while (n > 0) { System.out.println(“n = “ + n); n--; } int n = 10; while (true) { System.out.println(“n = “ + n); n--; if (n < 0) { break; } }
32
The “do/while” Structure ä Basic compute-test control flow ä Loop “compute” statement(s) guaranteed to execute at least once
33
“do/while” Syntax ä statement is executed ä boolean expression is evaluated ä if true, do statement block again ä else, control flows to next statement after do/while statement ä No built-in increment!! ä For good form, ALWAYS bracket the statement, even if it is a single- line statement ä Especially critical here for readability do statement while (boolean expression)
34
“do/while” Examples int n = 10; do { System.out.println(“n = “ + n); n--; } while (n > 0);
35
Related Statement Types ä labels ä break ä continue ä return
36
Labels ä Consists of an identifier followed by a colon ä Can be used to label: ä a repetition structure (for, switch, while, do/while) ä a block that is delimited by { } ä Can work in conjunction with break and continue (to modify repetition structure control logic)
37
The ‘break’ Statement ä Transfer control out of a switch, for, do or while statement or out of a labeled structure ä Only break out of the innermost block (no label specified) or out of the labeled statement or compound statement identified by the break ä Use basically same as C, C++ except for use with labels ä Some programmers avoid the use of break
38
‘break’ Example foo: for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { // do something; if (j+k > 10) { break foo; } // do something else
39
The ‘continue’ Statement ä Similar to break (alters control flow in for, do, while loops) ä Can be used with a label ä With no label - return to the boolean expression and reevaluate it (the for loop’s iteration statement is executed first) ä With label - return to the labeled for, do, or while and reevaluate as above ä Some programmers avoid the use of continue
40
‘continue’ Example foo: for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { // do something; if (j+k > 10) { continue foo; } // do something else
41
The ‘return’ Statement ä Exit from a method invocation ä Can return a value (or void) ä Control flows back to the next statement after the method invocation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.