; CS101, Spring 2013 ; Lab 3 ; Using decision statements to solve a problem. (display "\n CS101 Lab 3, Spring 2013\n") (display "\n SOLUTIONS\n\n") (newline) (newline) ; READ THE DIRECTIONS FOR EACH PROBLEM CAREFULLY BEFORE STARTING TO WRITE ; YOUR SOLUTIONS. (display "Problem 1: Twentieth-Century Postage Charges.\n\n") ; ; The Post Office charges the following postage for a first-class letter, ; depending upon its weight: ; o less than 30 grams, 15 cents, ; o 30 to 49 grams, 17 cents, ; o 50 to 99 grams, 22 cents, ; o 100 grams and over, 1 cent for each 4 grams. In this case, ; round the value obtained by dividing grams by 4 to the nearest ; integer (use the function round). ; ; Write a procedure that consumes the weight in grams and returns the ; postage in cents. Follow the design recipe, including contract, header, ; purpose, pre-function tests, function definition, and post-function ; printfs for results. Be sure you test every possible branch of execu- ; tion. ; ; Note: This function will involve making decisions. When more than 2 ; different conditions may occur, the best special form to use is a cond. ; ; Define seven constants in the global environment for the values 30, ; 50, 100, 15, 17, 22, and 4. Name these constants appropriately and ; use the names in your procedure definition. ; ; It may help you to write the function first using literal values in ; the function body and then replace the literal values with the constant ; names after you know where the constant names should be placed in the ; function. Be sure to type the constant names in the function and in ; the check-expect statements before you submit this lab. ; ;Constant values used in procedure: (define LOWER-CUTOFF 30) (define LOWEST-RATE 15) (define MID-CUTOFF 50) (define MID-RATE 17) (define MID->HIGH-CUTOFF 100) (define MID->HIGH-RATE 22) (define INCREMENT 4) ;Contract: (postage number) -> number ;Header: (define postage (lambda (grams) ...)) ;Purpose: Calculate the postage charges as given in the above comment box. ;Pre-function tests: (check-expect (postage 29) 15) (check-expect (postage 30) 17) (check-expect (postage 50) 22) (check-expect (postage 100) 25) (check-expect (postage 101) 25) (check-expect (postage 102) 26) (check-expect (postage 104) 26) ;Function definition: (define postage (lambda (grams) (cond ; letter weighs less than 30 grams [(< grams LOWER-CUTOFF) LOWEST-RATE] ; letter weighs between 30 and 49 grams [(< grams MID-CUTOFF) MID-RATE] ; letter weighs between 50 and 99 grams [(< grams MID->HIGH-CUTOFF) MID->HIGH-RATE] ; letter weighs 100 grams or over [else (round (/ grams INCREMENT))]))) ;Post-function printf's: (uncomment when ready) (printf "(postage 29) => ~a~%" (postage 29)) (printf "(postage 30) => ~a~%" (postage 30)) (printf "(postage 50) => ~a~%" (postage 50)) (printf "(postage 100) => ~a~%" (postage 100)) (printf "(postage 104) => ~a~%" (postage 104)) (newline) (newline) (display "Problem 2: A function to calculate speeding fines\n\n") ; ; The government has set new fines for speeding, as follows: ; - If the motorist was observed traveling over the speed limit: ; * If the motorist's speed was less than 40% above the speed ; limit, the basic fine is $50. ; * If the motorist was traveling 40% above the speed limit or ; more, the basic fine is $100. ; - Otherwise, the fine is 0. ; ; The actual fine is computed as the square of the number of speeding ; tickets the motorist has received in the last three years multiplied ; by the basic fine for their speed. Thus, a motorist traveling 70 km/hr ; in a 50 km/hr zone, with 3 prior speeding tickets in the last three ; years, will pay $100 * 3^2 = $900. (The hope is that motorists will ; stop speeding or, if not, the government's financial problems will ; be solved forever!) ; ; Part I: Define 3 constants in the global environment- ; i) the lower basic fine ($50), ; ii) the upper basic fine ($100), and ; iii) the cut-off for percentage above the limit (0.40). ; ; Part II: Define the function using the design recipe, including ; contract, header, purpose, pre-function tests, function ; definition, and post-function printfs. ; ; The function should consume three inputs: ; 1) The speed of the motorist as detected by the highway ; patrol, ; 2) the speed limit for the zone the motorist was pulled ; over in (in km/hr), and ; 3) the number of speeding tickets the motorist has ; received over the past three years. ; ; Be careful to make your function give the correct fine to those lucky ; motorists who have received 0 speeding tickets in the last 3 years. ; ; It may help you to write the function first using literal values in ; the function and to then replace the literal values with the constant ; names after you know where the constant names should be placed in the ; function. Be sure to type the constant names in the function and in ; the check-expect statements before you submit this lab. ; ;Constants representing the lower and upper basic fine amounts: (define LOWER-FINE 50) (define UPPER-FINE 100) (define PERCENTAGE-OVER 0.4) ;Contract: (calc-fine number number number) -> number ;Header: (define calc-fine ; (lambda (motorist-speed speed-limit num-tickets) ... )) ;Purpose: To calculate the speeding fine for motorist exceeding the ; speed limit based on clocked speed and number of past tickets. ;Pre-function tests: (check-expect (calc-fine 70 50 3) 900) (check-expect (calc-fine 75 55 2) 200) (check-expect (calc-fine 95 55 0) 100) (check-expect (calc-fine 95 55 1) 100) (check-expect (calc-fine 95 55 3) 900) (check-expect (calc-fine 76 55 0) 50) (check-expect (calc-fine 76 55 2) 200) (check-expect (calc-fine 55 55 2) 0) ;Function definition: (define calc-fine (lambda (motorist-speed speed-limit num-tickets) ;; was motorist speeding? (if (> motorist-speed speed-limit) ;; if so, calculate fine based on amount over the speed limit the ;; motorist was traveling plus the square of the number of tickets ;; they received in the last 3 years. (if (< motorist-speed (+ speed-limit (* speed-limit PERCENTAGE-OVER))) (* LOWER-FINE (if (= num-tickets 0) 1 (sqr num-tickets))) (* UPPER-FINE (if (= num-tickets 0) 1 (sqr num-tickets)))) ;; if the motorist was not speeding, fine is 0 0))) ;Post-function tests: (printf "(calc-fine 70 50 3) => $~a~%" (calc-fine 70 50 3)) (printf "(calc-fine 75 55 2) => $~a~%" (calc-fine 75 55 2)) (printf "(calc-fine 95 55 0) => $~a~%" (calc-fine 95 55 0)) (printf "(calc-fine 95 55 1) => $~a~%" (calc-fine 95 55 1)) (printf "(calc-fine 95 55 3) => $~a~%" (calc-fine 95 55 3)) (printf "(calc-fine 76 55 0) => $~a~%" (calc-fine 76 55 0)) (printf "(calc-fine 76 55 2) => $~a~%" (calc-fine 76 55 2)) (printf "(calc-fine 55 55 3) => $~a~%" (calc-fine 55 55 3)) (newline) (newline)