Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem.

Similar presentations


Presentation on theme: "Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem."— Presentation transcript:

1 Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem 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. Example problem

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 Example problem

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. Example problem

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. Example problem

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. Example problem

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. Example problem

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/OutputPseudocodeStructure chartSuggestions

10 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 to Notes

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 to Notes

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 to Notes

13 while the program is running display the available operations and prompt the user to choose one get the user’s choice perform 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 to Notes Structure chart

14 Reinvestment Account set invalid: userChoice, perSharePrice, perShareDiv, totalShares while program is running display menu, prompt get userChoice switch userChoice Pseudo code Structure chart initialize balance display balance display price, div update price, div update balance error-- choice

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 to parent

16 get the user’s choice read user choice into userChoice Back to parent

17 perform the requested operation or display an error message if the choice is invalid if userChoice is 1, initialize account balanceinitialize account balance if userChoice is 2, display current balancedisplay current balance if userChoice is 3, display current share infodisplay current share info if userChoice is 4, update share infoupdate share info if userChoice is 5, update balanceupdate balance if userChoice is 6, skip (do nothing) otherwise, print “Invalid choice.” Back to parent

18 initialize account balance print “Enter initial number of shares:” read user choice into totalShares Back to parent

19 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 to parent

20 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” display current share info Back to parent

21 update share info print “Enter price per share:” read user input into perSharePrice print “Enter dividend rate:” read user input into perShareDiv Back to parent

22 update balance add totalShares  perShareDiv  perSharePrice to totalShares if totalShares is valid if 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

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 to Notes

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 to Notes

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.


Download ppt "Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem."

Similar presentations


Ads by Google