Lecture 11, CS 302-7 and 8, February 15

 

  1. Reminders
    1. Program 1

                                                               i.      Due Oct 24

1.      Partner registration – Feb 17

                                                             ii.      Piazza

                                                            iii.      Backups – by submitting

  1. Review
    1. Loops

                                                               i.      While

                                                             ii.      For

  1. For loop
    1. Palindrome.java
  2. Do Loop
    1. Almost identical to a While loop.

                                                               i.      Doesn’t check condition before first iteration

                                                             ii.      Post-test instead of pre-test

do

{

}

while(<condition>);

    1. Why use do loops?

                                                               i.      When we know that we have to do something at least once

                                                             ii.      You then want to keep going while you haven’t met some condition

                                                            iii.      Balance.java

  1. Comparison of different types of loops
    1. While – indefinite, don’t need to do it
    2. Do - indefinite, need to do it at least once
    3. For – definite, don’t need to do it
  2. Input validation revisited
    1. What steps do we have to do to get input?

                                                               i.      Import scanner

                                                             ii.      Declare/initialize scanner object

                                                            iii.      Prompt user for input

                                                           iv.      Store input

    1. Let’s focus on the storing input part

                                                               i.      Before we store, we have to validate that input is ‘good’.  What could go wrong?

1.      Type

a.       Eg If we want an int, but they put in a string

2.      Range

a.       Eg if we want 1-4, but they put in 5

    1. What do we do if input wasn’t good?  We have to prompt user to enter it again

                                                               i.      We can use loops for this behavior – stick our input code in a loop

  1. Sentinel values
    1. What is a sentinel value?  A value for input that indicates that we are done accepting values.  The border between our data set and everything else.

                                                               i.      Real life example – period at the end of a sentence

                                                             ii.      Computer example – in a menu, quit

    1. Different ways to do this:

                                                               i.      while(value!=some number)

                                                             ii.      while(!done)

                                                            iii.      while(in.hasNextDouble()/in.hasNextInt())

    1. Example – Menu.java
  1. HW
    1. Finish reading ch. 4