1 / 19

Homework 4

Homework 4. Sun., 9/29. ( MT sections ). Due. at midnight. Mon., 9/30. ( WTh sections ). 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?.

lew
Download Presentation

Homework 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework 4 Sun., 9/29 ( MT sections ) • Due at midnight Mon., 9/30 ( WTh sections ) • 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 dodds@cs.hmc.edu or bthom@cs.hmc.edu and, if possible, include in that email your java files

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

  3. 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

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

  5. 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

  6. 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; //do some calculation in here } 3

  7. 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

  8. generating Random #’s CS5 Mantra: create structure first, then details Math.random(); produces a double between 0 and 1 (excluding 1) 0 1 3*Math.random(); produces a double between 0 and 3 (excluding 3) 0 1 2 3 (int)(3*Math.random()); 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

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

  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 program skeleton randomly choose a winning curtain, 1-3 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 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 adding details randomly choose a winning curtain, 1-3 Welcome! I have an opportunity for you...

  15. Problem 3 -- Monty Hall adding details 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 !

  16. Problem 3 -- Monty Hall adding details 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

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

  18. Android Monty! Variables 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! 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 • remove all printing except the final result of each trial print the number of wins and losses

More Related