### Design Recipe Two - Conditional Programs

#### Prior Knowledge: Program Definition Design Recipe

 DATA ANALYSIS CONTRACT, PURPOSE, HEADER - Name of program, Input / Output NUMBER LINE- For programs which have intervals, map out the numeric situations EXAMPLES - Function calls (for EACH situation AND end points) with estimated answers. BODY Skeleton / Template: For EACH situation there is ONE COND-CLAUSE Cond-Clause QUESTIONS: Conditions - Boolean expression that characterizes each situation Cond-Clause ANSWERS: ie. - FORMULA to calculate the answer for EACH situation SIMPLIFY conditional expressions when coding, since each cond-line is evaluated sequentially. TEST - Execute the program. Compare the computer answerS to your estimateS. Check logic. See the generic program ADDIT, which will solve an interval problem like shown in the above number line. All that changes are the constant values.

#### SALARY with CONDITIONSGross Wages - Taxes = Net Pay

DATA ANALYSIS
;;wage: number number -> number
;;compute weekly wages with time-and-half paid for overtime
(define (wage hours hrRate) EXAMPLES
(wage 20 10) = 200
(wage 40 10) = 400

(wage 45 10) = (+ 400 (* 5 15)) = 475
BODY
 Template Conditional Expressions Answers ```(define (wage hours hrRate) (cond [ ] [ ] ) ) ``` (<= hours 40) (> hours 40) (* hours hrRate) (+ (* 40 hrRate) ( * (- hours 40) hrRate 1.5 )) )
Interaction Window
```Welcome to DrScheme, version 100.
Language: Beginner.
200
=
200
400
=
400
#i475.0
=
475
```
Definition Window
```;;compute weekly wages with time-and-half paid for overtime
(define (wage hours hrRate)
(cond
((<= hours 40) (* hours hrRate))
((> hours 40) (+ (* 40 hrRate)
(* (- hours 40) (* hrRate 1.5))))))

(wage 20 10) = 200
(wage 40 10) = 400
(wage 45 10) = 475
```
DATA ANALYSIS
;;tax: number -> number
;;compute taxes at 3 different rates: 0-\$100 no tax, next \$400 15%, over \$500 30%
(define (tax wages) EXAMPLES
(tax 50) = 0
(tax 100) = 0
(tax 200) = (* 100 .15) = 15
(tax 500) = (* 400 .15) = 60
(tax 600) = (+ 60 (* 100 .3)) = 90
(tax 885) = (+ 60 (* 385 .3)) = about 175
BODY
 Template ? Conditional Expressions ? Answers ```(define (tax wages) (cond [ ] [ ] [ ] ) ) ``` (<= wages 100) (and (> wages 100) (<= wages 500)) (> wages 500) 0 (* (- wages 100) .15) ```(+ (* 400 .15) (* (- wages 500) .30)) ```
Interaction Window
```#i15.0
=
15
#i60.0
=
#i60.0
=
60
#i90.0
=
#i90.0
=
90
#i175.5
=
#i175.5
=
180
```
Definition Window
```;;tax:  number -> number
;;compute taxes at 3 different rates:
;;   0-\$100 no tax, next \$400 15%, over \$500 30%
(define (tax wages)
(cond
((<= wages 100) 0)
((<= wages 500)(* (- wages 100) .15))
((> wages 500) (+ (* 400 .15)
(* (- wages 500) .30)))))

(tax 50)  = 0
(tax 100) = 0
(tax 200) = 15
(tax 500) = (* (- 500 100) .15) = 60
(tax 600) = (+ 60 (* 100 .3)) = 90
(tax 885) = (+ 60 (* 385 .3)) = 180
```

The after-taxed wages program is exactly like the one in Chapter 2 on program definitions since the name of the functions did not change. The only thing which changed is:

• The way the wages are calculated, now with overtime.
• And the way the tax is calculated based on the income.
See the previous example on program definitions for help.

For another example on programs with intervals see SPENDING YOUR MONEY.