Upload
phil
View
39
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Program design example. Task: Develop an algorithm expressed in pseudocode for a specified problem. 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. - PowerPoint PPT Presentation
Citation preview
Program design example
Task: Develop an algorithm expressed in pseudocode for a specified problem
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
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
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
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
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
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
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
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
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)
Problem specification
Back toNotes
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
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
while the program is runningdisplay the available operations and prompt the user to choose oneget the user’s choiceperform the requested operation or display an error message if the choice is invalid
Describe how the methods will work
initialize totalShares, perSharePrice, perShareDiv, and userChoice to invalid values
Back toNotes
Structurechart
ReinvestmentAccount
set invalid:userChoice, perSharePrice,perShareDiv, totalShares
whileprogram is running
display menu, prompt
get userChoice switchuserChoice
Pseudocode
Structure chart
initializebalance
displaybalance
displayprice, div
updateprice, div
updatebalance
error--choice
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
get the user’s choice
read user choice into userChoice
Back toparent
perform the requested operation or display an error message if the choice is invalid
if userChoice is 1, initialize account balanceif userChoice is 2, display current balance if userChoice is 3, display current share infoif userChoice is 4, update share infoif userChoice is 5, update balanceif userChoice is 6, skip (do nothing)otherwise, print “Invalid choice.”
Back toparent
initialize account balance
print “Enter initial number of shares:”read user choice into totalShares
Back toparent
print “Current market value is ”, totalShares perSharePrice
display current balance
print “Current number of shares is ”, totalShares
else print “Per share price has not been entered”else print “Initial number of shares has not been
entered”
if totalShares is valid
if perSharePrice is valid
Back toparent
if perSharePrice is validprint “Current price per share is ”, perSharePrice
else print “Per share price has not been entered” if perShareDiv is valid
print “Current dividend rate is ”, perShareDivelse print “Dividend rate has not been entered”
if perSharePrice is validprint “Current price per share is ”, perSharePrice
else print “Per share price has not been entered” if perShareDiv is valid
print “Current dividend rate is ”, perShareDivelse print “Dividend rate has not been entered”
display current share info
Back toparent
update share info
print “Enter price per share:”read user input into perSharePriceprint “Enter dividend rate:”read user input into perShareDiv
Back toparent
update balance
add totalShares perShareDiv perSharePrice to totalShares
if totalShares is validif perSharePrice is valid
if perShareDiv is valid
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 to
parent
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
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
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.