1 / 20

Design with Structure Charts

Design with Structure Charts. Design Process. Problem solving and design should be done independent of programming. Code significantly clouds the design process.. It is very difficult to see the design and address design issues when coding

marcel
Download Presentation

Design with Structure Charts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design withStructure Charts

  2. Design Process • Problem solving and design should be done independent of programming. • Code significantly clouds the design process.. It is very difficult to see the design and address design issues when coding • Design needs to take place in a mode which minimizes code influence

  3. 4 Steps 1 2 Create a structure chart based on the main item, NOT programming requirements Modify structure chart based on the programming requirements. Top-down design. 4 3 Improve readabililty of the code by incorporating functions. Write code to implement the top-down design. As you mature this will be skipped by going to step 4.

  4. National Parcel Example • Example 2 : Parcel Service Company. • Emphasizes IF notation for top down design. • National Parcel Service (NPS) specializes in nationwide delivery of small packages. NPS will not accept • any packages whose largest dimension is greater than 3 feet or whose weight exceeds 50 pounds. • The charge for shipping a parcel is $0.75 plus an amount based on package weight as follows: • Weight (lb.) Rate • ----------------------------- • 20 or less $0.08 per lb. • 40 or less $0.10 per lb. • Over 40 $0.15 per lb. • ----------------------------- • There is an additional $1.00 charge if the volume of the package exceeds 18 cubic feet. Write a program • that will read the dimensions of a parcel (in feet) and its weight (in pounds) and then compute and print • the postage due. If the package is rejected, an appropriate message should be printed. • Example Test Data Length Width Depth Weight • 1.5 1.2 0.8 6.0

  5. Parcel Delivery National Parcel Service Parcel * Size Weight Shipping Status Length Width Depth Acceptable Not Acceptable Charge Weight Charge Volume Charge 0.75 Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18

  6. Change in Perspective • Our natural view of the problem domain may not match the requirements. • There are multiple views of anything. • Other views are not wrong, but do provide insight into the structure we expect and perceive. • In this problem we need to adjust for a single parcel.

  7. Parcel Delivery(adjust to one package) Parcel Size Weight Shipping Status Length Width Depth Acceptable Not Acceptable Charge Weight Charge Volume Charge 0.75 Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18

  8. TV Survey • Example 3 : Television Rating Service • Emphasizes WHILE notation for top down design • A television rating service makes a survey of the viewing audience to sample the popularity of tv shows. • When a call is made concerning a particular show, the sex and age of the person called, as well as whether • or not the person watches the program regularly, are recorded. Write a program that will process the data • gathered for the show. The total number of people called, the number who said they watched the show • regularly, and the percentage of those who watch the show on a regular basis should be printed. The • program should also print a table showing the percentages of those who watch the show by sex and age • categories. The output table should look something like the sample shown here. • SEX UNDER 18 18 to 35 36 to 55 OVER 55 • ------------------------------------------------------------------ • MALE 12.2% 47.5% 34.3% 6.0% • FEMALE 18.5% 32.4% 35.6% 13.5% • ------------------------------------------------------------------

  9. TV Survey Survey Call * Sex Viewer Status Age Male Female Regular Not Regular Age<18 18<=Age<36 36<=Age<55 Age>55

  10. Requirements demandadjusting view • Sex and age are typically related only through the person, not as subcategories. • This problem views them as subcategories of one another. • It doesn’t make any difference which is a subcategory of the other. • Next slide shows an adjusted view.

  11. TV Survey Survey Call * Viewer Status Regular Not Regular Sex Male Female Age<18 18<=Age<36 36<=Age<55 Age>55 Same as Male

  12. Step 2 Adapt the structure chart to accommodate the program specifications: DO A TOP-DOWN DESIGN

  13. Parcel DeliveryAdjusting Structure Chart for Program (Top-Down) Design Compute Postage Input Package Info Determine Acceptability Not Acceptable Size Weight Acceptable Length Width Depth Compute Charge Output Charge Output Rejection Message Compute Weight Charge Compute Volume Charge Compute Total Charge Charge= Wt_Chrg+ Vol_Chrg+ 0.75 Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18 Wt_chrg= Wt*0.08 Wt_chrg= Wt*0.1 Wt_chrg= Wt*0.15 Vol_chrg= 0.0 Vol_chrg= 1.0

  14. TV SurveyStructure Chart Conversion to Top Down Design Summarize Survey Initializations Process Call * Output Report Input Call Info Count # of Calls Process Regular Viewers Calculate % Write Report Regular Not Regular Count Regular Viewer Sex Male Female Age<18 18<=Age<36 36<=Age<55 Age>55 Same as Male but use WL18, WL36,... Increment ML18 Increment ML36 Increment ML55 Increment MG55

  15. Step 3 Write code to implement the design

  16. #include <iostream.h> main () { float length,width,height,weight,WtChrg, volume,VolChrg,TotalCharge; // input package information cout << " Enter length, width and height of package\n"; cin >> length >> width >> height; cout << " Enter weight of package\n"; cin >> weight; // determine acceptability if ( (length<3) && (height<3) && (width<3) && (weight<50)) { // ACCEPTABLE // COMPUTE CHARGE // WEIGHT CHARGE if (weight<=20) WtChrg = 0.08*weight; else if (weight<=40) WtChrg = 0.10*weight; else WtChrg = 0.15*weight; // VOLUME CHARGE volume = length*width*height; if (volume > 18.0) VolChrg = 1.0; else VolChrg = 0.0; // TOTAL CHARGE TotalCharge = 0.75 + WtChrg + VolChrg; // OUTPUT CHARGE cout << "Total charge for the package is " << TotalCharge << endl; } else // NOT ACCEPTABLE { cout << "Package EXCEEDS weight/size specs for shipment\n"; cout << "...... Try another company!"; } } Parcel Delivery Implementation

  17. while (num_read < NumberToRead) { // PROCESS CALL // input call information cin >> sex >> age >> regular; // count number of calls tv++; num_read++; // process regular viewer if ( (regular == 'R') || (regular == 'r')) { // count as a regular viewer rv++; // tabulate age/sex info if ( (sex == 'M') || (sex == 'm')) { // process Male if (age < 18) ml18++; else if (age < 36) ml36++; else if (age < 55) ml55++; else mg55++; } else { // process Female if (age < 18) fl18++; else if (age < 36) fl36++; else if (age < 55) fl55++; else fg55++; } } } // loop PercentRegular=rv / tv; // write reports to output device // ........ } // end TV Survey Implementation #include <iostream.h> main () { char sex, regular; float PercentRegular; int age, rv, tv, num_read, NumberToRead; int ml18,ml36,ml55,mg55,fl18,fl36,fl55,fg55; // initializations tv=0; rv=0; ml18=0;ml36=0;ml55=0;mg55=0; fl18=0;fl36=0;fl55=0;fg55=0; cin >> NumberToRead; num_read = 0;

  18. Step 4 Use FUNCTIONS to implement the design

  19. Parcel Delivery Implementation w/functions // FUNCTIONS int Acceptable(float length, float height, float width, float weight) { return ( (length<3) && (height<3) && (width<3) && (weight<50) ); } float WtChrg(float weight) { float wc; if (weight<=20) wc = 0.08*weight; else if (weight<=40) wc = 0.10*weight; else wc = 0.15*weight; return wc; } float PackageVolume(float length, float height, float width) { return length*height*width;} float VolChrg(float v) { float vcharge; if (v > 18.0) vcharge = 1.0; else vcharge = 0.0; return vcharge; } void Reject() { cout << "Package EXCEEDS weight/size specs for shipment"; cout << " ...... Try another company!"; } #include <iostream.h> // PROTOTYPES int Acceptable(float length, float height, float width, float weight); float WtChrg(float weight); float PackageVolume(float length, float height, float width); float VolChrg(float v); void Reject(); // MAIN ROUTINE main () { float length,width,height,weight,volume,TotalCharge; // input package information cout << " Enter length, width and height of package\n"; cin >> length >> width >> height; cout << " Enter weight of package\n"; cin >> weight; // determine acceptability if ( Acceptable(length,height,width,weight)) { // ACCEPTABLE volume = PackageVolume(length,width,height); TotalCharge = 0.75 + WtChrg(weight) + VolChrg(volume); cout << "Total charge for the package is " << TotalCharge << endl; } else // NOT ACCEPTABLE Reject(); }

  20. TV Survey Implementation w/functions It’s Your Turn!

More Related