Lecture 7, CS 302-7 and 8, February 6

 

  1. Reminders
    1. Exam conflicts – online form by Friday
    2. Program 1
  2. Review
    1. Arithmetic
    2. Random numbers

                                                               i.      Shortcut for importing everything from java.util (Random and Scanner):

1.      import java.util.* ;

    1. Strings
  1. More details about Strings
    1. Reading String input

                                                               i.      scanner.next() gets one word

                                                             ii.      scanner.nextLine() – whole line (multiple words)

    1. Parsing numbers from strings

                                                               i.      int count=Integer.parseInt(string)

                                                             ii.      double price=Double.parseDouble(string)

                                                            iii.      If not valid translation, run-time error

1.      Integer.parseInt(“Hello World!”);

    1. Characters

                                                               i.      theString.charAt()

                                                             ii.      Counts begin at zero

    1. Substrings

                                                               i.      theString=”STRING”

                                                             ii.      theString.substring(1,4)=”TRI”

    1. Example-EasyCipher.java
  1. Chapter 3 – Decisions
    1. In other words, logic.  We want to be able to do different things depending on different inputs, etc
    2. Conditional behavior
    3. Sequenced flow vs controlled flow
  2. Flow charts
    1. Diamond for condition, rectangle for statements
    2. Branching behavior
    3. http://xkcd.com/210/
  3. If statements
    1. If, else
    2. Syntax:

if (condition) {

//do this;

}

else

{

//do that;

}

    1. Note:

                                                               i.       {}

1.      Not necessary if we only have one statement after the if or else

2.      However, it is good form to always use them

                                                             ii.      There is no ; after the if/else

    1. ‘nested statements’           

                                                               i.      Once again, we have block-structured code

                                                             ii.      Indentation is not an accident – helps us keep track

    1. Avoid redundant code
    2. In our conditions, we need to make some sort of comparison

                                                               i.      == tests for equality between two values

    1. Example – NumberGame.java
  1. Comparisons
    1. Relational operators

                                                               i.      >, >=, < , <=, ==,!=

                                                             ii.      Note difference between = and ==

1.      = assigns, == tests equality

  1. HW
    1. Read ch 3.3-3.4