Version 5.11
DRAFT: needs proofreading and material on array lists.
Two class days
Handouts: none
First, we'll look at conditionals. Then we'll skip ahead into the graphics section to see how conditionals can be applied in GUIs. Finally, we'll come back to look at loops.
Equality and relational operators:
a == b
a != b
a < b
a <= b
a > b
a >= b
Logical operators:
p && q
p || q
!p
if statementThe simple if statement (no else)
Syntax:
if (CONDITION)
STATEMENT
Listing 5.1: Age
The if-else statement
Syntax:
if (CONDITION)
STATEMENT1
else
STATEMENT2
(int) (Math.random() * 2) ?Page 221 shows an example of misleading indentation.
Blocking. So, if indentation does not suffice to place a sequence of statements, what does work? A block of statements.
A block is a sequence of statements enclosed in {} braces:
{
STATEMENT1;
STATEMENT2;
...
STATEMENTN;
}
Block statements { ... } can stand syntactically for a single statement. So, they can be used in place of STATEMENT, STATEMENT1, or STATEMENT2 in the if statement.
Listing 5.5 Guessing
generator.nextInt(MAX) + 1 ?Blocking styles:
Textbook style:
if (condition)
{
statement; ...
}
else
{
statement; ...
}
Alternate style:
if (condition) {
statement; ...
}
else {
statement; ...
}
But please, not like this:
if (condition) {
statement; ...
} else { // don't do this!!!
statement; ...
}
The trouble is that the else is hidden; there are parts of 3 statements on one line.
Nested if statements
Listing 5.6 MinOfThree
Study with inputs:
2 5 7
5 7 2
7 2 5
How to write [not in the book?]:
if (c1)
a1;
else
if (c2)
a2;
else
a3;
can be rewritten more simply (more clearly?) as:
if (c1)
a1;
else if (c2)
a2;
else
a3;
Comparing floating point numbers: == requires exact equality, but floating point operations are inexact, so we usually want to find whether |a − b| < tolerance.
Comparing characters: the Unicode character set determines the ordering of characters.
Comparing Strings:
==,!=, <, 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
A single listener can listen for events from more than one object. In that case, we might want to determine which object emitted the event. Use the ActionEvent method getSource, which returns a reference to the emitting object.
Two buttons with a single action listener.
Note the subpanel used for grouping; but why is the arrangement of label and subpanel vertical? Because of the size of the outer panel — as you can see by expanding it.
Check boxes are square buttons that can be independently toggled (checked or unchecked). When their state changes, they generate an ItemEvent, which may be passed to an ItemListener. The listener typically doesn't ask the event source; rather it queries the state of each of the checkboxes that it serves.
These illustrate using two checkboxes to select italic and/or bold fonts. It also shows the Font class, representing a character font. If both are checked, the font is set to Font.ITALIC + Font.BOLD, representing the combination of italic bold.
Radio buttons are used in groups, called ButtonGroups. They can be toggled on and off; but unlike checkboxes, they are mutually exclusive; i.e., within a ButtonGroup, only one JRadioButton can be on at a time. A radio button generates an ActionEvent which it is turned on, but not when it is turned off, and the listener probably wants to determine the event source.
These use three radio buttons to choose the type of quotation displayed. Note the use of a ButtonGroup and adding each radio button to the group.
Provides controlled repetition of statements (iteration). We use the word iteration to mean either (1) the process of repeating a statement; or (2) a single repetition of a statement. E.g.:
Syntax:
while (expr)
statement;
Operation: as long as expr is true, keep executing statement (which can be and usually is a block statement).
Uses a while loop and a sentinel value to indicate end of input; uses priming read before loop and reads next value just at end of loop before next iteration. Also uses an accumulator (running sum).
Uses while loop to validate input.
Infinite loops occur if the expr never becomes false. The programmer must ensure that statement makes expr eventually become false. Be prepared to interrupt; in Unix, you can use C-c (control-C) to interrupt.
Nested loops are loops within loops.
Nested loops example:
int line = 1;
while (line < 5) {
char letter = 'a';
while (letter < 'f') {
System.out.print(letter);
letter++;
}
System.out.println(); // why?
line++;
}
Outer loop reads each line of input; inner loop compares characters forwards from beginning of line and backwards from end of line to middle. Note how the values of left and right are updated. (This program is not sophisticated enough to allow for differences of spacing, capitalization, or punctuation.)
Using the break or continue statement in a while statement is possible, but should be avoided, because it makes a less clearly structured program.
Why is this wrong for the empty string?
An Iterator is an object that allows us to iterate through a sequence of data — usually a collection, such as a list — so that we process each element in turn.
Iterators must have these methods:
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 ...
}
Scanners are iterators, but they also define some more specialized methods such as hasNextDouble and nextDouble.
Uses nested loops, with a Scanner as iterator for each loop.
The class java.util.ArrayList provides dynamically resizable lists implemented as arrays; we'll explore the array data type in Chapter 8.
As with strings, we can refer to the elements by index. Unlike strings, we can insert and remove elements. An ArrayList can expand as data are added and shrink when they are deleted.
Figure 5.8 lists some of the important methods of ArrayList: constructor/0, add(object), add(index, object), clear, remove(index), get(index), set(index, object), indexOf(object), contains(object), isEmpty(), size().
The constructor is specified as ArrayList<E>() — but what is E? E is the type of objects we want the list to contain.
It seems that DrJava Interactions will let us get away with some things that the Java compiler will not:
Welcome to DrJava. ...
> import java.util.ArrayList;
> ArrayList al = new ArrayList<String>();
> ArrayList as = new ArrayList<String>();
> ArrayList ai = new ArrayList<Integer>();
> as
[]
> ai
[]
> as.add("Cats")
true
> as.add("Dogs")
true
> as
[Cats, Dogs]
> as.set(1, "Mice")
"Dogs"
> as
[Cats, Mice]
> as.add(32)
true
> as
[Cats, Mice, 32]
> ArrayList<String> as = new ArrayList<String>();
> ArrayList<Integer> ai = new ArrayList<Integer>();
> ai.add("Horse")
Static Error: No method in ArrayList<Integer> with name 'add' matches this invocation
Arguments: (String)
Candidate signatures:
boolean add(Integer)
void add(int, Integer)
> as.add(32)
Static Error: No method in ArrayList<String> with name 'add' matches this invocation
Arguments: (int)
Candidate signatures:
boolean add(String)
void add(int, String)
> ai.add(32)
true
> ai.add(96);
> ai.add(37);
> ai
[32, 96, 37]
> ai.get(0)
32
> ai.set(0, 19)
32
> ai
[19, 96, 37]
> ai.isEmpty()
false
> as.isEmpty()
true
> ai.size()
3
> as.size()
0
> ai.contains(32)
false
> ai.contains(96)
true
> ai
[19, 96, 37]
> ai.indexOf(96)
1
> ai.remove(2)
37
> ai
[19, 96]
> ai.clear
Static Error: No field in ArrayList<Integer> has name 'clear'
> ai.clear()
> ai
[]
> ArrayList ax = new ArrayList()
> ax
[]
> as.add(10)
Static Error: No method in ArrayList<String> with name 'add' matches this invocation
Arguments: (int)
Candidate signatures:
boolean add(String)
void add(int, String)
> ax.add(10)
true
> ax.add("Horse")
true
> ax
[10, Horse]
> ArrayList<Short> ay = new ArrayList()
Static Error: Bad types in assignment: from raw ArrayList to ArrayList<Short>
> ArrayList<Short> ay = new ArrayList<Short>()
> ArrayList ag = ay;
> ay.add("Metaphor")
Static Error: No method in ArrayList<Short> with name 'add' matches this invocation
Arguments: (String)
Candidate signatures:
boolean add(Short)
void add(int, Short)
> ag.add("Hypothesis")
true
> ay
[Hypothesis]
> ag
[Hypothesis]
>
Listing 5.11 Beatles: uses an ArrayList to store the names of the famous musicians. Demonstrates constructor, add, indexOf, remove, add, get, and toString.
This is our first encounter with the Java Collections Framework. In general, collections can store elements of any reference type. The default element type is Object. Using the new feature (Java 1.5) called generic types, we can specify that the element type is something more specific, e.g.,
ArrayList<Musician> band = new ArrayList<Musician>();
This has two advantages:
eliminates the need for cast when retrieving elements, e.g., (Musician) band.get(0)
—On the other hand, it can make code quite a lot more complicated sometimes!
Example: a band as an ArrayList of Musicians (i.e., containing only Musicians):
- without generic types:
// creation and storage ArrayList band = new ArrayList(); band.add(new Musician("J. S. Bach")); band.add(new String("abc")); // oops! undetected error
// retrieval Object obj = band.get(0); if (obj instanceof Musician) {
Musician mus = (Musician) obj; ... } else // report an error!!
- with generic types:
// creation and storage ArrayList<Musician> band = new ArrayList<Musician>(); band.add(new Musician("J. S. Bach")); band.add(new String("abc")); // error detected at compile time
// retrieval Musician mus = band.get(0);
Discussion of the (in)efficiency of insert and delete far from the rear of the list, and of expanding and contracting the array.
Inserting an element that increases the size of the ArrayList requires copying the existing elements into a new (internal) array that is bigger.
Efficient storage of collections of data is a big subject, and has its own course: CSCI-C243 Introduction to Data Structures. There are many more interesting structures than arrays and array lists!