Top Banner
Chapter 9 Simulation and top-down design
16

Chapter 9 Simulation and top-down design. Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Chapter 9

Simulation and

top-down design

Page 2: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Simply put, simulation means using computing processes to simulate real-world situations to obtain information that would be difficult to get or would be obtainable only over a long period of time.

Page 3: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Because we want simulations to reflect the unpredictability of much of what happens in the world, the simulations should include “chance” probabilities.

In programming language terms, we want random generation functionality in our simulation.

Page 4: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

A seed value is given to a function which produces a return value; that value in turn is passed back to the same function so that a different return value is generated.

Python supports a module called random which does just this. The seed value is the date and time the function was launched, so the pseudorandom numbers produced are always unique.

Always import the library: import random

Page 5: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

The randrange() function creates a random integer between any two values given as parameters. randrange(1, 10) will return a random integer between 1 and 9.

A third parameter can give a multiple of a value. For example, randrange(1, 100, 2) will produce a pseudorandom integer between 1 and 99 which is a multiple of 2—that is, an even number.

Page 6: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

random() takes no parameters. It produces a float number between zero and up to but not including 1.0.

When importing the module, you may want to take the shortcut of specifying which function you want to use:from random import randomThis prevents the awkward code random.random()

Page 7: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Pseudocode According to the Dictionary of Computer

and Internet Terms, pseudocode is “an outline of a computer program, written in a mixture of a programming language and English. Writing pseudocode is one of the best ways to plan a computer program.” As we discuss top-down design in the ensuing slides, we will actually be working in a kind of pseudocode.

Page 8: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Top-down design simply means you start with the big problem and break it into smaller tasks. It’s a design strategy that complements the programming strategy of modularization well.

Your main() module will contain all the subroutines or submodules or subtasks that comprise the whole problem. Hence the main() module is the top level.

Page 9: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Separation of Concerns This again is closely connected to

modularization. The main() module only cares about what it has to provide to functions and what is returned from functions. At this stage, all we need to know are the parameters for the functions main() will invoke and what values main() will get back in return.

The name, parameters and return values of functions invoked by main() are the interface or signature of the functions.

Page 10: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Structure or module hierarchy charts We can illustrate these features in a

structure or hierarchy chart. Parameters, function names, and return values are listed—but no more detail than that.

main()

Get_input() Process_input() Display_outcome()

Page 11: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Abstraction The top-level design in the preceding slide

could be applied to almost any program; in fact, it works well as a very general outline of our final program (where we ask users to select items to buy, calculate the bill, and print an invoice). At this level, the details are not important; we’re just establishing the overall structure of the program.

The design principle of ignoring details and concentrating on important features at different levels is called abstraction.

Page 12: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Second- and third-level design As we work through our pseudo-code, we

will repeat the same process for each function which main() calls. As we break each subfunction down into its inherent tasks, or subsubfunctions, we are detailing second- and third-level designs.

Page 13: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Bottom-up implementation When we start actually coding in Python, it

is best to begin at the bottom of our structure/hierarchy chart. For example, in the very generic structure chart on slide 10, it would make sense to start coding the function get_input(). For our final project, that function might include getting the user’s name, address, email, product selection, quantity, etc. Each of those might be second-level functions. Beginning the coding with these lower functions is called Bottom-up Implementation.

Page 14: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Prototyping Prototyping design means you implement

part of a program, test it with users, then add more features if that prototype passes muster.

For our final project, you might prototype a GUI which just gets user contact info and prints it. The next prototype might be getting the product selection and calculating a total.

In a sense, earlier projects in this class are prototypes of the final assignment.

Page 15: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Spiral development When we code, test the prototype, add new

features in code, test the prototype again, etc., we are using spiral development. Each step in the process depends on the successful completion of the preceding type. Spiral development does several things:>>it modularizes the development process>>it ensures continuous user/client input>>it controls error by taking a step-by-step approach

Page 16: Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.

Workshop Look at Exercise # 12 on p. 293. In groups

of three, write a three-level structure chart that outlines what the code would look like to solve the problem posed in this question.