Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework 4 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/29 Mon., 9/30 Problems

Similar presentations


Presentation on theme: "Homework 4 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/29 Mon., 9/30 Problems"— Presentation transcript:

1 Homework 4 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/29 Mon., 9/30 Problems http://www.cs.hmc.edu/courses/2002/fall/cs5/ http://www.cs.hmc.edu/courses/2002/fall/cs5/week_04/homework.html CS 5 website Don’t worry! email dodds@cs.hmc.edu or bthom@cs.hmc.edu and, if possible, include in that email your java files Submission problems?

2 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 Tutors available academic computing labs (Parsons) Linde Activities Center lab

3 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 Tutors available

4 Power Generator Problem:Input a base and a power, find base power IMPORTANT ! Do NOT use Math.pow( … ) Abstraction:taking a power is repeated multiplication for loop Problem 2: Power Generator

5 double base; int power; //set above values via user double result; for ( ; ; ) //iteratively calculate power { } //display result Problem 2: Power Generator CS5 Mantra: create structure first, then details

6 for loops : Potential bugs for ( int i = start ; i < stop ; ++i ) { //some lower-level vars int tmp; //do some calculation in here } 12 3 4 int someWork = … ; … //some higher-level vars int start = …, stop = … ;

7 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 Problem 1 - Sequence Generator Printing the first 9 terms of these sequences…

8 generating Random #’ s Math.random(); produces a double between 0 and 1 (excluding 1) 01 0123 produces a double between 0 and 3 (excluding 3) produces an int between 0 and 2 (inclusive) 0123 produces an int between 1 and 3 (inclusive) 0123 3*Math.random(); (int)(3*Math.random()); (int)(3*Math.random() + 1); CS5 Mantra: create structure first, then details

9 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);

10 Potential making-choices bugs not necessarily mutually exclusive mutually exclusive blocks of code 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!”); } 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!”); }

11 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

12 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

13 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 program skeleton randomly choose a winning curtain, 1-3 input user’s initial choice need to find a losing curtain that was not the user’s choice ! get input compare to “switch” determine the user’s final choice & the result get input and continue or quit

14 Problem 3 -- Monty Hall Welcome! I have an opportunity for you... randomly choose a winning curtain, 1-3 adding details

15 Problem 3 -- Monty Hall 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. input user’s initial choice need to find a losing curtain that was not the user’s choice ! adding details

16 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 get input compare to “switch” determine the user’s final choice & the result adding details

17 Problem 3 -- Monty Hall Would you like to play again? yes get input and continue or quit adding details

18 Top-down software design Curtain numbers (1,2, or 3) : car, spam1, spam2, user 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! Else –You loose! Android Monty! Variables Code Skeleton Until user quits

19 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 print the number of wins and losses remove all printing except the final result of each trial


Download ppt "Homework 4 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/29 Mon., 9/30 Problems"

Similar presentations


Ads by Google