Chapter 9: Building Bigger Programs. Chapter Objectives.

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Advertisements

Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
Basics of Computer Programming Web Design Section 8-1.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Chapter 2: Algorithm Discovery and Design
Unit 191 Introduction to Software Engineering The objective of this section is to introduce the subject of software engineering. When you have read this.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Sentinel Logic Assumes while loops and input statements.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 9: Building Bigger Programs.
Hello AP Computer Science!. What are some of the things that you have used computers for?
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CS1315: Introduction to Media Computation Making sense of functions.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
CIT 590 Intro to Programming Lecture 3. Pair programming logistics You and your partner submit 1 assignment Figure out who the submitter will be PLEASE.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Program Design and Debugging. How do programmers start? How do you get started with a program? “Programming is all about debugging a blank piece of paper.”
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Chapter 1 Software Engineering Principles. Problem analysis Requirements elicitation Software specification High- and low-level design Implementation.
Chapter 10: Building Bigger Programs. Chapter Objectives.
Welcome to Software Engineering. Lecture 1 Metaphysics of Software Engineering Alex
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Getting Started With Python Brendan Routledge
CS1315: Introduction to Media Computation Making sense of functions.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
Chapter 10: Building Bigger Programs
While Loops Chapter 3.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Week 8 - Programming II Today – more features: Loop control
Call Stacks, Arguments and Returns
Procedures Programming Guides.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Coding Concepts (Basics)
Introduction to TouchDevelop
CHAPTER 6: Control Flow Tools (for and while loops)
CS 177 Week 9 Recitation Slides
Chapter 9: Building Bigger Programs
Presentation transcript:

Chapter 9: Building Bigger Programs

Chapter Objectives

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.

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.

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.

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.

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.

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.

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 ()

pickRoom() def pickRoom(direction, room ): if (direction == "quit") or (direction == "exit"): printNow("Goodbye!") return "Exit" if direction == "help": showIntroduction () return room if room == "Porch": if direction == "north": return "Entryway" if room == "Entryway": if direction == "north": return "Kitchen" if direction == "east": return "LivingRoom" if direction == "south": return "Porch" return "LivingRoom"

Rest of pickRoom() if room == "Kitchen": if direction == "east": return "DiningRoom" if direction == "south": return "Entryway" if room == "LivingRoom": if direction == "west": return "Entryway" if direction == "north": return "DiningRoom" if room == "DiningRoom": if direction == "west": return "Kitchen" if direction == "south":

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.")

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.

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

Returning a reasonable response to unreasonable pickRoom() input def pickRoom(direction, room ): if (direction == "quit") or (direction == "exit"): printNow("Goodbye!") return "Exit" if direction == "help": showIntroduction () return room … if room == "DiningRoom": if direction == "west": return "Kitchen" if direction == "south": return "LivingRoom" printNow("You can’t (or don’t want to) go in that direction.") return room #Stay in current room

Now we handle unexpected input better >>> pickRoom(’north ’,’Porch ’) You can’t (or don’t want to) go in that direction. ’Porch ’ >>> pickRoom(’Entryway ’,’Porch ’) You can’t (or don’t want to) go in that direction. ’Porch ’

Tips on Debugging Learn to trace code Print statements are your friends Don’t be afraid to change the program Use comments to “remove” parts temporarily when testing.

Seeing the Variables: showVars()

Stepping through makeSunset() with the Watcher

Improving the Adventure Game When testing, we discover: It’s hard to tell which room was which when playing the game. We can’t figure out what we typed where.

Improving showRoom() def showRoom(room ): printNow("===========") if room == "Porch": showPorch () if room == "Entryway": showEntryway () if room == "Kitchen": showKitchen () if room == "LivingRoom": showLR () if room == "DiningRoom": showDR ()

Improving playGame() def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") printNow("You typed: "+direction) location = pickRoom(direction, location)

Better game play

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!