Prep for HN Introduction to Software Development

Download Report

Transcript Prep for HN Introduction to Software Development

Prep for HN
Introduction to Software
Development
Lecture 4
Multiple Selection Statements
A simple Error Handling routine
7/17/2015
Electrical Resistors
• Remember the
resistor calculator?
• What happens if your
question has multiple
- choice answers?
• In this case, a simple
true or false is not
going to do the job!
7/17/2015
Number
Colour
0
1
2
3
black
brown
red
orange
4
5
6
yellow
green
blue
7
violet
8
9
grey
white
Electrical Resistors
•We saw that the decision diamond only
ever had two responses – yes or no, as it
was based on Boolean logic.
•But here, we would have to ask nine or
ten possible questions in turn….
•What would the flowchart look like?
7/17/2015
Electrical Resistors
Is it
black?
yes
no
Is it
brown
?
no
no
Is it
red?
Value = 0
yes
Value = 1
yes
no
Is it
orange
?
Value = 2
yes
Value = 3
7/17/2015
Electrical Resistors
Get colour of stripe
IF choice is
black then value = 0
brown then value = 1
red then value = 2
orange then value = 3
and so on ……
white then value = 9
END of choice
Number
Colour
0
black
1
brown
2
red
3
orange
4
yellow
5
green
6
blue
7
violet
8
grey
9
white
It is neater and more efficient to list multiple items
as a Case Statement.
7/17/2015
Menus
• When implementing in
code, the best method is to
offer the user a menu of
choices.
• This is very common in a
GUI environment.
• We can use drop-down
boxes, buttons, hyperlinks
etc.
7/17/2015
Menus
In a command-line program the user has to
pick a letter or number corresponding to their
choice.
7/17/2015
Menus
• Here’s what a menu looks like in Delphi.
writeln ('Windows 98 Startup Menu');
writeln ('=======================');
writeln ('1. Normal');
writeln ('2. Logged (BOOTLOG.TXT)');
writeln ('3. Safe mode');
writeln ('4. Safe mode with network support');
writeln ('5. Step-by-step confirmation');
writeln ('6. Command prompt only');
writeln ('7. Safe mode command prompt only');
write ('Enter a choice: ');
readln (choice);
7/17/2015
Menus
• Here’s the matching case statement.
case choice of
'1' : advice := 'Hard drive now reformatted!';
'2' : advice := 'All files deleted!';
'3' : advice := 'Win386.exe missing or corrupt.
Windows cannot start.';
'4' : advice := 'General Protection Error at
0xFF336699';
'5' : advice := 'Too many bad sectors. Replace
hard drive immediately!';
'6' : advice := 'Fatal error in Kernel32.exe.
Please reboot.';
'7' : advice := 'General Windows Fault. Have A
Nice Day.';
end; {of the case statement}
7/17/2015
Menus
• Look at the copy of the menu
program supplied.
• Type it in and run it.
• Test it with various inputs, both legal
and illegal. Observe what happens.
7/17/2015
Menus
• Design an
algorithm to
display a
restaurant menu,
as shown.
• Ask the user what
they would like to
eat.
• Display back the
item they chose.
7/17/2015
Sam ‘n’ Ella’s Gourmet
Restaurant
Starters
1: Salmon Mousse
2: Egg Mayonnaise
3: Melon Balls
4: Caviare On Toast
5: Soup Of The Day
6: Grapefruit Cocktail
Menus
• Amend the program to declare a variable
price.
• As well as telling
the user what
they picked, it
should also
inform them how
much it will cost.
7/17/2015
Salmon Mousse £3.95
Egg Mayonnaise £2.95
Melon Balls
£2.50
Caviare On Toast £7.95
Soup Of The Day £0.95
Grapefruit Cocktail £2.50
More Menus
• Now that you can write
a menu program, go
back to the Resistor
Calculator algorithm.
• Display a menu and
offer the user a choice
from 0 – 9.
• Repeat back to the
user which colour they
chose.
7/17/2015
Number
Colour
0
black
1
brown
2
red
3
orange
4
yellow
5
green
6
blue
7
violet
8
grey
9
white
More Menus
• Use an integer for the
choice.
• Test it with integers
bigger than 9!
• What happens?
Number
Colour
0
black
1
brown
2
red
3
orange
4
yellow
5
green
6
blue
7
violet
• Save the program
somewhere safe.
We will expand it to make a complete
Resistor Calculator at a later point.
7/17/2015
8
grey
9
white
A Simple Error Handler
Study the following algorithm.
Prompt the user to state which sex they are by
typing m or f
Get a reply
If reply = m or M then
display “You are a guy”
Else
display “You are a girl”
Is it complete, unambiguous, deterministic and finite?
7/17/2015
A Simple Error Handler
Implement the algorithm in Delphi.
Prompt the user to state which sex they are by
typing m or f
Get a reply
If reply = m or M then
display “You are a guy”
Else
display “You are a girl”
Run it and test it with different inputs. What happens?
7/17/2015
A Simple Error Handler
How could we
improve the
algorithm so that
the program will
be foolproof?
We could only permit the user to type m or f
– and keep showing them an error message
until they get it right!!!
7/17/2015
A Simple Error Handler
• A piece of code that deals with any illegal
input is called an error handler.
Prompt the user to state which sex they are by typing m
or f
Get a reply
Loop while the reply is an invalid one
Show an error message
Get the reply again
End the loop
If the reply was or m etc. etc…….
7/17/2015
A Simple Error Handler
Here’s how to implement the error handler.
while not (reply IN [‘m’, ‘M’, ‘f’, ‘F’]) do
begin
writeln (‘Error in input! Type M or F
only!’);
readln (reply);
end {while not loop};
This forces the user to only type m, M, f or F.
The program cannot continue until the reply
is one of these four things.
7/17/2015
A Simple Error Handler
•Add the error handler to your program.
•Test it with a variety of different inputs.
•Does it work every time?
•This error handler uses a construct called
a while…do loop. Next time we will look
more closely at how this actually works.
7/17/2015
Programming Practice
•Go back to the Windows Menu program and
add an error handler. Your user can only
choose the numbers between 1 – 7. Use –
while not (choice in [‘1’..’7’])
•Add an error handler to the Restaurant
program.
•Add an error handler to the Resistor
Calculator program.
7/17/2015
Next week we will...
• Look at different types of loop
structure
• Continue to implement the whole
Resistor Calculator
• Design a simple dice game based
on a while..do loop
7/17/2015
Acknowledgements
• Lecture designed and written by Chrissie
Nyssen
• Materials all ©Aberdeen College 2005 unless
otherwise stated
• Master slide © Carnegie Training
• Adam and Eve, error button © Microsoft
• Lecture available online at
http://abcolac1.abcol.ac.uk/~chrissie/PascalHome.htm
7/17/2015