pptx - iLearn

Document technical information

Format pptx
Size 937.7 kB
First found May 22, 2018

Document content analysis

Category Also themed
Language
English
Type
not defined
Concepts
no text concepts found

Persons

Organizations

Places

Transcript

Five-Minute Review
1. What are classes and objects?
What is a class hierarchy?
2. What is an expression? A term?
3. What is a variable declaration?
4. What is an assignment?
What is precedence?
5. What do you know about Booleans?
1
Programming – Lecture 4
Statement Forms (Chapter 4)
• Repeat N-Times Pattern
• Repeat-Until-Sentinel Pattern
• if/else
•? :
• switch
• while, do while
• Loop-and-a-Half Pattern
• for
• Animation
Statement Types
Recall: Expressions, have type, are
interested in value
Recall: Classes, Methods, Statements
Statements: units of execution,
are interested in side effect
Distinguish
• Expression statements
• Declaration statements
• Control flow statements
• Compound statements (blocks)
4
Expression Statements
• Assignment expressions
aValue = 8933.234;
• Any use of ++ or -aValue++;
• Method invocations
System.out.println("Hello!");
• Object creation expressions
new Bicycle();
Note: the book defines simple statements as “expression ;”
instead of expression/declaration statements.
However, declarations are not expressions, and not all
5
expressions (such as “17”) lead to valid statements.
if Statement
if (condition) {
statements to be executed if the condition is true
}
if (condition) {
statements to be executed if the condition is true
} else {
statements to be executed if the condition is false
}
Cascading if
8
Dangling Else
if (x > 0)
if (doprint)
println(“x positive”);
else
if (doprint)
println(“x not positive”);
If an if has an else clause,
always use braces in if clause!
9
?: Operator
condition ? expression1 : expression2
12
switch Statement
switch ( expression ) {
case v1:
statements to be executed if expression = v1
break;
case v2:
statements to be executed if expression = v2
break;
. . . more case clauses if needed . . .
default:
statements to be executed if no values match
break;
}
13
public void run() {
println("This program shows the number of days in a
month.");
int month = readInt("Enter numeric month (Jan=1): ");
switch (month) {
case 2:
println("28 days (29 in leap years)");
break;
case 4: case 6: case 9: case 11:
println("30 days");
break;
case 1: case 3: case 5: case 7: case 8: case 10: cas
12:
println("31 days");
break;
default:
println("Illegal month number");
break;
}
14
}
Add4Integers
public class Add4Integers extends ConsoleProgram
{
public void run() {
println("This program adds four numbers.");
int n1 = readInt("Enter n1: ");
int n2 = readInt("Enter n2: ");
int n3 = readInt("Enter n3: ");
int n4 = readInt("Enter n4: ");
int total = n1 + n2 + n3 + n4;
println("The total is " + total + ".");
}
}
15
Repeat-N-Times Idiom
for (int i = 0; i < repetitions; i++) {
statements to be repeated
}
16
AddNIntegers
public class AddNIntegers extends ConsoleProgram
{
public void run() {
println("This program adds " + N +
" numbers.");
int total = 0;
for (int i = 0; i < N; i++) {
int value = readInt(" ? ");
total += value;
}
println("The total is " + total + ".");
}
private static final int N = 100;
}
17
Repeat-Until-Sentinel Idiom
while (true) {
prompt user and read in a value
if (value == sentinel) break;
rest of loop body
}
18
AddIntegerList
public class AddIntegerList extends ConsoleProgram {
public void run() {
println("This program adds a list of integers.");
println("Enter values, one per line, using " +
SENTINEL);
println("to signal the end of the list.");
int total = 0;
while (true) {
int value = readInt(" ? ");
if (value == SENTINEL) break;
total += value;
}
println("The total is " + total + ".");
}
private static final int SENTINEL = 0;
}
19
AddIntegerList
public void run() {
println("This program adds a list of integers.");
println("Enter values, one per line, using " + SENTINEL);
println("to signal the end of the list.");
int total = 0;
while (true) {
int value = readInt(" ? ");
if (value == SENTINEL) break;
total += value;
}
value
total
println("The total is " + total + ".");
1
2
3
0
0
0
1
3
6
6
}
AddIntegerList
This program adds a list of integers.
Enter values, one per line, using 0
to signal the end of the list.
? 1
? 2
? 3
? 0
The total is 6.
20
skip simulation
while, do while Statements
while ( condition ) {
statements to be repeated
}
do {
statements to be repeated
} while ( condition )
23
DigitSum
public void run() {
println("This program sums the digits in an integer.");
int n = readInt("Enter a positive integer: ");
int dsum = 0;
while ( n > 0 ) {
dsum += n % 10;
n /= 10;
}
println("The sum of the digits is " + dsum);
}
n
dsum
117
0
0
1729
172
18
19
19
0
9
11
This program sums the digits in an integer.
Enter a positive integer: 1729
The sum of the digits is 19.
skip simulation
Loop-and-a-Half Pattern
while (true) {
computation necessary to make the test
if (test for completion) break;
computation for the rest of the loop cycle
}
25
Repeat-Until-Sentinel Revisited
while (true) {
computation
prompt
user and
necessary
read intoa make
valuethe test
if (value
(test for completion)
== sentinel)break;
break;
computation
rest
of loop body
for the rest of the loop cycle
}
26
for
for ( init ; test ; step ) {
statements to be repeated
}
init;
while ( test ) {
statements to be repeated
step;
}
28
Exercise
for (int i = 1; i <= 10; i++)
for (int i = 0; i < N; i++)
for (int n = 99; n >= 1; n -= 2)
for (int x = 1; x <= 1024; x *= 2)
29
Countdown
public void run() {
for ( int t = 10 ; t >= 0 ; t-- ) {
println(t);
}
println("Liftoff!");
}
t
–1
10
9876543210–1
10
9
8
7
6
5
4
3
2
1
0
Liftoff!
30
skip simulation
Nested for
for (int i = 0; i < N_ROWS; i++) {
for (int j = 0; j < N_COLUMNS; j++) {
Display the square at row i and column j.
}
}
32
Checkerboard
public void run() {
double sqSize = (double) getHeight() / N_ROWS;
for (int i = 0; i < N_ROWS; i++) {
for (int j = 0; j < N_COLUMNS; j++) {
double x = j * sqSize;
double y = i * sqSize;
GRect sq = new GRect(x, y, sqSize, sqSize);
sq.setFilled((i + j) % 2 != 0);
add(sq);
}
}
sqSize i
j
x
y
}
30.0
0
1
0
1
2
8
0.0
30.0
60.0
210.0
0.0
210.0
8
the
Execute these
inner loop six
statements
seven
timesto
to
more times
complete the
checkerboard.
row.
sq
33
skip simulation
Exercise: Triangle Number Table
1
1
1
1
1
1
1
1
1
1
=
+
+
+
+
+
+
+
+
+
1
2
2
2
2
2
2
2
2
2
=
+
+
+
+
+
+
+
+
3
3
3
3
3
3
3
3
3
=
+
+
+
+
+
+
+
6
4
4
4
4
4
4
4
=
+
+
+
+
+
+
10
5=
5+
5+
5+
5+
5+
15
6=
6+
6+
6+
6+
21
7=
7+
7+
7+
28
8 = 36
8 + 9 = 45
8 + 9 + 10 = 55
34
TriangleTable
public class TriangleTable extends ConsoleProgram {
public void run() {
for (int n = 1; n <= MAX_VALUE; n++) {
int total = 0;
for (int i = 1; i <= n; i++) {
if (i > 1) print(" + ");
print(i);
total += i;
}
println(" = " + total);
}
}
/* Private constants */
private static final int MAX_VALUE = 10;
}
36
Simple Graphical Animation
for (int i = 0; i < N_STEPS; i++) {
update the graphical objects by a small amount
pause(PAUSE_TIME);
}
37
AnimatedSquare
public void run() {
GRect square = new GRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
square.setFilled(true);
square.setFillColor(Color.RED);
add(square);
double dx = (getWidth() - SQUARE_SIZE) / N_STEPS;
double dy = (getHeight() - SQUARE_SIZE) / N_STEPS;
for (int i = 0; i < N_STEPS; i++) {
square.move(dx, dy);
pause(PAUSE_TIME);
i
dx
dy
square
}
101
3.0
1.7
}
AnimatedSquare
38
skip simulation
Summary
• Compound statements (blocks) provide
structure and scoping
• Java control statements include if/else,
switch, while, for
• Common Idioms are Repeat N-Times,
Repeat-Until-Sentinel, and Loop-and-a-Half
39
×

Report this document