; CS101, Spring 2013 ; Lab 3 ; Using decision statements to solve problems. (display "\n CS101 Lab 3, Spring 2013\n") (display "\n PLEASE WRITE YOUR NAME HERE\n\n") (newline) (newline) ; READ THE DIRECTIONS FOR EACH PROBLEM CAREFULLY BEFORE STARTING TO WRITE ; YOUR SOLUTIONS. (display "Problem 1: Mid-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 ; closest 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 (given--just uncomment). Be sure you test every ; possible branch of execution. ; ; 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 body. 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: ;Contract: ;Header: ;Purpose: ;Pre-function tests: ;Function definition: ;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% or more above the speed ; limit, 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 between the lower and upper fines for ; percentage above the speed 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. Call the function ; calc-fine. ; ; 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 ; 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 and the ;relevant percentage over the limit: ;Contract: ;Header: ;Purpose: ;Pre-function tests: ;Function definition: ;Post-function tests: (uncomment when ready) ;(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)