Stepwise Refinement Eric Roberts CS 106A January 8, 2010.

Slides:



Advertisements
Similar presentations
Procedural Recursion Eric Roberts CS 106B April 15, 2009.
Advertisements

COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Software Engineering and Design Principles Chapter 1.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
QUALITY TOOLS Tree Diagram. WHAT IS ? The tree diagram is used to break-up complex problems into smaller subproblems. Helps understand the complexity,
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
CS 201 Functions Debzani Deb.
Robot? What’s a Robot? Introducing Karel-the-Robot.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
5-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Karel J. Robot Chapter 6 Instructions That Repeat.
1 Team Skill 3 Defining the System Part 1: Use Case Modeling Noureddine Abbadeni Al-Ain University of Science and Technology College of Engineering and.
Software Testing CS II: Data Structures & Abstraction
CS 106A, Lecture 3 Problem-solving with Karel
AP CSP: Creating Functions & Top-Down Design
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
to understand recursion you must understand recursion
Algorithms and Problem Solving
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
In the beginning… software
Recursion: The Mirrors
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Introduction To Repetition The for loop
Eric Roberts and Jerry Cain
Copyright © 2008 by Helene G. Kershner
CSE 374 Programming Concepts & Tools
Storage Management.
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
CS 106A, Lecture 2 Programming with Karel
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Static Methods 14-Nov-18.
Problem Solving Techniques
Learning to Program in Python
Written Description of Algorithms
Designing Programs.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
slides courtesy of Eric Roberts
Algorithms IV Top-Down Design.
Algorithms and Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Week 4 Lecture-2 Chapter 6 (Methods).
Nested If Statements While Loops
Programming Fundamentals (750113) Ch1. Problem Solving
slides courtesy of Eric Roberts
CS 144 Advanced C++ Programming January 31 Class Meeting
Basic Concepts of Algorithm
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Computational Thinking
SOFTWARE ENGINEERING LECTURE 4
Functions, Procedures, and Abstraction
Presentation transcript:

Stepwise Refinement Eric Roberts CS 106A January 8, 2010

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

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(); } 3 2 1 2 1 2 3 4 5

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 The proposed steps should be easy to explain. One indication that you have succeeded is being able to find simple names. 1. 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. 2. 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 doesn’t make sense in the context of the problem, it is probably worth defining a new method that calls the old one. 3.

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: + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 + 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 couldn’t it just stop at the end of 1st Street? + 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