a == b
a != b
a < b
a <= b
a > b
a >= b
!p
p && q
p || q
ifif-elseifsSyntax:
if (CONDITION)
STATEMENT
Listing 5.1: Age
Syntax:
if (CONDITION)
STATEMENT1
else
STATEMENT2
(int) (Math.random() * 2)?Page 221: misleading indentation.
A block is a sequence of statements enclosed in {} braces:
{
STATEMENT1;
STATEMENT2;
...
STATEMENTN;
}
Block statements { ... } stand syntactically for a single statement.
generator.nextInt(MAX) + 1 ? // 1. Textbook style // 2. Alternate style
if (condition) if (condition) {
{ statement; ...
statement; ... }
} else {
else statement; ...
{ }
statement; ...
}
Avoid this:
if (condition) {
statement; ...
} else { // don't do this!!!
statement; ...
}
The else is hidden.
How to write:
// Textbook // Simpler?
if (c1) if (c1)
a1; a1;
else else if (c2)
if (c2) a2;
a2; else
else a3;
a3;
if does each else match?Study with inputs:
2 5 7
5 7 2
7 2 5
== requires exact equality, but floating point operations are inexact, so we usually want to find whether |a − b| < tolerance.
if (Math.abs(a - b) < tol)
...
else
...
The Unicode character set determines the ordering of characters.
==,!=, <, etc.Instead of ==, use the equals method:
s1.equals(s2) // value is boolean
Instead of !=, use
! (s1.equals(s2))
Instead of <, >, etc., use compareTo:
s1.compareTo(s2) < 0 // true if s1 less than s2
s1.compareTo(s2) == 0 // true if s1 equals s2
s1.compareTo(s2) > 0 // true if s1 greater than s2
The comparison is made on the basis of alphabetical order, or, to speak more precisely, lexicographic order, the order determined by the character codes.
Graphics applications
ActionEvent method getSource, which returns a reference to the emitting object.LeftRight, LeftRightPanel (Listings 5.12–5.13)
ItemEventItemListener can receive ItemEventsStyleOptions, StyleOptionsPanel (Listings 5.14–5.15)
Font class, representing a character fontFont.ITALIC + Font.BOLD, representing the combination of italic boldButtonGroupsActionEvent when toggled on, but not offQuoteOptions, QuoteOptionsPanel (Listings 5.16–5.17)
ButtonGroup and adding each radio button to the groupSyntax:
while (expr)
statement;
Semantics:
expr is true, keep executing statementwhile loop and a sentinel value to indicate end of inputwhile loop to validate input.expr never becomes false.statement makes expr eventually become false.C-c (control-C) to interrupt.Example:
int line = 1;
while (line < 5) {
char letter = 'a';
while (letter < 'f') {
System.out.print(letter);
letter++;
}
System.out.println(); // why?
line++;
}
left and right are updatedbreak or continue statement in a while statement is possiblebreak in switch statementscontinue at allif insteadNormal exit from a while loop implies the condition is false.
while (x > 0)
{
something;
if (x == 2)
break;
x--;
}
// assert x <= 0 ???
The break statement makes it harder to reason about code.
hasNext() → boolean, tells us if there are any more data.next() → Object, the next value in the sequence(Assume c is some type of collection)
Iterator citer = c.iterator();
while (citer.hasNext()) {
Object thing = citer.next();
// process thing ...
}
hasNextDoublenextDoubleUses nested loops, with a Scanner as iterator for each loop.
throws IOException because things can go wrong in opening the filejava.util.ArrayListSee Figure 5.8 for details.
ArrayList() // constructor
add(object)
add(index, object)
get(index)
set(index, object)
What is E?
ArrayList<E>()
E is the type of object we want the list to contain.
new ArrayList<String>()
new ArrayList<Integer>()
clear()
remove(index)
indexOf(object)
contains(object)
isEmpty()
size()
Demo in DrJava
if; conditional operatorwhile, iterators, array listsif to determine event sourcesif statements