Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis.

Similar presentations


Presentation on theme: "COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis."— Presentation transcript:

1 COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis

2 Announcements zP3 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.) zWhat are the 4 ways to decrement a variable?  What's the difference between ++i and i++ ?

5 Today  the switch statement zSteps to take in developing a program

6 switch zThe switch statement format: switch (expression) { case exp1: statement1; break; case exp2: statement2; break; default: statement; }

7 switch zThe switch statement format: switch (expression) { case exp1: statement1; break; case exp2: statement2; break; default: statement; } Java reserved words

8 switch zThe switch statement format: switch (expression) { case exp1: 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 zThe switch statement format: switch (expression) { case exp1: 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 zThe switch statement format: switch (expression) { case exp1: 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) zThis is basically an equality test

13 Developing programs zThe book describes for basic steps yestablishing the requirements ycreating a design yimplementing the code ytesting the implementation

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

15 Example: assignment P2 zWe'll use assignment P2

16 Establishing requirements zHere are the requirements: yread in two names ygreet the users yread in the day of the month that each of the people was born yreport winner as person born earlier in month yif it's a tie, report that it's a tie zAnything else?

17 Creating a design zWe'll start by breaking it down into manageable steps: y1) ask for players' names y1.5) greet users y2) ask for players' days of birth y3) determine winner (or that it's a tie) y4) print out result

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

19 Exciting new term! zpseudocode - a mixture of programming statements and English zExample: if class is over everyone can leave else everyone stays

20 1) ask for names zprompt for first name zread in first name zprompt for second name zread in second name

21 2) ask for day of birth zask (first name) for day of birth zread in day of birth zask (second name) for day of birth zread 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 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 } 4) print out result zTurns out that it's easier to combine 3&4

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

25 Your turn! zTry this with the PB&J example zLet's talk about the requirements together first...

26 PB&J requirements zwhat you have: yrobot w/ 2 arms, jar of PB, jar of J, 2 slices of bread, 1 knife zwhat you have to do: ymake PB&J sandwich zrobot can only take simple instructions

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

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

29 A well-known algorithm zMaybe you've seen this before? yLather yRinse yRepeat zWhat's wrong with this as an algorithm?

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

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

32 Homework zread 4.1-4.2 (and 3.3, 3.5 if you missed that) zP3 is due Tuesday (start now!)


Download ppt "COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis."

Similar presentations


Ads by Google