You are on page 1of 14

Chapter 2

Examples

2.1
Generalized Pseudocode

2.2
The Triangle Problem

• The triangle program accepts 3 integers:a,b &c as


sides of a triangle

• It outputs the type of the triangle from the length of


the 3 sides
– Equilateral (all sides are equal)
– Isosceles (2 sides are equal)
– Scalene (all sides are not equal)
– Not a triangle (if the sum of any 2 sides <= the third)

• The triangle property:


– The sum of any pair of sides must be strictly greater
than the third side ..(>) not (>=)
• a<b+c
• b<a+c
• c<a+b
• a,b,& c are integers from 1 to 200

2.3
The program (ver 1, first test)

• Type TRIANGLE and press the <enter> key


– Blank screen, ? At the top of the screen
• Press 3
– ?3 appears
• Press <Enter> ? 3
– ? On the next line ? 3
• Press 3 ? 3
– ?3 appears
Equilateral
• Press <Enter>
– ? On the next line
?
• Press 3
– ?3 appears
• Press <Enter>
– Equilateral appears on the next line
– A couple lines below a ? appears

2.4
Problem reports arising from the first test

• Design Errors:
– No Program Name
– No onscreen instructions
– How do you stop the program?
• Coding Errors:
– Layout (the result ‘Equilateral’ is not well explained)
• Submit one Problem Report for each error, why?
– Problems grouped together might not be solved at the same
time
– The unfixed ones will be lost

2.5
Flowchart for the traditional triangle program
implementation

2.6
The program (ver 2)

•What different types of input could the program encounter?


•What types of boundaries should be placed on the inputs?
2.7
The NEXTDATE Problem
• An example of relationships among the input
variables
• NextDate is a function of 3 variables:
– Month, Day, Year
• It returns the day after the given date
• Inputs are numbers with boundaries:
– Month: 1..12
– Day: 1..31
– Year: 1812..2012
• Problems:
– 30 day months
– 31 day months
– February: common year
– February: leap year
– Distinguishing a leap year from a common year
– When to add a day
– When to add a month and reset day
– When to add a year, reset month and day
• A year is a Leap year if it’s divisible by 4 & not a
century year
• Century years are leap years if they are divisible by
400
• Refer to text for implementation
2.8
The Commission Problem

• Problem Statement

2.9
Commission Problem Implementation

2.10
The SATM System

• The Simple Automated Teller Machine (SATM)


• Below is the SATM terminal

• Problem Statement
– SATM customers can select any of three transaction
types: deposits, withdrawals, and balance inquiries
– Transactions can be done on checking or saving
accounts
– Every customer has a card and with a PAN (Personal
Account Number)
– Every customer has a PIN
– Customer has 3 chances to enter the PIN correctly
– (Refer to text for detailed problem statement)

2.11
The SATM System Cont’d

• The SATM system communicates with bank


customers via the 15 screens below

2.12
The Currency Converter

• An event-driven program with a graphical user


interface (GUI), shown below

• The application converts U.S. dollars to any of the


four currencies(chosen by a radio button)
• When a country is chosen, ‘Equivalent in’ label is
completed & a flag is shown

2.13
Saturn Windshield Wiper Controller

• The windshield wiper on some Saturn cars is


controlled by a lever with a dial
• The lever has 4 positions: OFF, INT(intermittent),
LOW and HIGH
• The dial has three positions: 1,2,3
– The dial positions indicate three intermittent speeds
– It is relevant only when the lever is at the INT position

• The decision table below shows the windshield wiper


speeds in wipes per minute for the lever and dial
positions.

c1. Lever OFF INT INT INT LOW HIGH


c2. Dial n/a 1 2 3 n/a n/a
a1.Wiper 0 4 6 12 30 60

2.14

You might also like