DOC PREVIEW
UNC-Chapel Hill COMP 14 - LECTURE NOTES

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 VallidisAnnouncementsP3 due Tuesday, June 6ReviewHow 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++?Todaythe switch statementSteps to take in developing a programswitchThe switch statement format:switch (expression){case exp1:statement1;break;case exp2:statement2;break;default:statement;}switchThe switch statement format:switch (expression){case exp1:statement1;break;case exp2:statement2;break;default:statement;}JavareservedwordsswitchThe 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 defaultswitchThe 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 switchswitchThe 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 switchonly works with integral types (any primitive type except boolean or the floating point types)This is basically an equality testDeveloping programsThe book describes for basic stepsestablishing the requirementscreating a designimplementing the codetesting the implementationDeveloping 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 processExample: assignment P2We'll use assignment P2Establishing 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?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 resultCreating 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.Exciting new term!pseudocode - a mixture of programming statements and English Example:if class is overeveryone can leaveelseeveryone stays1) ask for namesprompt for first nameread in first nameprompt for second nameread in second name2) 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 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 resultTurns out that it's easier to combine 3&4Now turn this into codeThe pseudocode should be written such that turning it into code is trivial.Your turn!Try this with the PB&J exampleLet's talk about the requirements together first...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 instructionsNow break down the taskAim 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 algorithmMaybe you've seen this before?LatherRinseRepeatWhat's wrong with this as an algorithm?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 creditOpening 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>.slnHomeworkread 4.1-4.2 (and 3.3, 3.5 if you missed that)P3 is due Tuesday (start


View Full Document

UNC-Chapel Hill COMP 14 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?