Postcondition: Moe, Larry, Curly, and Shemp totals hav - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Postcondition: Moe, Larry, Curly, and Shemp totals hav

Description:

Postcondition: Moe, Larry, Curly, and Shemp totals have user-specified values; ... 2 is Curly, #3 is Shemp. int stoogeTotal; // The total number of survey ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 23
Provided by: william732
Learn more at: http://www.cs.siue.edu
Category:

less

Transcript and Presenter's Notes

Title: Postcondition: Moe, Larry, Curly, and Shemp totals hav


1
The Software System Life Cycle
2
Specification Example
  • Produce a program which will
  • Request four values from the user, representing
    the popularity of the four stooges (Moe, Larry,
    Curly, and Shemp), based upon a recent survey.
  • Compute the percentage popularity for each
    stooge.
  • Display the result in a pie chart composed of
    ASCII characters, with a different character used
    for each stooge.
  • Display a character legend indicating which
    character is associated with which stooge.

3
Design Example
4
Risk Analysis Example
  • Time Risks
  • Having an interactive session instead of just
    reading the survey data from a file might delay
    the production of the pie chart.
  • Ethical Risks
  • Displaying these survey results might be
    considered an invasion of privacy by one or more
    of the stooges.
  • No information about the validity of the survey
    results is being provided.
  • Economic Risks
  • Having an interactive session instead of just
    reading the survey from a file might waste the
    users time.
  • To adequately present the pie chart and legend
    information, more expensive display equipment
    might be necessary.
  • Training users to correctly input the survey
    data, and to correctly interpret the output
    results, could prove expensive.
  • Health Risks
  • Moe might poke the software development team in
    the eyes if he doesnt like the survey results.
  • Curly might mistake the pie chart for a real pie
    and tossing the computer monitor into someones
    face.
  • More seriously, the choice of ASCII characters
    used in the pie chart might create
    headache-inducing patterns.

5
Verification Example
  • Calculating Popularity Percentages
  • Convert survey total for specified stooge into a
    double value.
  • Divide stooges total by the total number of
    people surveyed.
  • The result is the fraction of people surveyed who
    prefer the specified stooge.
  • To obtain actual percentage, this value must be
    multiplied by 100.
  • Calculating Pie-Chart Angles
  • Preset an angle-so-far value to zero it
    represents the amount of the circular pie which
    has already been traversed (i.e., the angle at
    which the current stooges pie wedge begins).
  • Add the product of 2?. and the specified stooges
    popularity percentage to obtain the angle at
    which the current stooges pie wedge ends.
  • Reset the angle-so-far to the newly computed
    value, in preparation for the the next stooges
    calculation.
  • All angles are measured in radians to facilitate
    the use of the angle operations defined in math.h.
  • Determining Stooge Character at Position (x,y)
  • The pie chart is being mapped to a square grid
    of ASCII characters, indexed from -MAXRADIUS to
    MAXRADIUS (for some hard-coded value of
    MAXRADIUS, the radius of the circular pie chart).
  • For each (x,y) position, compute the pie-chart
    angle of this position by using arctan(x/y).
  • Angles between zero and Moes maximum angle will
    cause the position to be filled with Moes ASCII
    character angles between Moes maximum and
    Larrys maximum will result in the Larry
    character angles between Larrys maximum and
    Curlys maximum will result in the Curly
    character and angles greater than Curlys
    maximum will result in Shemps character.

