1 / 12

Stepwise Refinement

Stepwise Refinement. A Method for Program Design. Stepwise Refinement. An approach to creating a program by decomposing the problem into simpler sub-problems Start with the main method Identify any required classes/services Write classes/services Identify "helper" services.

rebecca
Télécharger la présentation

Stepwise Refinement

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. Stepwise Refinement A Method for Program Design

  2. Stepwise Refinement • An approach to creating a program by decomposing the problem into simpler sub-problems • Start with the main method • Identify any required classes/services • Write classes/services • Identify "helper" services

  3. Advantages to Stepwise Refinement • When using this method, the final program is: • Easy to understand • Easy to debug • Easy to modify

  4. Sample Problem • Create a Robot that can “harvest” a field of Things: • The Robot will alwaysstart at the top-leftcorner • The Robot must pickup all Things in thegrid

  5. Repeat 6x Planning Using Stepwise Refinement Create the Main Method: • What does the Harvester need to do to pick up a field of Things? • Harvest each row • How do you harvest an entire row? • Move West --> East, picking things • How would you do the entire field? • Harvest Row • Turn around • Move to beginning of row • Move South

  6. Evaluate Plan • Advantages: • Entire field is harvested • Meets requirements • Disadvantages: • “Empty trips” when Harvester returns to the beginning of the row • Improvements? • At the end of a row, move South and harvest on the way back

  7. Repeat 3x Modify Plan • Harvest Two Rows • Move South

  8. Evaluate Plan • Advantages: • Entire field is harvested • Meets requirements • More efficient (less trips) • Disadvantages: • Only works when there is an even number of rows

  9. Stub Implement Plan Using Stepwise Refinement • Create the harvestField ( ) command: public void harvestField ( ) { this.harvestTwoRows( ); this.positionForNextHarvest( ); this.harvestTwoRows( ); this.positionForNextHarvest( ); this.harvestTwoRows( ); this.positionForNextHarvest( ); }

  10. Implement Plan Using Stepwise Refinement • Stubs included in the harvestField method indicate sections of code that require refinement • Each stub should be given an empty header in the program so that the class can be compiled and tested • Code is then created for empty stubs

  11. Notice this method is protected Implement Plan Using Stepwise Refinement • Create the harvestTwoRows( ) command: protected void harvestTwoRows ( ) { this.harvestRow( ); this.positionForReturnTrip( ); this.harvestRow( ); }

  12. Implement Plan Using Stepwise Refinement • This pattern is continued until all methods have been completed • Notice that the terminology remains the same throughout the class (harvest, position, etc.)

More Related