170 likes | 313 Views
Review for Midterm 2. Aaron Bloomfield CS 101-E. 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. Chapter 5. Logical expressions.
E N D
Review for Midterm 2 Aaron Bloomfield CS 101-E
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
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
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
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”
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
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
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
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!
Chapter 6: Iterationwhile 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
Chapter 6: Iterationdo-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
Chapter 6: Iterationfor 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
Chapter 6: IterationCommon 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
Chapter 6: IterationFile 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
Chapter 6: IterationScanner 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