6
Coding Example
//////////////////////////////////////////////////
//////////////////////////////// // This program
requests four values from the user, representing
survey results // // from a poll which was taken
to determine people's favorite Three Stooges
// // character Moe, Larry, Curly, or Shemp.
Percentages are computed and the // // results
are displayed in a pie chart composed entirely of
characters. // ////////////////////////////
//////////////////////////////////////////////////
//// include ltiostreamgt include
ltiomanipgt include ltcmathgt using namespace
std void queryUser(int total, int
stgTot) void calculate(int tot, int stgTot,
double pct, double ang) void outputPie(double
angle) void outputLegend(double
percent) const int MAXRADIUS 6
// Pie chart radius (in characters) const double
PI 3.141592653589793 // Extremely precise
approximation const char MOECHAR '',
// Pie chart character for Moe
LARRYCHAR '', // " " "
" Larry CURLYCHAR 'O', //
" " " " Curly
SHEMPCHAR '\\' // " " "
" Shemp
7
Coding Example (Continued)
void main() int surveyTotal4 // The
survey results for the four stooges, indicating
// how many
respondents selected each character as
// their favorite stooge 0 is
Moe, 1 is Larry,
// 2 is Curly, 3 is Shemp. int stoogeTotal
// The total number of survey respondents
(i.e., the // sum of
the four surveyTotal values). double
surveyPercent4 // The percentage of the
survey results allocated to
// each stooge 0 is Moe, 1 is Larry, 2
is Curly, // 3 is
Shemp. double surveyAngle4 // The angle
(in radians) encompassed by each section
// of the pie chart. Thus,
Moe's section goes from 0
// to surveyAngle0, Larry's from
surveyAngle0 to //
surveyAngle1, etc. int i
// Loop iteration variable.
queryUser(surveyTotal, stoogeTotal) for (i
0 i lt 3 i) calculate(surveyTotali,
stoogeTotal, surveyPercenti, surveyAnglei)
outputPie(surveyAngle) outputLegend(surveyPer
cent) return
8
Coding Example (Continued)
//////////////////////////////////////////////////
////////////////////////// // Query the user for
the survey results, compute percentages and
angles. // ///////////////////////////////////////
///////////////////////////////////// void
queryUser(int total, int stgTot) int i
// Loop iteration variable. cout ltlt "How many
people surveyed chose Moe as their favorite
Stooge? " cin gtgt total0 cout ltlt "How many
people surveyed chose Larry as their favorite
Stooge? " cin gtgt total1 cout ltlt "How many
people surveyed chose Curly as their favorite
Stooge? " cin gtgt total2 cout ltlt "How many
people surveyed chose Shemp as their favorite
Stooge? " cin gtgt total3 stgTot 0 for (i
0 i lt 3 i) stgTot totali return
9
Coding Example (Continued)
//////////////////////////////////////////////////
//////////////////////// // Calculate the
popularity percentage and maximum pie-chart angle
for // // the designated stooge, using a running
tally of the pie-chart angle // // that has
already been traversed by previous stooge
calculations. // /////////////////////////////
///////////////////////////////////////////// void
calculate(int tot, int stgTot, double pct,
double ang) static double angleSoFar 0.0
// This static variable tallies the
// total of all of the
angles for //
the previously computed stooges. pct
double(tot) / stgTot ang angleSoFar 2 PI
pct angleSoFar ang return
10
Coding Example (Continued)
//////////////////////////////////////////////////
//////////////////////// // Cycle point by point
through the character positions. For any char-
// // acter within the pie chart's boundaries,
determine which stooge's // // pie wedge it
lies within, and insert the appropriate
character. // ///////////////////////////////
/////////////////////////////////////////// void
outputPie(double angle) int x,
// The horizontal and vertical
coordinates y // of
the pie chart point being considered. double
distanceFromCenter // The distance (in
characters) from the
// pie chart's center to the point which
// is currently being
considered. double currentAngle //
The angle (in radians) formed by the three
// points the point at
the top of the pie
// chart, the center of the pie chart, and the
// point which is
currently being considered.
11
Coding Example (Continued)
cout ltlt endl for (y MAXRADIUS y gt
-MAXRADIUS y--) cout ltlt '\t'
for (x -MAXRADIUS x lt MAXRADIUS x)
distanceFromCenter sqrt(x x y
y) if (distanceFromCenter gt MAXRADIUS)
cout ltlt ' ' else
currentAngle atan2(x, y) // Library
function atan2 returns arctan if
(currentAngle lt 0) // of x/y (adjusting if
y0) 2PI may be currentAngle
2 PI // added to yield angles betw 0 and
2PI. if (currentAngle lt angle0)
cout ltlt MOECHAR else if
(currentAngle lt angle1) cout ltlt
LARRYCHAR else if (currentAngle lt
angle2) cout ltlt CURLYCHAR
else cout ltlt SHEMPCHAR
cout ltlt endl return
12
Coding Example (Continued)
//////////////////////////////////////////////////
//////////// // A small legend is added below the
pie chart to indicate // // which character is
associated with which of the stooges.
// ///////////////////////////////////////////////
/////////////// void outputLegend(double
percent) cout.setf(iosfixed) cout ltlt
setprecision(3) cout ltlt "\t\t\t\t" ltlt MOECHAR
ltlt " - Moe " ltlt (percent0 100) ltlt
"\n" cout ltlt "\t\t\t\t" ltlt LARRYCHAR ltlt " -
Larry " ltlt (percent1 100) ltlt "\n" cout
ltlt "\t\t\t\t" ltlt CURLYCHAR ltlt " - Curly " ltlt
(percent2 100) ltlt "\n" cout ltlt "\t\t\t\t"
ltlt SHEMPCHAR ltlt " - Shemp " ltlt (percent3
100) ltlt "\n" cout ltlt endl return
13
Testing Example
14
Refinement Example
REFINEMENT Since displayed characters are 50
taller than they are wide, change output
algorithm to print 50 more characters on each
line, making pie chart look more circular.
for (y MAXRADIUS y gt -MAXRADIUS y--)
cout ltlt '\t' for (x
int(-1.5MAXRADIUS) x lt int(1.5MAXRADIUS)
x) distanceFromCenter
sqrt((2.0x/3.0) (2.0x/3.0) y y)
if (distanceFromCenter gt MAXRADIUS)
cout ltlt ' ' else
currentAngle atan2(2.0x/3.0, y) // atan2
returns arctan of 2x/3y if
(currentAngle lt 0) // (adjusting if
y0) 2PI may be currentAngle
2 PI // added to yield angles in
0,2PI. if (currentAngle lt
angle0) cout ltlt MOECHAR
else if (currentAngle lt angle1)
cout ltlt LARRYCHAR else if
(currentAngle lt angle2) cout ltlt
CURLYCHAR else cout ltlt SHEMPCHAR
cout ltlt endl
Modified Loop
Modified Output
15
Production Example
16
Maintenance Example
17
Procedural Abstraction Information Hiding
To simplify the implementation of each module,
functional specifications should be hidden from
public view, i.e., other modules should not be
aware of the specifications of a separate module.
Does any module besides queryUser need to know
how the survey values are set?
Does any module besides outputPie need to know
how the wedges on the pie are printed?
Does any module besides calculate need to know
how the angles are calculated?
Does the outputLegend module need to know
anything besides the survey result values?
Does the queryUser module need to accept the
stooge total parmeter as a call-by-reference
parameter?
18
Object-Oriented Design
  • By viewing programming from an object-oriented
    perspective instead of a procedural perspective,
    additional mechanisms for hiding information (and
    thus safeguarding our programs) have been
    developed
  • Encapsulation
  • Place the specific implementation details inside
    the object being manipulated (e.g., how a list
    object is searched or sorted, how a string object
    is output).
  • Inheritance
  • An object class that shares major features with a
    previously defined object class can just
    inherit those features from the old class
    without having them rewritten (e.g., a credit
    card account class could inherit from a more
    generic bank account class).
  • Polymorphism
  • Operators can be overloaded to mean different
    things, depending on what class of object is
    using them (e.g., the addition operator for
    integers might be very different than the
    addition operator for bank accounts, but both
    would use the symbol to denote addition).
  • Well examine these mechanisms in further detail
    as we proceed into the concept of C classes.

