Lecture 9, CS 302-6, September 23

  1. Review
    1. Decisions
    2. Switch statements
    3. Booleans/Boolean operators
    4. Question from last time: switch statement with default in wrong place.

                                                               i.      Java interpret its correctly, as an else (as though it is at the end of the switch)

                                                             ii.      Will not compile if two default statements given, though

  1. Boolean operator
    1. Note – exams will have the precedence table for Boolean operations, so if you don’t memorize the order, that’s ok.
    2. Examples – Character.isDigit,.isLetter,.isUpperCase, etc
    3. Simplifications

                                                               i.      !(x=y) -> x!=y

                                                             ii.      ! (x>y) -> x<=y

    1. DeMorgan’s law

                                                               i.      !(A && B)=>!A || !B

                                                             ii.      !(A || B) =>!A && !B

    1. Lazy evaluation

                                                               i.      If(quantity>0 && price/quantity)

1.      analyze quantity>0 first, if it isn’t, don’t process price/quantity

                                                             ii.      Why?  Less processing, prevent divide by zero issues.

  1. Assorted Topics:
    1. Dangling else

if(a)

   if(b)

else

                                                               i.      The else goes with the second if in the above case.  But it’s best to avoid this syntax

    1. rand.nextInt(2)

                                                               i.      Returns a random number between 0 inclusive and 2 exclusive, so either 0 or 1

    1. String comparisons

                                                               i.      Don’t do string1==string2

                                                             ii.      Instead, do string1.equals(string2)

                                                            iii.      string1.compareTo(string2)

1.      0 if equal, - if string 1 is before, + if string 1 is after alphabetically

                                                           iv.      XKCD pointers - http://xkcd.com/138/

    1. Floating point comparisons

                                                               i.      sqrt(2.0)^2!=2.0

  1. Input validation
    1. Scanner.hasNextInt,hasNextDouble

                                                               i.      Verifies that the next input is of the desired type

                                                             ii.      Can be used in conditions for input data validation

    1. Example: Calculator.java
  1. While loops
    1. Used when you want to do something multiple times
    2. Structure is similar to an if statement:

while(condition)

{

do something
}

    1. Example – AreWeThereYet.java
    2. Reminder: if statements do something once.  While statements do something until the condition is met.
    3. Off-by-one-errors

                                                               i.      Be careful with <=, < etc in conditions

    1. Be careful to avoid infinite loops
  1. HW
    1. Read and understand 4.1-4.3