1 / 32

COMP 14: switch, Developing Programs

COMP 14: switch, Developing Programs. June 2, 2000 Nick Vallidis. Announcements. P3 due Tuesday, June 6. Review. How does the do loop work? How does the for loop work?. do statement ; while ( condition );. for ( initialization ; condition ; increment ) statement ;.

kasi
Télécharger la présentation

COMP 14: switch, Developing Programs

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. COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis

  2. Announcements • P3 due Tuesday, June 6

  3. Review • How does the do loop work? • How does the for loop work? do statement; while (condition); for (initialization; condition; increment) statement;

  4. Review (cont.) • What are the 4 ways to decrement a variable? • What's the difference between ++i and i++?

  5. Today • the switch statement • Steps to take in developing a program

  6. switch • The switch statement format: switch (expression) { caseexp1: statement1; break; case exp2: statement2; break; default: statement; }

  7. switch • The switch statement format: switch (expression) { caseexp1: statement1; break; case exp2: statement2; break; default: statement; } Java reserved words

  8. switch • The switch statement format: switch (expression) { caseexp1: statement1; break; case exp2: statement2; break; default: statement; } The expression is evaluated and control jumps to the case whose value matches. If there is no matching case, then control goes to default

  9. switch • The switch statement format: switch (expression) { caseexp1: statement1; break; case exp2: statement2; break; default: statement; } The statements are executed starting from the corresponding case until it gets to a break. At a break, control jumps to the end of the switch

  10. switch • The switch statement format: switch (expression) { caseexp1: statement1; case exp2: statement2; break; default: statement; } But the break statements are optional, so you could just run on to the next case if you leave it out

  11. switch example // i is a character the user typed in switch(i) { case ‘a’: case ‘A’: System.out.println("You entered 0"); case ‘Z’: System.out.println("You entered 1"); default: System.out.println("You entered " + "something other than 0 or 1"); }

  12. comments on switch • only works with integral types (any primitive type except boolean or the floating point types) • This is basically an equality test

  13. Developing programs • The book describes for basic steps • establishing the requirements • creating a design • implementing the code • testing the implementation

  14. Developing programs • For the small code fragments we've done in class, we've gone straight for the implementation • For the rest of your assignments, it will really help you to go through this process

  15. Example: assignment P2 • We'll use assignment P2

  16. Establishing requirements • Here are the requirements: • read in two names • greet the users • read in the day of the month that each of the people was born • report winner as person born earlier in month • if it's a tie, report that it's a tie • Anything else?

  17. Creating a design • We'll start by breaking it down into manageable steps: • 1) ask for players' names • 1.5) greet users • 2) ask for players' days of birth • 3) determine winner (or that it's a tie) • 4) print out result

  18. Creating a design • Then we can either break things down again if they're still big tasks, or get down to writing specifics. • This is simple enough that we'll go to specifics next.

  19. Exciting new term! • pseudocode - a mixture of programming statements and English • Example: if class is over everyone can leave else everyone stays

  20. 1) ask for names • prompt for first name • read in first name • prompt for second name • read in second name

  21. 2) ask for day of birth • ask (first name) for day of birth • read in day of birth • ask (second name) for day of birth • read in day of birth

  22. 3) determine winner if (days of birth are equal) it's a tie else { if (person 1's day < person 2's day) person 1 was born first else person 2 was born first }

  23. 4) print out result • Turns out that it's easier to combine 3&4 if (days of birth are equal) print it's a tie else { if (person 1's day < person 2's day) print person 1 was born first else print person 2 was born first }

  24. Now turn this into code • The pseudocode should be written such that turning it into code is trivial.

  25. Your turn! • Try this with the PB&J example • Let's talk about the requirements together first...

  26. PB&J requirements • what you have: • robot w/ 2 arms, jar of PB, jar of J, 2 slices of bread, 1 knife • what you have to do: • make PB&J sandwich • robot can only take simple instructions

  27. Now break down the task • Aim for a maximum of about 7 steps at any one level

  28. So now we have 1 level... • Now break down each of these tasks into their steps. • Repeat this until you get down to steps you know how to program!

  29. A well-known algorithm • Maybe you've seen this before? • Lather • Rinse • Repeat • What's wrong with this as an algorithm?

  30. Assignment P3 • Here's the order you should probably try to do things: • Get the framework going - announcing the game, asking name, etc. • make the higher-lower game work once. (don't worry about checking for valid input) • add the "do you want to play again?" loop • add checking for valid input • do the extra credit

  31. Opening existing projects • Some people have had trouble with getting their programs to run after they quit Visual J++ • 2 of the ways to do this: • (if it's your machine) go to the File menu and right above exit will be the names of recent projects • click on my computer and keep going through folders till you get to the right one and double-click on <project name>.sln

  32. Homework • read 4.1-4.2 (and 3.3, 3.5 if you missed that) • P3 is due Tuesday (start now!)

More Related