RU beehive logo ITEC dept promo banner
ITEC 380
2013fall
ibarland
aaray

homelecturesexamshwsD2Lbreeze (snow day)

hw02
handling simple and range data

Due Sep.13 (Fri)15 (Sun) 23:59 (both hardcopy, and on Desire2Learn).
Your name and the assignment-number must be in a comment at the start of the file. Your hardcopy must be stapled.

Use the Beginning Student language1 All pertinent steps of the design-recipe must be evident; in particular test cases that precede the function are worth a signification portion of the points.

Rather than repeating code, you should have helper functions as appropriate.

  1. (10pts) Write the three functions f2c (convert temperatures from °F to °C), c2k (convert °C to K), and f2k (convert °F to K). Include at least 2 test cases for each function.
    For any test cases involving inexact numbers, use check-within with a third argument (tolerance) of 0.000001 or so. (E.g. (check-within (sqrt 2) 1.414 0.001))

    TODO for prof:Ian: the following paragraphs are more about c2f, not f2c!
    thermometer illustration

    In some odd countries (namely Canada, and most of the rest of the world), temperatures are measured in degrees Celcius, instead of degrees Fahrenheit. So when you are traveling and you look in the newspaper and see a forecast for 25°C, it might sound chilly until a friendly Canadian citizen tells you that this is a balmy 77°F. Other notable temperatures2 include freezing (0°C, which is 32°F), and boiling (100°C, which is 212°F). (However: if you really want to internalize metric, you're better off thinking in terms of new reference points than converting.) The general case for converting degrees-Celcius into degrees-Fahrenheit is given by the arithmetic formula

    F(C) = 95 · C + 32

    You read in a science article that the surface of the sun is 5700K (“kelvins”3), and that absolute zero is (conveniently) 0 Kelvin. These numbers seem even more baffling, until that same cheerful Canadian tells you that to convert a temperature from Kelvins to °C, you simply subtract 273.15.
    So 0 K (absolute zero) is -273.15°C, which in turn is -459.67°F, brr.
    Similarly, 273.15K (freezing) is 0°C which (as we've seen earlier) is 32°F.

    (Hint: If somebody gives you a temperature in Kelvins, converting that temperature into degrees Celsius is pretty easy. How can you then leverage code from the preceding problem, to convert that many degrees Celsius amount into degrees Fahrenheit?)

    Futurama: Fry and Leela chat with Moon farmer From Futurama The Series Has Landed:

    Fry: Ooh, nighttime on the moon!
    Old coot: It git down to minus one hundr'd, sev'nty-three.
    Fry, worriedly: Fahrenheit, or Celsius?
    Old coot: First one, then th' other.

  2. (6pts) Write the function has-suffix?, which determines if one string ends in another.
    Hint: you can use a conditional if you want, but you can also write it using and and no conditional.
  3. (6pts) Write the function pluralize, which passes the following tests:
    (check-expect (pluralize 3 "t-shirt") "3 t-shirts")
    (check-expect (pluralize 1 "fish") "1 fish")
    (check-expect (pluralize 2 "fish") "1 fishes")
    (check-expect (pluralize 2 "cactus") "2 cacti")
        
    You need only specially-handle words ending in “sh” and “us”; to all others you can just form the plural by adding an "s".
    (You definitely need at least one or two more tests. What important input(s) are entirely untested?)

Images in racket

To experiment with functions-which-create-and-handle-images in DrRacket, include (require 2htdp/image) near the top of your file. Then you should be be able to evaluate each of the following:

(rectangle 80 20 'solid 'blue)
(circle 20 'solid 'red)
(ellipse 80 20 'outline 'orange)

(beside (rectangle 80 20 'outline 'blue)
        (circle 20 'solid 'red))
(underlay (rectangle 80 20 'outline 'blue)
         (circle 20 'solid 'red))
; Think of 'underlay' and 'beside' as being "addition for images"

; underlay with an offset:
(underlay/offset (rectangle 80 20 'outline 'blue)
                0
                10
                (circle 20 'solid 'red))


; If you want to explore documentation:
;   In DrRacket, position the caret on a function-name
;   like 'underlay', and hit F1.

Test cases for images

For functions that return an image, your expected-value should either be (a) an expression involving calls to: the image primitives and/or previously-tested functions, OR (b) an image-literal (but, show the expressions you used to create that image-literal).

For example, if the problem were to "create a function that takes in a width, and returns a solid purple rectangle with the given width and a 16:9 aspect ratio", then the "expected result" part of the test cases might involve calling the image-library's functions like rectangle:

(check-expect (screen 32) (rectangle 32 18 'solid 'purple))
(check-expect (screen 16) (rectangle 16 9 'solid 'purple))
(check-expect (screen 0) (rectangle 0 0 'solid 'purple))
  ; Note: for the expected 0x0 rectangle, I could equally well write:
(check-expect (screen 0) empty-image)
Or, since DrRacket supports image literals (!), I could copy/paste images as the second argument to check-expects: a sample check-expect, with an image-literal as the expected result


    1. example results of calling donut, eyes
    2. (3pts) Write a function which takes in a size, and returns an image of a brown donut. (Mmmm, doughnut.)
    3. (3pts) Write a function eyes which takes a size and a color, and returns two colored rings side-by-side.
    4. (4pts) As needed, refactor the above functions so that there is little-to-no repeated code. (If the underlying helper function has a solid set of tests, then fewer -- perhaps even just one -- test cases can suffice for the function.)

  1. (3pts) Write a function chomper which takes in a number in [0,1], an returns a pac-man-esque figure whose mouth is the open the indicated fraction. That is, (chomper 0.9) would return an image of a pac-man-esque figure whose mouth is nearly as wide-open as it will ever be.

    Alas, the library 2htdp/image doesn't contain any function for drawing arcs of circles/discs. If you would like such a function, download the file arc.rkt and save it to4 the same directory as your program. Then, in your program, include the line (require "arc.rkt")5. This will allow you to call ellipse/arc, which is similar to ellipse but with a start/stop angle: example of calling (ellipse/arc 200 100 30 180 'solid 'lavendar) (Alternately, a more crude way of making a mouth is by overlaying a triangle onto a circle; this has the drawback that it obscures anything drawn behind the "open" mouth.)

  2. (5pts) Familiarize yourself with the Pac-Man arcade game, such as at kongregate.com. Focusing just on the main character (not the rest of the board/game):
    What fields does a pac-man structure (object) have? For each, give
    1. a descriptive one-word field name,
    2. the type you'd use to represent it,
    3. and
    4. a short explanation/comment (one sentence or less).
    (I expect most answers will have 4 to 7 fields.)

    Hints:

See also scheme-resources—ITEC380 Racket resources.


1 choose Beginnning Student language by: DrRacket: Language > Choose Language... > Teaching Languages > How to Design Programs: Beginning Student.      

2 One other intriguing test case is that -40°C = -40°F. Is it some mystical coincidence, that two scales exactly meet at such a nice round number, instead of some random number with a bajillion decimal places?

Well, it is a small bit of luck that the answer is an integer, but every integer celcius temperature corresponds to an even fifth-of-a-fahrenheit temperature, because of the 9/5 in the formula. Where does 9/5 come from?

The factor of 9/5 stems from the difference between freezing and boiling in the two systems: (212-32)/(100-0) = 180/100 = 9/5. Both scientists Fahrenheit and Celsius chose integers for the freezing and boiling of water, so that there'd be an integer number of notches on their thermometer between the two. This is the ultimate reason why conversions between the two systems use rational numbers, and not some long irrational real number arising randomly from nature.

If some martians came to Earth and (fascinated by all the ambient water) decided that they would invent a new temperature scale where the freezing point of water was declared as -27°Martian and the boiling point as 123°Martian, we'd still end up with a conversions which used fractions, not irrationals.

     

3Interestingly, you never say “degrees” with the Kelvin scale; the unit of temperature has just the one-word name “kelvins” rather than a two-word name “degrees fahrenheit”.      

4 Do not copy/paste the contents into an already-open DrRacket window: The reason is that the already-open window will be set to beginner-student language, but the file isn't written in beginner-student. If you did this, you'll get an error about “read: #lang not enabled in the current context”.      

5 One person reported doing this, but getting an error about `not a module`. If this happens to you, try downloading arc2.rkt, and (require "arc2").      

homelecturesexamshwsD2Lbreeze (snow day)


©2013, Ian Barland, Radford University
Last modified 2013.Oct.04 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme