320 likes | 401 Views
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 ;.
E N D
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;
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 program
switch • The switch statement format: switch (expression) { caseexp1: statement1; break; case exp2: statement2; break; default: statement; }
switch • The switch statement format: switch (expression) { caseexp1: statement1; break; case exp2: statement2; break; default: statement; } Java reserved words
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
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
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
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"); }
comments on switch • only works with integral types (any primitive type except boolean or the floating point types) • This is basically an equality test
Developing programs • The book describes for basic steps • establishing the requirements • creating a design • implementing the code • testing the implementation
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
Example: assignment P2 • We'll use assignment P2
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?
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
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.
Exciting new term! • pseudocode - a mixture of programming statements and English • Example: if class is over everyone can leave else everyone stays
1) ask for names • prompt for first name • read in first name • prompt for second name • read in second name
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
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 }
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 }
Now 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 instructions
Now break down the task • Aim for a maximum of about 7 steps at any one level
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!
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 credit
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
Homework • read 4.1-4.2 (and 3.3, 3.5 if you missed that) • P3 is due Tuesday (start now!)