Designing Programs - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Designing Programs

Description:

Imagine the owner of a movie theater who has complete freedom to set ticket prices. ... movie theater based on ticket prices. Program Examples. Do this before ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 18
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Designing Programs


1
Designing Programs
2
Outline
  • Prerequisites
  • Basic Scheme Syntax
  • Objectives
  • Basic recipe for program design
  • Purpose
  • Examples
  • Body
  • Tests
  • Reference
  • HTDP Section 2.5

3
Background
  • The blank screen problem
  • You have started Dr Scheme, and the Definitions
    window is blank
  • You know you have to put something there but have
    no idea what
  • Staring at it doesnt help
  • Neither does staring at the problem
  • The solution a recipe for proceeding
  • Purpose
  • Examples
  • Body
  • Test

4
Questions to Answer
  • Problem statement
  • What is relevant?
  • What can I safely ignore?
  • The Program
  • What does it consume? its inputs
  • What does it produce? its output
  • How are the inputs and outputs related? its
    body
  • Resources
  • Does Scheme provide the functions I need?
  • If not, must I develop those functions myself?
  • Testing
  • How will I know this thing is doing what I want?

5
Problem Statement
  • Imagine the owner of a movie theater who has
    complete freedom to set ticket prices. The more
    he charges, the fewer the people who can afford
    tickets. In a recent experiment, he determined a
    precise relationship between the price and
    attendance. At 5 a ticket, 120 people came.
    Decreasing the price by 0.10 increased
    attendance by 15. However, increasing attendance
    increased his costs. Every performance costs him
    180, and each attendee costs him 0.04. The
    owner wants to know exactly how profit relates to
    ticket price so that he can determine the price
    at which he makes the most profit.

6
Programs Purpose
  • Begin every program with a CONTRACT which states
    its purpose, first logically
  • profit number -gt number
  • Generate a program header
  • (define (profit ticket-price) )
  • Write a brief explanatory statement
  • to estimate the impact on profit from a
  • movie theater based on ticket prices

7
Program Examples
  • Do this before you write the body
  • Predict the behavior of the program
  • Think about the computations involved
  • Illustrate the purpose statement with examples
  • to estimate the impact on profit from a
  • movie theater based on ticket prices
  • attendance is 120 at 5 a ticket
  • attendance is 135 at 4.90
  • cost is 180 .04 per attendee
  • (profit 5.00) -gt
  • 120 5 (180 120 .04) 415.20

8
Write the Body
  • Replace the ellipses () in the header with the
    code necessary to implement the computation
  • Easy if you have the formulae provided
  • More complex if you are given a word problem
  • Make liberal use of other functions to express
    the solution
  • Gain experience with the functions Scheme
    provides
  • Use Dr Scheme help screens
  • (define (profit ticket-price)
  • (- (revenue ticket-price)
  • (cost ticket-price)))

9
Design the Tests
  • As a minimum, ensure that the examples work
  • Cannot show correct behavior for all inputs
  • Look for difficult logical situations
  • Mathematical (could I divide by 0?)
  • Logical (test all paths more later)
  • Large numbers
  • Small numbers

10
Continue the Process
  • If you defined other functions you need to write,
    repeat the same process for each
  • Purpose
  • Examples
  • Body
  • Testing

11
(No Transcript)
12
Finished Code
  • profit number -gt number
  • to estimate the impact on profit from a
  • movie theater based on ticket prices
  • attendance is 120 at 5 a ticket
  • attendance is 135 at 4.90
  • cost is 180 .04 per attendee
  • (profit 5.00) -gt
  • 120 5 (180 120 .04) 415.20
  • (define (profit ticket-price)
  • (- (revenue ticket-price)
  • (cost ticket-price)))
  • revenue number -gt number
  • to estimate the revenue for a
  • movie theater based on ticket prices
  • attendance is 120 at 5 a ticket
  • attendance is 135 at 4.90
  • (revenue 5.00) 120 5.00 600
  • (define (revenue ticket-price)

13
Finished Code (continued)
  • attendance number -gt number
  • to estimate the attendance at a
  • movie theater based on ticket prices
  • attendance is 120 at 5 a ticket
  • attendance is 135 at 4.90
  • (attendance 5.00) -gt 120
  • (define (attendance ticket-price)
  • ( 120
  • ( (/ 15 .10) (- 5.00 ticket-price))))
  • cost number -gt number
  • to estimate the cost for a
  • movie theater based on ticket prices
  • cost for 100 people is 180 0.04 per customer
  • (cost 5.00) 180 120 0.04 184.80
  • (define (cost ticket-price)
  • ( 180 ( 0.04 (attendance ticket-price))))

14
Tests
  • (attendance 5.00) should give 120
  • (attendance 4.50) should give 195
  • (revenue 5.00) should give 600.00
  • (revenue 4.50) should give 877.50
  • (cost 5.00) should give 184.80
  • (cost 4.50) should give 187.80
  • (profit 5.00) should give 485.20
  • (profit 4.50) should give 689.70

15
Questions?
16
Summary
  • You should now know
  • Basic recipe for program design
  • Purpose
  • Examples
  • Body
  • Tests

17
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com