1 / 17

Review for Midterm 2

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.

zohar
Download Presentation

Review for Midterm 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review for Midterm 2 Aaron Bloomfield CS 101-E

  2. 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

  3. Chapter 5

  4. 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

  5. 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

  6. 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”

  7. 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

  8. 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

  9. 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

  10. 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!

  11. Chapter 6

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

More Related