; CS101, Spring 2013 ; HW 2 (display "\n CS101 Assignment 2, Spring 2013") (display "\n PLEASE WRITE YOUR NAME HERE\n\n") ; Before starting to write functions, you should read completely through ; every problem statement. There are three parts to problem 1 and one ; part for problem 2. (display "Problem 1(a): Writing a function to convert days to hours\n") ; ; Define a Racket procedure that consumes a single input representing ; a number of days, and returns as its output the corresponding number ; of hours. Call the function days->hours. ; ; Examples of running the days->hours function are shown below: ; ; (days->hours 1) ==> 24 ; (days->hours 2) ==> 48 ; (days->hours 4) ==> 96 ; ; Use the constant HOURS-PER-DAY (defined below) in your function ; instead of the literal value 24. ; (newline) ;Constant definition: (define HOURS-PER-DAY 24) ;Contract: ;Header: ;Purpose: ;; Pre-function tests: ;; Function definition: ;; Post-function printf's (uncomment when ready): ;(printf "(days->hours 1) => ~a~%~%" (days->hours 1)) ;(printf "(days->hours 2) => ~a~%~%" (days->hours 2)) ;(printf "(days->hours 4) => ~a~%~%" (days->hours 4)) (newline) (newline) (display "Problem 1(b): Defining a function to convert hours to minutes\n") ; ; Use a define special form to name a similar procedure, ; called hours->minutes, that takes as input a number of hours and ; returns as output the corresponding number of minutes. ; ; Examples of running the hours->minutes function are shown below: ; ; (hours->minutes 1) ==> 60 ; (hours->minutes 1.5) ==> 90 ; (hours->minutes 4) ==> 240 ; ; To avoid using literal values in your procedure, define a ; constant for the number of minutes in an hour. ; (newline) ;; CONSTANT DEFINITION: ;Contract: ;Header: ;Purpose: ;; Pre-function tests: ;; Function definition: ;; Post-function printf's (uncomment when ready): ;(printf "(hours->minutes 1) => ~a~%" (hours->minutes 1)) ;(printf "(hours->minutes 1.5) => ~a~%" (hours->minutes 1.5)) ;(printf "(hours->minutes 4) => ~a~%" (hours->minutes 4)) (newline) (newline) (display "Problem 1(c): Defining a composite function\n") ; ; Finally, use a define special form to name a similar procedure, ; called days->minutes, that takes as its input a number of days ; and returns as output the corresponding number of minutes. The ; body of the lambda expression in the days->minutes function should ; COMPOSE the two functions days->hours and hours->minutes analogous ; to the way that the mathematical expression f(g(x)) composes the ; functions f and g. In other words, days->minutes should use ; days->hours and hours->minutes as helper functions. ; ; Examples of running the days->minutes function are shown below: ; ; (days->minutes 1) ==> 1440 ; (days->minutes 0.1) ==> 144.0 ; (days->minutes 0) ==> 0 ; (newline) ;Contract: ;Header: ;Purpose: ;; Pre-function tests: ;; Function definition: ;Post-function printf's (uncomment when ready): ;(printf "(days->minutes 1) => ~a~%" (days->minutes 1)) ;(printf "(days->minutes 0.1) => ~a~%" (days->minutes 0.1)) ;(printf "(days->minutes 0) => ~a~%" (days->minutes 0)) ;(printf "(days->minutes 5) => ~a~%" (days->minutes 5)) (newline) (newline) (display "Problem 2: Define a panic thermometer\n") ; ; Using a primitive function called TEXT to express urgency. ; ; To read about how to use the text function, go to the help menu and the ; help desk, type in the function name text at the top of the page, and ; read the function contract. ; ; Take note that the first argument of the text function is a string, but ; you are given a number to output. The function to convert a number to ; a string is called number->string. To make sure a number like 98.6 is ; produced in decimal form (not as a fraction), use the function ; exact->inexact. ; ; ----------------------------------- ; ; Develop a function called panic-thermometer that takes in an integer ; (in degrees Fahrenheit) and produces an image of the temperature as ; text, colored either blue (below 99 degrees F), gold (at least 99 ; but less than 101 degrees F), orange (at least 101 but less than 104 ; degrees F), or red (104 degrees F or higher). ; ; Besides the coloring of the image, the function should also make the ; size of the output image larger if the temperature is higher. For text ; shown in blue, the size of the text should be 24 point. For gold text, ; the size of the text should be 28 point. For orange text, the size of ; the text should be 32 point. For red text, the size should be 48 point. ; ; You should define sufficient constants so that you use no literal ; values (numbers or strings) inside the procedure you write. This will ; mean you should write 11 constant definitions. To be specific, you ; should have: ; 3 constant names for body temperature in degrees F ; 4 constant names for color strings ; 4 constant names for font size ; ; It may help you to write the function first using literal values in ; the code 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 homework assignment. ; (newline) ;; Constant definitions: ;Contract: ;Header: ;Purpose: To express a person's body temperature with an image that is in a ; larger font and closer to the red end of the visual spectrum as the ; temperature gets higher. ; ;Pre-function tests: (be sure to test all branches of the function) ;To write the function tests, compare the result of calling the function on ;the call to the text function that is run for a particular temperature. ;Function definition: ;Post-function printf's: (uncomment when ready) ;(printf "(panic-thermometer 98.6) => ~a~%" (panic-thermometer 98.6)) ;(printf "(panic-thermometer 100) => ~a~%" (panic-thermometer 100)) ;(printf "(panic-thermometer 102) => ~a~%" (panic-thermometer 102)) ;(printf "(panic-thermometer 104) => ~a~%" (panic-thermometer 104))