Top Banner
COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis
32

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

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COMP 14: switch, Developing Programs June 2, 2000 Nick Vallidis.

COMP 14: switch, Developing Programs

June 2, 2000Nick Vallidis

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

Announcements

P3 due Tuesday, June 6

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

Review

How does the do loop work?

How does the for loop work?

do

statement;

while (condition);

for (initialization; condition; increment)

statement;

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

Review (cont.)

What are the 4 ways to decrement a variable?

What's the difference between ++i and i++?

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

Today

the switch statementSteps to take in developing a

program

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

switch

The switch statement format:switch (expression){

case exp1:statement1;break;

case exp2:statement2;break;

default:statement;

}

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

switch

The switch statement format:switch (expression){

case exp1:statement1;break;

case exp2:statement2;break;

default:statement;

}

Java

reserved

words

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

switch

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 default

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

switch

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 switch

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

switch

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 out

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

switch 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");

}

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

comments on switch

only works with integral types (any primitive type except boolean or the floating point types)

This is basically an equality test

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

Developing programs

The book describes for basic steps establishing the requirements creating a design implementing the code testing the implementation

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

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

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

Example: assignment P2

We'll use assignment P2

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

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?

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

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

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

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.

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

Exciting new term!

pseudocode - a mixture of programming statements and English

Example:if class is over

everyone can leaveelse

everyone stays

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

1) ask for names

prompt for first nameread in first nameprompt for second nameread in second name

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

2) 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 birth

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

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

elseperson 2 was born first

}

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

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

elseprint person 2 was born first

}

4) print out result

Turns out that it's easier to combine 3&4

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

Now turn this into code

The pseudocode should be written such that turning it into code is trivial.

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

Your turn!

Try this with the PB&J example

Let's talk about the requirements together first...

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

PB&J requirements

what 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

instructions

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

Now break down the task

Aim for a maximum of about 7 steps at any one level

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

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!

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

A well-known algorithm

Maybe you've seen this before? Lather Rinse Repeat

What's wrong with this as an algorithm?

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

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

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

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

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

Homework

read 4.1-4.2 (and 3.3, 3.5 if you missed that)

P3 is due Tuesday (start now!)