Similar presentations

3
How to Design Larger Programs Building something larger requires good software engineering. Top-down: Start from requirements, then identify the pieces to write, then write the pices. Bottom-up: Start building pieces you know, test them, combine them, and keep going until you have your program Debugging: Programming is “the art of debugging a blank sheet of paper.” Testing: Because nothing complicated and man-made is flawless. Maintenance: By far, the most expensive part of any program.

4
Top-Down Design Start from a problem statement. What are you trying to do? Refine the problem statement. Use hierarchical decomposition to define subparts. Refine until you know how to write the programs. Use procedural abstraction so that higher-level functions are written in terms of lower-level.

5
Example Top-Down Design: An Adventure Game Top-level function: 1. Tell the user how to play the game. 2. Describe the room. 3. Get the player’s command. 4. Figure out the next room. 5. Return to Step 2, until the user Quits.

6
Two new functions printNow(): Takes a string as input, and prints it on the Command Area immediately. Print waits until the program is done. requestString(): Takes a prompt string as input, accepts a string from the user in a dialog window, then returns the user’s input.

7
An important new loop How do we keep going, indefinitely, until the user says “quit”? A while loop repeats a block until a test becomes false.

8
Writing the top level function def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") location = pickRoom(direction, location) Working directly from our earlier outline. This function makes sense, even without knowing the lower level functions. It is decoupled from the lower-level. 9
Writing the subfunctions def showIntroduction (): printNow("Welcome to the Adventure House!") printNow("In each room, you will be told which directions you can go.") printNow("You can move north, south, east, or west by typing that direction.") printNow("Type help to replay this introduction.") printNow("Type quit or exit to end the program.") def showRoom(room ): if room == "Porch": showPorch () if room == "Entryway": showEntryway () if room == "Kitchen": showKitchen () if room == "LivingRoom": showLR () if room == "DiningRoom": showDR ()

12
Each room (function) describes itself def showPorch(): printNow("You are on the porch of a frightening looking house.") printNow("The windows are broken. It’s a dark and stormy night.") printNow("You can go north into the house. If you dare.") def showEntryway(): printNow("You are in the entry way of the house. There are cobwebs in the corner.") printNow("You feel a sense of dread.") printNow("There is a passageway to the north and another to the east.") printNow("The porch is behind you to the south.")

13
Running our program (so-far) >>> playGame () Welcome to the Adventure House! In each room, you will be told which directions you can go. You can move north, south, east, or west by typing that direction. Type help to replay this introduction. Type quit or exit to end the program. You are on the porch of a frightening looking house.

14
Testing our program Try both expected, and unexpected input. We should return something reasonable in response to unreasonable input.

24
Running programs outside of JES Once you make a larger program, you may want to run it in Jython directly. 1. Import sys 2. Insert the JES sources into your sys.path 3. From media import * That’s it!