0% found this document useful (0 votes)
18 views12 pages

Stepwise Refinement: Eric Roberts CS 106A April 6, 2012

The document discusses stepwise refinement in programming. It explains that stepwise refinement involves breaking down a complex problem into successively simpler subproblems. This process makes the problem easier to solve by dividing it into smaller, more manageable tasks. The document provides examples of using stepwise refinement to solve Karel programming exercises that involve placing beepers in a grid world and decorating "trees" by adding bundles of leaves. It emphasizes defining preconditions and postconditions for methods to avoid bugs from incorrect assumptions.

Uploaded by

api-127299018
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

Stepwise Refinement: Eric Roberts CS 106A April 6, 2012

The document discusses stepwise refinement in programming. It explains that stepwise refinement involves breaking down a complex problem into successively simpler subproblems. This process makes the problem easier to solve by dividing it into smaller, more manageable tasks. The document provides examples of using stepwise refinement to solve Karel programming exercises that involve placing beepers in a grid world and decorating "trees" by adding bundles of leaves. It emphasizes defining preconditions and postconditions for methods to avoid bugs from incorrect assumptions.

Uploaded by

api-127299018
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Stepwise Refinement

Eric Roberts CS 106A April 6, 2012

Outline
1. Review Karel statement forms 2. Exercise: The putBeeperLine method 3. Stepwise refinement 4. Exercise: The BanishWinter program 5. Preconditions and postconditions 6. Story time: Past Karel Contest winners

Review: The Karel Language

Review: The Karel Language

Exercise: Creating a Beeper Line


Write a method putBeeperLine that adds one beeper to every intersection up to the next wall. Your method should operate correctly no matter how far Karel is from the wall or what direction Karel is facing. Consider, for example, the following main program:
public void run() { putBeeperLine(); turnLeft(); putBeeperLine(); }

2 +

Stepwise Refinement
The most effective way to solve a complex problem is to break it down into successively simpler subproblems. You start by breaking the whole task down into simpler parts.

Some of those tasks may themselves need subdivision.


This process is called stepwise refinement or decomposition. Complete Task

Subtask #1

Subtask #2

Subtask #3

Subsubtask #2a

Subsubtask #2b

Criteria for Choosing a Decomposition


1. The proposed steps should be easy to explain. One indication that you have succeeded is being able to find simple names. 2. The steps should be as general as possible. Programming tools get reused all the time. If your methods perform general tasks, they are much easier to reuse. 3. The steps should make sense at the level of abstraction at which they are used. If you have a method that does the right job but whose name doesnt make sense in the context of the problem, it is probably worth defining a new method that calls the old one.

Exercise: Banishing Winter


In this problem (which is described in detail in Handout #11), Karel is supposed to usher in springtime by placing bundles of leaves at the top of each tree in the world. Given this initial world, the final state should look like this:
6 5 4 3 2 1 + + + + + + 1 + + + + + + 2 + + + + + + 3 + + + + + + 4 + + + + + + 5 + + + + + + 6 + + + + + + 7 + + + + + + 8 + + + + + + 9 + + + + + + 10 + + + + + + 11 + + + + + + 12 + + + + + + 13 + + + + + + 14

Understanding the Problem


One of the first things you need to do given a problem of this sort is to make sure you understand all the details. According to the handout, Karel stops when it runs out of beepers. Why couldnt it just stop at the end of 1st Street?
6 5 4 3 2 1 + + + + + + 1 + + + + + + 2 + + + + + + 3 + + + + + + 4 + + + + + + 5 + + + + + + 6 + + + + + + 7 + + + + + + 8 + + + + + + 9 + + + + + + 10 + + + + + + 11 + + + + + + 12 + + + + + + 13 + + + + + + 14

The Top-Level Decomposition


You can break this program down into two tasks that are executed repeatedly:
Find the next tree. Decorate that tree with leaves.
6 5 4 3 2 1 + + + + + + 1 + + + + + + 2 + + + + + + 3 + + + + + + 4 + + + + + + 5 + + + + + + 6 + + + + + + 7 + + + + + + 8 + + + + + + 9 + + + + + + 10 + + + + + + 11 + + + + + + 12 + + + + + + 13 + + + + + + 14

Preconditions and Postconditions


Many of the bugs that you are likely to have come from being careless about the conditions under which you use a particular method. As an example, it would be easy to forget the turnLeft call at the end of the addLeavesToTree method. To reduce the likelihood of such errors, it is useful to define pre- and postconditions for every method you write.
A precondition specifies something that must be true before a method is called. A postcondition specifies something that must be true after the method call returns.

The End

You might also like