the keyword list thus far
play

The keyword list thus far: Creating Correct Programs Creating - PowerPoint PPT Presentation

Topic 16 The keyword list thus far: Creating Correct Programs Creating Correct Programs Complete list of Java keywords: Complete list of Java keywords: abstract default if private this boolean do implements protected


  1. Topic 16 The keyword list thus far: Creating Correct Programs Creating Correct Programs � Complete list of Java keywords: Complete list of Java keywords: abstract default if private this boolean do implements protected throw "It is a profoundly erroneous truism, repeated by all the break double throws import public byte else byte else instanceof instanceof return return transient transient copybooks, and by eminent people when they are making case extends int short try speeches, that we should cultivate the habit of thinking about catch final interface static void char finally long strictfp volatile what we are doing. The precise opposite is the case. g p pp class class float float native super while native super while Civilization advances by extending the number of const for switch new operations which we can perform without thinking about continue goto package synchronized assert enum them. Operations of thought are like cavalry charges in a them. Operations of thought are like cavalry charges in a battle -they are strictly limited in number, they require fresh horses, and must only be made at decisive moments." - Alfred North Whitehead Alfred North Whitehead Based on slides for Building Java Programs by Reges/Stepp, found at http://faculty.washington.edu/stepp/book/ p y g pp CS305j Introduction to Odds and Ends CS305j Introduction to Odds and Ends 1 2 Computing Computing The Random class Generating "Random" g � Java has a class named Random whose objects generate � Java has a class named Random whose objects generate Numbers pseudo-random numbers. Method name M th d D Description i ti nextInt() returns a random integer nextInt( max ) returns a random integer in the range [0, max) in other words, from 0 up through one less than the max nextDouble() returns a random real number in the range [0.0, 1.0) – Example: Random rand = new Random(); int randomNumber = rand.nextInt(10); // randomNumber has a random value between 0 and 9 – Class Random is found in the java.util package. import java.util.*; p j ; CS305j Introduction to Odds and Ends 3 CS305j Introduction to Odds and Ends 4 Computing Computing

  2. Pseudo Random Numbers Pseudo Random Numbers � What does "pseudo random" numbers mean? � What does "pseudo random" numbers mean? � T � Try this: thi � Computers don't do things ambiguously for(int i = 0; i < 10; i++){ – despite what people think desp te at peop e t Random r = new Random(1127); ( ); � They have a limited number of commands for(int j = 0; j < 20; j++){ System.out.print( r.nextInt() ); – "Pick a random number" isn't one of them } � pseudo random numbers are generated � d d b t d System.out.println(); algorithmically via mathematical operations } � They start with an initial number of see number � They start with an initial number of see number � Try with out the initial seed. � If you know the seed and the algorithm you can � A whole area of computer science devoted completely predict the sequence of numbers p y p q to trying to generate seemingly random numbers CS305j Introduction to Odds and Ends CS305j Introduction to Odds and Ends 5 6 Computing Computing Random examples Random examples (answers) Random rand = new Random(); Random rand new Random(); Random rand new Random(); Random rand = new Random(); � A random number between 0 and 19 inclusive: � A random number between 0 and 19 inclusive: int random1 = rand.nextInt(20); � A random number between 1 and 10 inclusive: � A random number between 1 and 10 inclusive: int random1 = rand.nextInt(10) + 1; � A random number between 4 and 17 inclusive: � A random number between 4 and 17 inclusive: int random1 = rand.nextInt(14) + 4; � A random even number between 0 and 10 inclusive: � A random even number between 0 and 10 inclusive: � A random even number between 0 and 10 inclusive: � A random even number between 0 and 10 inclusive: int random1 = rand.nextInt(6) * 2; � A random multiple of 10 between 100 and 200 inclusive: � A random multiple of 10 between 100 and 200 inclusive: int random1 = rand.nextInt(11) * 10 + 100; CS305j Introduction to Odds and Ends 7 CS305j Introduction to Odds and Ends 8 Computing Computing

  3. Random practice problem � Write a multiplication tutor program. Example dialogue: � Write a multiplication tutor program Example dialogue: This program helps you learn multiplication by Asking you 5 random multiplication questions and counts how many you get right. #1 of 5: 10 * 25 = 250 Correct! The do/while Loop The do/while Loop #2 of 5: 72 * 12 = 864 #2 of 5: 72 12 864 Correct! #3 of 5: 87 * 21 = 1741 Incorrect. The correct answer was 1827 #4 of 5: 8 * 84 = 692 Incorrect. The correct answer was 672 #5 of 5: 25 * 36 = 900 Correct! You got 3 out of 5 CS305j Introduction to Odds and Ends CS305j Introduction to Odds and Ends 9 10 Computing Computing The do/while loop � Java has another kind of loop named the do/while loop . � Java has another kind of loop named the do/while loop – It is almost identical to the while loop , except that its body statement(s) will always execute the first time, regardless of whether the condition is true. � The do/while loop, general syntax: Creating Correct Programs do { <statement(s)> ; <statement(s)> ; and Reasoning About Programs } while ( <condition> ); – Example: p // roll until we get a number other than 3 Random rand = new Random(); int dice; do { dice = rand.nextInt(); } while (dice == 3); CS305j Introduction to Odds and Ends 11 CS305j Introduction to Odds and Ends 12 Computing Computing

  4. Assertions Assertions � Assertion : A declarative sentence that is � Assertion : A declarative sentence that is � Bonus quote: � B t either true or false – "As soon as we started programming, we found � Examples: Examples: out to our surprise that it wasn't as easy to get out to our surprise that it wasn t as easy to get 2 + 2 equals 4 programs right as we had thought. Debugging The Yankees did not play in the world series in 2006. had to be discovered. I can remember the exact x > 45 > 45 instant when I realized that a large part of my life It is raining. from then on was going to be spent in finding UT beat OU last year in football. y mistakes in my own programs." UT volleyball will make the NCAA tourney – Maurice V Wilkes this year. � Not assertions � Not assertions How old are you? Take me to H.E.B. Take me to H.E.B. CS305j Introduction to Odds and Ends CS305j Introduction to Odds and Ends 13 14 Computing Computing Assertions Assertions � Assertions that depend on context can be � Assertions that depend on context can be � S � Some assertions are true or false depending ti t f l d di evaluated if the context is provided. on context. Which of these depend on the when x is 13, x > 45 when x is 13, x > 45 context? t t? It was raining in Round Rock, at 8 am on, 2 + 2 equals 4 October 10, 2006. The Yankees did not play in the world series in 2006. The Yankees did not play in the world series in 2006 � M � Many skills required to be a programmer or kill i d t b x > 45 computer scientists It is raining. � Just a few we have seen so far � Just a few we have seen so far UT will beat OU next year. – ability to generalize UT volleyball will make the NCAA tourney – create structured solutions this year. thi – trace code – manage lots of details CS305j Introduction to Odds and Ends 15 CS305j Introduction to Odds and Ends 16 Computing Computing

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend