Program design example PowerPoint PPT Presentation

presentation player overlay
1 / 25
About This Presentation
Transcript and Presenter's Notes

Title: Program design example


1
Program design example
  • Task Develop an algorithm expressed in
    pseudocode for a specified problem

2
Program design steps
  • Problem specification
  • Define inputs and outputs
  • Decompose into classes and methods
  • Describe how the methods will work
  • Convert method descriptions to code
  • Test the code

3
Problem specification
  • Based on information from customer
  • job supervisor (employee)
  • client (independent consultant)
  • instructor (student)
  • State what the program must do.
  • Specification is like a contract.
  • Summarize specification in Purpose section of
    header comment.

Exampleproblem
4
Define inputs and outputs
  • Describe information input by user.
  • Describe results output to user.
  • Include descriptions in data dictionary
  • most inputs will correspond to variables
  • outputs may be variables or expressions
  • other items
  • constants
  • transitional variables

Exampleproblem
5
Decompose into classes and methods
  • Simple programs that perform a single task may be
    implemented with a single class containing a
    single method, main.
  • When a design includes more than one class or
    method, each should be documented as to its
    specific purpose within the overall design.

Exampleproblem
6
Describe how the methods will work
  • Show each method as a separate algorithm.
  • Algorithm design should be precise, but should
    not include programming language details like
    type declarations and syntax.
  • Use variable names.
  • Use pseudocode.
  • Indent to show program structure.

Exampleproblem
7
Convert method descriptions to code
  • Choose appropriate types and declare constants,
    input and output variables, and transition
    variables.
  • Converting pseudocode statements to Java
    statements should only involve changes to conform
    to Java syntax.
  • Do not change the design while writing code.

Exampleproblem
8
Test the code
  • Do not change the design when correcting code for
    compiler errors.
  • Do not poke and hope. If you get a compiler
    error or incorrect output, first determine why
    you got the error.
  • Choose test input that exercises each part of
    your program.
  • If output is wrong, change the design.

Exampleproblem
9
Reinvestment account
  • Money invested in shares of stock may be
    maintained in a special reinvestment account.
  • When dividends are paid, they are automatically
    used to purchase additional shares, which are
    added to the account.
  • Dividends must be computed for original as well
    as reinvested shares.

Input/Output
Pseudocode
Structure chart
Suggestions
10
Problem specification
  • Write a program to maintain a reinvestment
    account. The following operations must be
    implemented
  • initialize the account balance
  • display the current balance (number of shares and
    current market value)
  • display current price and dividend per share
  • update price and dividend information
  • update the balance (at current dividend rate)

Back toNotes
11
Define inputs and outputs
  • Inputs
  • operation to be performed
  • number of shares in account
  • price per share
  • dividend rate per share
  • Outputs
  • number of shares in account
  • market value of account
  • price per share
  • dividend rate per share

Back toNotes
12
Decompose into classes and methods
  • A single class will be used, with a single
    method, main.
  • An alternative approach, using multiple methods,
    would be to implement each of the required
    operations as an individual method.

Back toNotes
13
Describe how the methods will work
  • initialize totalShares, perSharePrice,
    perShareDiv, and userChoice to invalid values
  • while the program is running
  • display the available operations and prompt the
    user to choose one
  • get the users choice
  • perform the requested operation or display an
    error message if the choice is invalid

Back toNotes
Structurechart
14
Structure chart
ReinvestmentAccount
Pseudocode
15
display the available operations and prompt the
user to choose one
  • print Reinvestment account, available
    operations
  • print 1. Initialize the account balance
  • print 2. Display the current balance (number of
    shares and current market value)
  • print 3. Display current price and dividend per
    share
  • print 4. Update price and dividend information
  • print 5. Update the balance (at current dividend
    rate)
  • print 6. Quit the program
  • print Enter desired operation

Back toparent
16
get the users choice
  • read user choice into userChoice

Back toparent
17
perform the requested operation or display an
error message if the choice is invalid
  • if userChoice is 1, initialize account balance
  • if userChoice is 2, display current balance
  • if userChoice is 3, display current share info
  • if userChoice is 4, update share info
  • if userChoice is 5, update balance
  • if userChoice is 6, skip (do nothing)
  • otherwise, print Invalid choice.

Back toparent
18
initialize account balance
  • print Enter initial number of shares
  • read user choice into totalShares

Back toparent
19
display current balance
if totalShares is valid
  • print Current number of shares is , totalShares

if perSharePrice is valid
print Current market value is , totalShares ?
perSharePrice
else print Per share price has not been
entered else print Initial number of shares has
not been entered
Back toparent
20
display current share info
if perSharePrice is valid print Current price
per share is , perSharePrice else print Per
share price has not been entered if perShareDiv
is valid print Current dividend rate is ,
perShareDiv else print Dividend rate has not
been entered
  • if perSharePrice is valid
  • print Current price per share is ,
    perSharePrice
  • else print Per share price has not been entered
  • if perShareDiv is valid
  • print Current dividend rate is , perShareDiv
  • else print Dividend rate has not been entered

Back toparent
21
update share info
  • print Enter price per share
  • read user input into perSharePrice
  • print Enter dividend rate
  • read user input into perShareDiv

Back toparent
22
update balance
  • if totalShares is valid
  • if perSharePrice is valid
  • if perShareDiv is valid

add totalShares ? perShareDiv ? perSharePrice to
totalShares
else print Dividend rate has not been
entered else print Per share price has not been
entered else print Initial number of shares has
not been entered
Back toparent
23
Convert method descriptions to code
  • Define constants for
  • valid menu choices (int)
  • invalid data value (double)
  • error messages (String)
  • Use switch statement to implement menu
  • If you change the design, you must submit a copy
    of the design with your changes along with your
    code

Back toNotes
24
Test the code
  • Test all switch cases and selection branches,
    including those that display error messages
  • Make sure numeric formulas produce correct
    results
  • Make sure prompts and other elements of the user
    interface are coherent and understandable

Back toNotes
25
Suggestion Box
  • Suggest at least one improvement to the user
    interface or program design.
  • The best suggestions, if feasible, will be
    incorporated into future lab assignments.
    Particular consideration will be given to
    improvements involving the use of arrays.
  • If there are no feasible suggestions, changes to
    the design dictated by the instructor may be
    assigned for future labs.
Write a Comment
User Comments (0)
About PowerShow.com