Lecture 12, CS 302-7 and 8, February 17
- Reminders
- Program
1
i.
Due February 24
ii.
Partner registration Tonight
iii.
Documentation make sure to read standards on website
- OO
for Monday
i.
11-12 instead of 4-5
- Lab
scores on Learn@UW
- Codelab
ex. 2 assigned
- Review
- Loops:
For, while, do
- Input
validation
- Input
Validation
- Example
Menu.java
- Some
things you can do with loops:
- Sums,
averages
- Counting
matches
- Finding
first match
- Max
and min
- Comparing
adjacent values
- Example
Factors.java
- Nested
Loops
- Loop inside a loop
- Example
with classroom how to iterate students
i.
By row, then column
- Printing
a table of values with .print and .println
- Example
MultTable.java
- Can
also use nested loops to run multiple things through a loop
- Example Primes.java
- Simulations
- Bit
of a change of topic another sort of example
- We
can perform the same random task many times to approximate solutions.
- Simulating
coin flips, dice rolls, etc
- For
instance, estimating pi (you dont 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 xs and ys 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
- Example MonteCarlo.java
- HW
- Next
week arrays (and methods)
- Read
6.1-6.2 (NOT 5)