Lecture 12, CS 302-7 and 8, February 17

 

  1. Reminders
    1. Program 1

                                                               i.      Due February 24

                                                             ii.      Partner registration – Tonight

                                                            iii.      Documentation – make sure to read standards on website

    1. OO for Monday

                                                               i.      11-12 instead of 4-5

    1. Lab scores on Learn@UW
    2. Codelab ex. 2 assigned
  1. Review
    1. Loops: For, while, do
    2. Input validation
  2. Input Validation
    1. Example – Menu.java
  3. Some things you can do with loops:
    1. Sums, averages
    2. Counting matches
    3. Finding first match
    4. Max and min
    5. Comparing adjacent values
    6. Example – Factors.java
  4. Nested Loops
    1. Loop inside a loop
    2. Example with classroom – how to iterate students

                                                               i.      By row, then column

    1. Printing a table of values with .print and .println
    2. Example – MultTable.java
    3. Can also use nested loops to run multiple things through a loop
    4. Example – Primes.java
  1. Simulations
    1. Bit of a change of topic – another sort of example
    2. We can perform the same random task many times to approximate solutions.
    3. Simulating coin flips, dice rolls, etc
    4. For instance, estimating pi (you don’t need to know the specifics)

                                                               i.      A circle is the set of all points such that (x-a)^x+(y-b)^2=r^2

                                                             ii.      So, for circle with r 1 centered at origin, all the points such that x^2+y^2<=1 are in the circle

                                                            iii.      Only consider upper-right quadrant

                                                           iv.      Generate lots of x’s and y’s between 0 and 1: if x^2+y^2 <=1, it is in the circle, else outside

                                                             v.      Area of square is 1*1=1, so area of inside is proportion of that which falls inside circle

                                                           vi.      Then, just multiply by four to account for the entire area (four quadrants)

                                                          vii.      Compare to -

1.      Area – pi*r^2

                                                        viii.      X, y random between 0 and 1

    1. Example – MonteCarlo.java
  1. HW
    1. Next week – arrays (and methods)
    2. Read 6.1-6.2 (NOT 5)