Transcript Lec4r.ppt

Homework 4
• Due
Sun., 9/29
( MT sections )
Mon., 9/30
( WTh sections )
at midnight
• Problems
http://www.cs.hmc.edu/courses/2002/fall/cs5/week_04/homework.html
• CS 5 website
http://www.cs.hmc.edu/courses/2002/fall/cs5/
• Submission problems?
Don’t worry! email [email protected] or [email protected] and, if possible,
include in that email your java files
Tutors available
Friday Afternoons 1-4 pm
------------------------------------------Chris Hwang
this week (9/20): Parsons
Don Lee
this week (9/20): Parsons
Daniel Chan
this week (9/20): LAC Lab
Zach Andree
this week (9/20): LAC Lab
Annie Chang
this week (9/20): LAC Lab
Saturday Afternoons 1-4pm
------------------------------------------Chris Weisiger
this week (9/21): Parsons
Yu-Min Kim
this week (9/21): Parsons
Elizabeth Lee-Su this week (9/21): LAC Lab
Aaron Homer
this week (9/21): LAC Lab
Sunday Afternoons 1-4pm
------------------------------------------Gabriel Neer
this week (9/22): Parsons
Jeff Brenion
this week (9/22): LAC Lab
Rene Logan
this week (9/22): LAC Lab
academic computing
labs (Parsons)
Linde Activities Center
lab
Tutors available
Sunday evenings 7-12pm
-------------------------------------------------Jenny Xu 6-9
this week (9/22): Parsons
Eric Flynn 7-10
this week (9/22): Parsons
Melissa Federowicz 7-10
this week (9/22): LAC Lab
Max Yi 7-10
this week (9/22): LAC Lab
Yu-Min Kim 9-12
this week (9/22): Parsons
A. Klose 9-12
this week (9/22): Parsons
Matt Beaumont-Gay 9-12
this week (9/22): LAC Lab
Chris Hwang 9-12
this week (9/22): LAC Lab
Monday Evenings 7-12pm
--------------------------------------------------Adam Kangas 7-10
this week (9/23): Parsons
Ryka Neher 7-10
this week (9/23): Parsons
Max Yi 7-10
this week (9/23): LAC Lab
Annie Chang 9-12
this week (9/23): Parsons
Paul Scott 9-12
this week (9/23): Parsons
John McCollough 9-12
this week (9/23): Parsons
Alex Pipkin 9-12
this week (9/23): LAC Lab
Chris Wottawa 9-12
this week (9/23): LAC Lab
Alex Utter 9-12
this week (9/23): LAC Lab
Problem 2: Power Generator
• Power Generator
Problem: Input a base and a power, find
base
IMPORTANT! Do NOT use Math.pow(
power
… )
Abstraction: taking a power is repeated multiplication
for
loop
Problem 2: Power Generator
CS5 Mantra: create structure first, then details
double base;
int power;
//set above values via user
double result;
for (
;
;
//iteratively calculate power
{
}
//display result
)
for loops : Potential bugs
//some higher-level vars
int start = … , stop = … ;
int someWork = … ;
…
1
2
4
for ( int i = start ; i < stop ; ++i )
{ //some lower-level vars
int tmp;
3
//do some calculation in here
}
Problem 1 - Sequence Generator
Printing the first 9 terms of these sequences…
10 15 20 25 30 35
11 22 33 44 55 66
1 2 4 8 16 32
0 3 9 12 36 39
1 22 333 4444 55555 666666
generating Random #’s
CS5 Mantra: create structure first, then details
Math.random();
produces a double between 0 and 1 (excluding 1)
0
3*Math.random();
1
produces a double between 0 and 3 (excluding 3)
0
(int)(3*Math.random());
1
2
3
produces an int between 0 and 2 (inclusive)
0
1
2
3
(int)(3*Math.random() + 1); produces an int between 1 and 3 (inclusive)
0
1
2
3
potential Random # Bugs
//following 2 don’t compile
int r = (int)scale*Math.random() + offset;
int r = offset + (int)scale*Math.random();
//only returns the value offset
int r = offset + (int)Math.random()*scale;
//a good one
int r = offset + (int)(Math.random()*scale);
Potential making-choices bugs
if (choice == 1) {
H.out.println(“good 1”);
}
if (choice == 2) {
H.out.println(“good 2”);
}
if (choice == 3) {
H.out.println(“good 3”);
} else {
H.out.println(“bad!”);
}
not necessarily
mutually exclusive
if (choice == 1) {
H.out.println(“good 1”);
} else if (choice == 2) {
H.out.println(“good 2”);
} else if (choice == 3) {
H.out.println(“good 3”); {
} else {
H.out.println(“bad!”);
}
mutually exclusive blocks of code
Problem 3 -- Monty Hall
Welcome! I have an opportunity for you...
Behind two of the following (virtual) curtains are
cans of Spam, but behind the third is our grand prize!
You may choose a curtain (1, 2, or 3): 1
All right, you've chosen curtain #1.
Before showing you what you've won, however, I'm
going to show you what's behind one of the curtains
you did not choose...
Behind curtain #3 was a can of Spam!
A good thing you didn't choose that one, I guess.
Now, you may stay with whatever is behind the curtain you
initially chose, OR you may switch to the only remaining
curtain -- which would you prefer, (switch or stay): switch
You win! Behind curtain #2 was the car!
Would you like to play again? yes
Problem 3 -- Monty Hall
Welcome! I have an opportunity for you...
Behind two of the following (virtual) curtains are
cans of Spam, but behind the third is our grand prize!
You may choose a curtain (1, 2, or 3): 1
All right, you've chosen curtain #1.
Before showing you what you've won, however, I'm
going to show you what's behind one of the curtains
you did not choose...
Behind curtain #3 was a can of Spam!
A good thing you didn't choose that one, I guess.
Now, you may stay with whatever is behind the curtain you
initially chose, OR you may switch to the only remaining
curtain -- which would you prefer, (switch or stay): switch
You win! Behind curtain #2 was the car!
Would you like to play again? yes
Problem 3 -- Monty Hall
Welcome! I have an opportunity for you...
Behind two of the following (virtual) curtains are
cans of Spam, but behind the third is our grand prize!
You may choose a curtain (1, 2, or 3): 1
program skeleton
randomly choose a
winning curtain, 1-3
input user’s
initial choice
All right, you've chosen curtain #1.
Before showing you what you've won, however, I'm
going to show you what's behind one of the curtains
you did not choose...
need to find a
losing curtain
that was not the
user’s choice !
Behind curtain #3 was a can of Spam!
A good thing you didn't choose that one, I guess.
Now, you may stay with whatever is behind the curtain you
initially chose, OR you may switch to the only remaining
curtain -- which would you prefer, (switch or stay): switch
You win! Behind curtain #2 was the car!
Would you like to play again? yes
get input
compare to “switch”
determine the user’s
final choice & the result
get input and continue or quit
Problem 3 -- Monty Hall
Welcome! I have an opportunity for you...
adding details
randomly choose a
winning curtain, 1-3
Problem 3 -- Monty Hall
Behind two of the following (virtual) curtains are
cans of Spam, but behind the third is our grand prize!
adding details
input user’s
initial choice
You may choose a curtain (1, 2, or 3): 1
All right, you've chosen curtain #1.
Before showing you what you've won, however, I'm
going to show you what's behind one of the curtains
you did not choose...
Behind curtain #3 was a can of Spam!
A good thing you didn't choose that one, I guess.
need to find a
losing curtain
that was not the
user’s choice !
Problem 3 -- Monty Hall
Now, you may stay with whatever is behind the curtain you
initially chose, OR you may switch to the only remaining
curtain -- which would you prefer, (switch or stay): switch
You win! Behind curtain #2 was the car!
Would you like to play again? yes
adding details
get input
compare to “switch”
determine the user’s
final choice & the result
Problem 3 -- Monty Hall
Would you like to play again? yes
adding details
get input and continue or quit
•
•
Code Skeleton
Variables
Android Monty!
Top-down software design
Curtain numbers (1,2, or 3) : car, spam1, spam2,
When user switches curtain : change = true
•
car <--random--{1,2,3}; spam1, spam2 fully determined
•
•
•
•
query for user
Reveal spam1 or spam2 curtain (don’t reveal car!)
query for change
If
– You win!
Until user
quits
user
• Else
– You loose!
Problem 4
• Monte Carlo Monty Hall
Main idea:
run your M.H. emulator 1000 times
keep track of whether the user wins or loses
Key details: no one is willing to play 1000 times!
the program needs to play the part of the user, too
• have the user always choose curtain #1
• have the user always switch
• remove all printing except the final result of each trial
print the number of wins and losses