Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
CS 5 Reminders PowerPoint Presentation
Download Presentation
CS 5 Reminders

CS 5 Reminders

138 Views Download Presentation
Download Presentation

CS 5 Reminders

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CS 5 Reminders due Sunday, 9/19 at midnight M/T sections • Hw 3 - (3 problems) due Monday, 9/20 at midnight W/Th sections Reading: Week 3’s sections of the online text Hw3Pr1) The virtual songwriter Programs: Hw3Pr2) Mathematical Menu Hw3Pr3) Rock-Paper-Scissors Linde Lab • Take advantage of the tutors. Academic Computing Labs - their schedule is linked from the CS5 webpage • Friday 8:00 am recitation section: HW hints and Q&A

  2. Two roads diverged in a wood, and I--I took the one less traveled by,And that has made all the difference. Today • If Robert Frost had been a programmer... if (road < traveledBy) { take(road); that = all - the; } • Indecisive? CS 5 can help! • Java conjunctions • String theory -- it’s not just for physicists.

  3. Last time Variables as storage spaces type: String name: animal type: String name: noise String animal = “”, noise = “”; H.pl(“Enter an animal and a noise it makes:”); animal = H.nw(); // next word noise = H.nw();

  4. Last time Variables as storage spaces type: String name: animal type: String name: noise String animal = “”, noise = “”; H.pl(“Enter an animal and a noise it makes:”); animal = H.nw(); // next word noise = H.nw(); H.pl(“Old MacDonald had a farm.”); H.pl(“E I E I O!”); H.pl(“And on that farm he had a ” + animal); H.pl(“E I E I O!”); H.pl(“With a ” + noise + “ ” + noise + “ here”); H.pl(“ and a ” + noise + “ ” + noise + “ there”);

  5. This time Hw3Pr1) The virtual songwriter First, some questions: What is one thing you really hate? spam Type three plural nouns related to that thing: Hit return after each one. tin cans camping trips unwanted emails What is your favorite color? blue What is your favorite poet? Frost What is the name of a significant other? Rita

  6. Virtual Alanis I Think ------- I think tin cans are a really huge problem I think camping trips are too much on my mind I think unwanted emails are bringing the world down But what can you do? Like a blue rain, beating down on me Like a Frost line, which won't let go of my brain Like Rita’s voice, it is in my head Blame it on spam Blame it on spam Blame it on spam Either emulate this example, or adapt to suit your tastes...

  7. Hw 3 The example programs are available from the “Files for download” link from the CS 5 webpage. Hw3Pr1) The virtual songwriter

  8. Hw 3 The example programs are available from the “Files for download” link from the CS 5 webpage. Hw3Pr1) The virtual songwriter Hw3Pr2) A mathematical menu use switchto select among different cases use the Moth Menuprogram to get you started… check your numbers against the examples in the Hw !

  9. Hw 3 The example programs are available from the “Files for download” link from the CS 5 webpage. Hw3Pr1) The virtual songwriter Hw3Pr2) A mathematical menu use switchto select among different cases use the Moth Menuprogram to get you started… check your numbers against the examples in the Hw ! get L, a, and x from the user and then output the results of plugging them into this formula... ! Option #2:

  10. Straight-line code Hw3Pr2) The math menu thread of execution int points = 0, leagues = 0; H.pl(“Enter a number of leagues”); H.pl(“And I will convert it to points”); leagues = H.ni(); points = leagues * 3 * 5280 * 12 * 72 ; H.pl(“That is ” + points + “ points.”); straightforward code?

  11. The Others: Java’s built-in types int x = 5; holds from -2,147,483,648 to 2,147,483,647

  12. The Others: Java’s built-in types int x = 5; holds from -2,147,483,648 to 2,147,483,647 holds from -128 to 127 byte b = 3; holds from -32768 to 32767 short s = 4; long l = 6; holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

  13. The Others: Java’s built-in types int x = 5; holds from -2,147,483,648 to 2,147,483,647 holds from -128 to 127 byte b = 3; holds from -32768 to 32767 short s = 4; long l = 6; holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 String s = “this is a string of text”; Notice the single quotes ! used to hold single characters char c = ‘t’;

  14. The Others: Java’s built-in types int x = 5; holds from -2,147,483,648 to 2,147,483,647 holds from -128 to 127 byte b = 3; holds from -32768 to 32767 short s = 4; long l = 6; holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 String s = “this is a string of text”; Notice the single quotes ! used to hold single characters char c = ‘t’; double d = 42.0; about 8 places of precision (vs. double’s 16) float f = 42.0;

  15. The Others: Java’s built-in types int x = 5; holds from -2,147,483,648 to 2,147,483,647 holds from -128 to 127 byte b = 3; holds from -32768 to 32767 short s = 4; long l = 6; holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 String s = “this is a string of text”; Notice the single quotes ! used to hold single characters char c = ‘t’; double d = 42.0; about 8 places of precision (vs. double’s 16) float f = 42.0; holds either true or false boolean b = true;

  16. What’s different about String ? Code: String cc = “rock”; rock What it seems like: String cc

  17. What’s different about String ? Code: String cc = “rock”; rock What it seems like: String cc reference a memory location: #42 #42 ‘r’ ‘o’ ‘c’ ‘k’ What it really is: char char char char #42 #43 #44 #45 String cc mental models!

  18. Input: Know your type! H.in.nextWord(); H.in.nextLine(); return a String including whitespace H.in.nextChar(); return a char H.in.nextAnyChar(); including whitespace H.in.nextInt(); returns an int H.in.nextLong(); returns a long H.in.nextDouble(); returns a double

  19. Input: Know your type! H.in.nextWord(); H.in.nextLine(); H.nw(); H.nl(); return a String including whitespace H.in.nextChar(); H.nc(); H.nanyc(); return a char H.in.nextAnyChar(); including whitespace H.in.nextInt(); H.ni(); returns an int H.in.nextLong(); returns a long H.nlong(); H.in.nextDouble(); returns a double H.nd(); shortcuts !

  20. Variables are cheap... Hw3Pr2) The math menu double a=0, b=0, c=0; H.pl(“Enter the three coefficients of a quadratic ” + “equation and I’ll solve it…”); a = H.nd(); b = H.nd(); c = H.nd(); Feel free to create variables as needed! H.pl(“The solutions are ” + + “ and ” + );

  21. Math functions Hw3Pr2) Mathematical Menu Math.sqrt( 9 ) Math.sin( Math.PI ) Math.abs( -5 ) square root sine absolute value Math.log( Math.E ) natural log (ln) Math.exp( x ) e to the x angle conversion Math.toRadians( 360 ) Full Java library: http://java.sun.com/j2se/1.4.2/docs/api/ Math.random()

  22. Well, this is random… Hw3Pr2) Mathematical Menu • greater or equal to 0.0 Math.random() returns a double • less than 1.0 Code to store a random double from 0.0 to 2.0 : including 0.0 excluding 2.0 To store a random double from 1.0 to 2.0 : including 1.0 excluding 2.0 To store a random integer from 0 to 9 : inclusive To store a random integer from 1 to 3 : inclusive

  23. Randomness vs. Determinism Are there random numbers? Can a computer generate them? RNG A “black box” model of a random number generator. Output

  24. Randomness vs. Determinism Are there random numbers? Can a computer generate them? Yes Not without help! // initialize with current time long seed = System.currentTimeMillis(); seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); int i1 = (int)(seed >>> (48 - 26)); seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); int i2 = (int)(seed >>> (48 - 27)); double randomNumber = (((long)i1 << 27) + i2)/ (double)(1L << 53); The RNG revealed. Output What would a nondeterministic computing machine be like?

  25. True Randomness ! an enthusiastic endorser http://www.leapzine.com/hr/ three “random” lava lamp owners & mom LavaRand’s lava lamps using a chaotic physical system to seed random number generators (Patent 5,732,138: "Method for seeding a pseudo-random number generator with a cryptographic hash of a digitization of a chaotic system.") www.wired.com/wired/archive/11.08/random.html This has since been “improved”…

  26. Hw 3 The example programs are available from the “Files for download” link from the CS 5 webpage. Hw3Pr1) The virtual songwriter Hw3Pr2) A mathematical menu use switchto select among different cases use the Moth Menuprogram to get you started… check your numbers against the examples in the Hw ! Extra Credit:find the max and min of 4 different integers in as few comparisons as possible ?!

  27. The code not taken: if H.p(“What year were you born? ”); int y = H.ni();

  28. The code not taken: if H.p(“What year were you born? ”); int y = H.ni(); if ( y == 1984 ) { H.pl(“You’re a Leaper!”); }

  29. The code not taken: if H.p(“What year were you born? ”); int y = H.ni(); if ( y == 1984 ) { H.pl(“You’re a Leaper!”); } What is missing here ?!? The “test” or “comparison” The if block this code happens only if the test is true

  30. The code not taken: if H.p(“What year were you born? ”); int y = H.ni(); if ( ) { H.pl(“You’re a Leaper!”); } What should this test be ?

  31. The code not taken: if H.p(“What year were you born? ”); int y = H.ni(); if ( y%4 == 0 ) { H.pl(“You’re a Leaper!”); } is y divisible by 4 ? two uses of equals !!

  32. == vs. = == indicates a comparisonbetween two values It’s usually used with if if ( road == 10 ) = indicates an assignmentfrom right to left It’s usually a stand-alone statement. road = 10;

  33. == vs. = == indicates a comparisonbetween two values It’s usually used with if if ( road == 10 ) = indicates an assignmentfrom right to left It’s usually a stand-alone statement. road = 10; == “are they equal ?” a question, a test a command, an assignment = “set equal !”

  34. The code not taken: if H.p(“What year were you born? ”); int y = H.ni(); if ( y%4 == 0 ) { H.pl(“You’re a Leaper!”); } What about the rest of us?

  35. The code not taken: if H.p(“What year were you born? ”); int y = H.ni(); if ( y%4 == 0 ) { H.pl(“You’re a Leaper!”); } else { H.pl(“You’re Leapfree.”); } The if block Code run only when the previous if is false No test needed The else block

  36. A warning ! Inspired by Robert Frost and Route 94: String message; int road = H.ni(); if ( road == 94 ) { message = “The road less traveled”; } H.pl( message ); What if we want to go to Las Vegas?

  37. Initialize variables! Be sure to give your variables initial values: String message = “The road more traveled”; int road = H.ni(); if ( road == 94 ) { message = “The road less traveled”; } H.pl( message ); Now there is a message to print, regardless of the road chosen.

  38. Brace yourself… H.p(“What year were you born? ”); int y = H.ni(); if ( y%4 == 0 ) H.pl(“You’re a Leaper!”); else H.pl(“You’re Leapfree.”); H.pl(“My condolences…”); Why is this program too apologetic?

  39. Brace yourself… H.p(“What year were you born? ”); int y = H.ni(); if ( y%4 == 0 ) { H.pl(“You’re a Leaper!”); } else { H.pl(“You’re Leapfree.”); H.pl(“My condolences…”); } Watch out for your curly braces !

  40. Or And Not What years match these conditions? if ( year%4 == 0 && year > 1776 ) “and” if ( year%4 == 0 || year%4 == 2 ) “or” if ( year%20 != 0 ) “not” “not equal” if ( !(year%20 == 0) )

  41. Presidential Problems William H. Harrison -- He died in office on April 4, 1841. Abraham Lincoln -- He was shot and died April 15, 1865. James A. Garfield -- He was shot and died September 19, 1881. William McKinley -- He was shot and died September 14, 1901. Warren G. Harding -- He died in office August 2, 1923. Franklin D. Roosevelt -- He died in office April 12, 1945. John F. Kennedy -- He was shot and died November 22, 1963. Ronald Reagan -- He was shot, but survived...

  42. Presidential Problems William H. Harrison -- He died in office on April 4, 1841. Abraham Lincoln -- He was shot and died April 15, 1865. James A. Garfield -- He was shot and died September 19, 1881. William McKinley -- He was shot and died September 14, 1901. Warren G. Harding -- He died in office August 2, 1923. Franklin D. Roosevelt -- He died in office April 12, 1945. John F. Kennedy -- He was shot and died November 22, 1963. Ronald Reagan -- He was shot, but survived... if ( yr%400 == 0 ) H.pl(“It is a leap year.”); else if ( yr%4 == 0 && yr%100 != 0 ) H.pl(“It is a leap year.”); else H.pl(“It’s NOT a leap year.”); Determining leap years

  43. Time for a switch int month = H.ni(); int numDays = 0; switch ( month ) { case 2: { numDays = 28; break; } case 4: case 6: case 9: case 11: { numDays = 30; break; } default: { numDays = 31; } }

  44. Time for a switch int month = H.ni(); int numDays = 0; switch ( month ) { case 2: { numDays = 28; break; } case 4: case 6: case 9: case 11: { numDays = 30; break; } default: { numDays = 31; } } Supposemonth == 9 . It jumps to the appropriatecase

  45. Time for a switch int month = H.ni(); int numDays = 0; switch ( month ) { case 2: { numDays = 28; break; } case 4: case 6: case 9: case 11: { numDays = 30; break; } default: { numDays = 31; } } Supposemonth == 9 . It jumps to the appropriatecase It does everything up to thebreak

  46. Time for a switch int month = H.ni(); int numDays = 0; switch ( month ) { case 2: { numDays = 28; break; } case 4: case 6: case 9: case 11: { numDays = 30; break; } default: { numDays = 31; } } Supposemonth == 9 . It jumps to the appropriatecase It does everything up to thebreak It then jumps to the end of the whole switchblock.

  47. 1 Jan 31 2 Feb 28 3 Mar 31 4 Apr 30 5 May 31 6 Jun 30 7 Jul 31 8 Aug 31 9 Sep 30 10 Oct 31 11 Nov 30 12 Dec 31 “Quiz” Names: (1) Assume it is NOT a leap year. This code still assigns the wrong number of days to February. WHY? Can you fix it? int month = H.ni(); int numDays = 0; if ( month == 2 ) { numDays = 28; } if ( month == 4 || month == 6 || month == 9 || month == 11 ) { numDays = 30; } else { numDays = 31; } (2) What could you change to make this code shorter?

  48. Write a switch statement that has the same behavior as the if / else if / else statements to the left: if (x == 0) { H.pl(0); } if (x == 1) { H.pl(1); } else if (x == 2) { H.pl(2); } else { H.pl(42); } switch (x) { case : { } 3 cases and default. }

  49. 1 Jan 31 2 Feb 28 3 Mar 31 4 Apr 30 5 May 31 6 Jun 30 7 Jul 31 8 Aug 31 9 Sep 30 10 Oct 31 11 Nov 30 12 Dec 31 “Quiz” Names: (1) Assume it is NOT a leap year. This code still assigns the wrong number of days to February. WHY? Can you fix it? int month = H.ni(); int numDays = 0; if ( month == 2 ) { numDays = 28; } if ( month == 4 || month == 6 || month == 9 || month == 11 ) { numDays = 30; } else { numDays = 31; } numDays is set to 28 right here… (2) What could you change to make this code shorter? but numDays is incorrectly reset to 31 right here! initial values!