COMP 14: switch, Developing ProgramsAnnouncementsReviewReview (cont.)TodayswitchSlide 7Slide 8Slide 9Slide 10switch examplecomments on switchDeveloping programsSlide 14Example: assignment P2Establishing requirementsCreating a designSlide 18Exciting new term!1) ask for names2) ask for day of birth3) determine winner4) print out resultNow turn this into codeYour turn!PB&J requirementsNow break down the taskSo now we have 1 level...A well-known algorithmAssignment P3Opening existing projectsHomeworkCOMP 14: switch, Developing ProgramsJune 2, 2000Nick VallidisAnnouncementsP3 due Tuesday, June 6ReviewHow does the do loop work?How does the for loop work?dostatement;while (condition);for (initialization; condition; increment)statement;Review (cont.)What are the 4 ways to decrement a variable?What's the difference between ++i and i++?Todaythe switch statementSteps to take in developing a programswitchThe switch statement format:switch (expression){case exp1:statement1;break;case exp2:statement2;break;default:statement;}switchThe switch statement format:switch (expression){case exp1:statement1;break;case exp2:statement2;break;default:statement;}JavareservedwordsswitchThe 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 defaultswitchThe 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 switchswitchThe 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 outswitch example// i is a character the user typed inswitch(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");}comments on switchonly works with integral types (any primitive type except boolean or the floating point types)This is basically an equality testDeveloping programsThe book describes for basic stepsestablishing the requirementscreating a designimplementing the codetesting the implementationDeveloping programsFor the small code fragments we've done in class, we've gone straight for the implementationFor the rest of your assignments, it will really help you to go through this processExample: assignment P2We'll use assignment P2Establishing requirementsHere are the requirements:read in two namesgreet the usersread in the day of the month that each of the people was bornreport winner as person born earlier in monthif it's a tie, report that it's a tieAnything else?Creating a designWe'll start by breaking it down into manageable steps:1) ask for players' names1.5) greet users2) ask for players' days of birth3) determine winner (or that it's a tie)4) print out resultCreating a designThen 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.Exciting new term!pseudocode - a mixture of programming statements and English Example:if class is overeveryone can leaveelseeveryone stays1) ask for namesprompt for first nameread in first nameprompt for second nameread in second name2) ask for day of birth ask (first name) for day of birthread in day of birthask (second name) for day of birthread in day of birth3) determine winnerif (days of birth are equal)it's a tieelse{if (person 1's day < person 2's day)person 1 was born firstelseperson 2 was born first}if (days of birth are equal)print it's a tieelse{if (person 1's day < person 2's day)print person 1 was born firstelseprint person 2 was born first}4) print out resultTurns out that it's easier to combine 3&4Now turn this into codeThe pseudocode should be written such that turning it into code is trivial.Your turn!Try this with the PB&J exampleLet's talk about the requirements together first...PB&J requirementswhat you have:robot w/ 2 arms, jar of PB, jar of J, 2 slices of bread, 1 knifewhat you have to do:make PB&J sandwichrobot can only take simple instructionsNow break down the taskAim for a maximum of about 7 steps at any one levelSo 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!A well-known algorithmMaybe you've seen this before?LatherRinseRepeatWhat's wrong with this as an algorithm?Assignment P3Here'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?" loopadd checking for valid inputdo the extra creditOpening existing projectsSome 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 projectsclick on my computer and keep going through folders till you get to the right one and double-click on <project name>.slnHomeworkread 4.1-4.2 (and 3.3, 3.5 if you missed that)P3 is due Tuesday (start
View Full Document