Review for exam 2

Download Report

Transcript Review for exam 2

Review for Midterm 2
Aaron Bloomfield
CS 101-E
1
Test focus
Test will focus on the material covered
since the last midterm

Chapters 5 and 6
The test is cumulative, though, and can
include any material covered so far
2
Chapter 5
3
Logical expressions
Logical expression has values either true
or false
Java has the boolean type with values true
or false
Truth table: method to dissect a logical
expression
4
Logical operators
Three primary logical operators: and, or, not
An and operation is only true when both parts
are true
An or operation is true when either (or both)
parts are true
A not operation negates (switches) the value of
the expression
Logical operators: and is &&, or is ||, not is !
Not operator is unary
5
Equality
Two equality operators: == and !=
When comparing objects, == compares
the references, not the objects themselves
Use the .equals() method, when available,
to test for object equality
Don't test floating point values for equality!
Instead, test for “closeness”
6
Ordering
Ordering operators: <, >, <=, and >=.
These only work on primitive types!
Relational operators are the equality
operators and the ordering operators
For booleans, false is less than true
For characters, ordering is based on the
Unicode numbers of the characters
7
If statements
An if statement has the form: if
(expression) action
An if-else statement has the form: if
(expression) action1 else action2
An if-else-if statement is used when there
are many tasks to do, depending on the
logical expressions
8
Switches
A switch statement is useful instead of a
long-winded if-else-if block
Must always put either break at the end of
a switch statement block, or a comment
such as '// FALL THRU'
The default case means any case not
matched by any of the other cases
9
Misc
Operator precedence: (p 187)
Short-circuit evaluation: left side is
evaluated first. If the result can be
determined at that point, right side is not
evaluated
System.exit() will terminate the program
immediately
Use consistent indentation!
10
Chapter 6
11
Chapter 6: Iteration
while loop syntax
While statements:





while ( expression ) action
Action
is
executed
repeatedly
while
expression is true
Once expression is false, program execution
moves on to next statement
Action can be a single statement or a block
If expression is initially false, action is never
executed
12
Chapter 6: Iteration
do-while loop syntax
Do-while statements:


do action while ( expression )
Action is executed one ALWAYS
Then expression is tested after each loop



Once expression is false, program execution
moves on to next statement
Action can be a single statement or a block
If expression is initially false, action is never
executed
13
Chapter 6: Iteration
for loop syntax
For statements:







for ( forinit; forexpression; forupdate ) action
forinit is executed once only (before the loop starts
the first time)
Action is executed repeatedly while forexpression is
true
After action is executed at the end of each loop,
forupdate is executed
Once forexpression is false, program execution
moves on to next statement
Action can be a single statement or a block
If expression is initially false, action is never executed
14
Chapter 6: Iteration
Common pitfalls
Infinite loop: a loop whose text expression
never evaluates to false
Be sure that your for loop starts and ends
where you want it to


For example, in an array of size n, it needs to
start at 0 and end at n-1
Otherwise, it’s called an “off-by-one” error
Be sure your loop variable initialization is
correct
15
Chapter 6: Iteration
File access
Java provides the File class for file I/O

Constructor takes in the file name as a String
A stream is a name for a input or output
method




System.out: output stream
System.err: error output stream
System.in: input stream
File: file input or output stream
16
Chapter 6: Iteration
Scanner methods
The Scanner class can be initialized with
an File object

Scanner filein = new Scanner (new File
(filename));
The Scanner class has a bunch of
methods useful in loops:


hasNextInt(): tells whether there is a next int
hasNextDouble(): same idea, but with doubles
17