19
Advantages of Modularity
  • Simplifies the construction of a program
  • Writing various small functions is simpler than
    orchestrating a huge single-unit program.
  • Simplifies the debugging of a program
  • Isolating errors to particular modules is made
    possible, and testing individual functions is
    easier than debugging a massive piece of code.
  • Improves the readability of a program
  • Its easy to get lost in a huge program, but
    separating functionality into self-contained
    modules makes it easier to follow the logic of
    whats happening in the code (especially, if the
    functions are well named and documented).
  • Enables a program to be easily modified
  • Modifications to algorithms are isolated to a
    small set of modules, if each module has been set
    up to perform a single cohesive task.
  • Eliminates redundant code
  • Similar functionality occurring in various
    locations in the program can be implemented as a
    generic function instead of repetitive code.
  • Enables a programmer to reuse code segments in
    other programs
  • Well-written generic modules can be cut and
    pasted into other programs, rather than rewriting
    them from scratch.

20
Modifiability Guidelines
  • Using functions
  • Writing every simple task relegated to a separate
    module makes it much easier to determine what
    code segments must be altered when execution
    details are being modified.
  • Named constants
  • Declaring a constant value at the beginning of
    the program (e.g., the size of an array, a
    default value for certain types of variables) and
    using that constants name in the body of the
    program, greatly simplifies later modifications
    (e.g., using the code with an array of a
    different size, using the code with a different
    type of variable). For example, using
  • const char HORIZ_BAR _
  • can be easily replaced with
  • const char HORIZ_BAR -
  • Type definitions
  • Declaring a generic type definition at the
    beginning of a program (using a typedef
    statement) can eliminate the need to alter
    parameter and local variable types when
    modifications are needed. For example, using
  • typedef int number
  • can be easily replaced with
  • typedef long unsigned int number

21
Improving I/O
  • Interactive sessions with the user should be
    clear and concise
  • The user should be prompted for any input.
  • Good
  • Enter a Social Security Number of the form
    --
  • Bad
  • SSN
  • The input should be echoed back to the user as
    validation and for clarity.
  • Good
  • Enter the current number of goldfish 27
  • Specify the average age of the 27 goldfish (in
    months) 3
  • Specified 27 goldfish with an average age of 3
    months.
  • Bad
  • Enter the current number of goldfish 27
  • Specify the average age of the goldfish (in
    months) 3
  • Invalid input should be detected and corrected by
    the user (if possible).
  • Good
  • Enter your current age (in years) 1975
  • INVALID NUMBER - TOO LARGE. PLEASE TRY AGAIN.
  • Enter your current age (in years)

22
Dos and Donts of Programming Style
1. DO use functions extensively every
non-trivial task in the program should have its
own function. 2. DO call by value (or by
constant-reference - well see that later) when
passing parameters that should not be modified in
the subroutine. 3. DO use valued functions when
the function is producing a single value to be
returned use reference arguments if multiple
values are being produced. 4. DO check for
invalid input values and use assert statements
whenever other logical errors might be easily
detected. 5. DO include explanatory comments at
the beginning of the program, summarizing the
programs purpose, at the beginning of each
function, specifying the functions task, and
internal to the code, whenever the algorithm
being used is particularly complicated. 6. DO
name variables, constants, and functions in a
meaningful way.
1. DONT use global variables ever (although
global constants are okay). 2. DONT use goto
statements ever. Loops and conditional
statements should be used whenever flow control
is desired.
Write a Comment
User Comments (0)
About PowerShow.com