1. Images

This lab will use DrRacket’s image library.

In order to create and manipulate images in DrRacket we need to make the image library available to our code. Add the following line to your definitions window

(require 2htdp/image)

The image library allows us to create all sorts of shapes. Have a look at the library’s documentation for more details.

2. Design Recipe

Practise Exercises
  • 1) You are asked to design a function that given a dollar amount will return the equivalent Euro amount. The exhcange rate is is $1 = 0.9€.

  • 2) Design a second function that will take as input the dollar amount and the exchange rate for another currency and will return the equivalent amount for that other currency.

  • 3) You are helping out a friend to create a program that displays fluctuations of stock prices. Design a program that consumes the previous and current stock price for a given stock. Your program should return the difference between the previous and current stock price as a text image.

    1. If the difference is negative, your program should display the difference in the color red (without displaying the negative sign),

    2. If the difference is positive, your program should display the difference in the color green (without displaying the positive sign)

    3. If there is no difference, i.e., the difference is 0, your program should display the difference in the color yellow.

  • 4) You are asked to design a function that will calculate the distance between 2 Cartesian Coordinates. Your function will consume 4 inputs such that

    1. The first input is the x coordinate of the first point (\(x_1\) in your distance formula)

    2. The second input is the y coordinate of the first point (\(y_1\) in your distance formula)

    3. The third input is the x coordinate of the second point (\(x_2\) in your distance formula)

    4. The fourth input is the y coordinate of the second point (\(y_2\) in your distance formula)

2.1. Creating Images (again) this time using the Design Recipe

Practise Exercises
  • 5) A friend is prototyping a small game. He asking you for help with the image generation. They would like you to develop a function that given one of three symbols draws the corresponding image, for example,

    1. Given 'circle it will generate a red circle with radius 100.

    2. Given 'square it will generate a blue square with side 50.

    3. Given 'star it will generate a green star with side 50. You should read the documentation on the function symbol=?

  • 6) Use check-expect to write tests for your new function.

3. Itemizations

We are trying to develop a program to keep track of all equipment for a store that buys and sells computer accessories.

A popular category of accessories in the store is media (CDs, DVDs, etc.), and gaming peripherals (keyboards, mice, etc.).

Here are two data definitions for media and peripherals

;; A Media is one of
;; - 'dvd
;; - 'cd
;; - 'blueray
;; - 'floppy

;; INTERP: represents each kind of media kept at the store


;; A Peripheral is one of
;; - "keyboard"
;; - "mouse"
;; - "driving wheel"
;; - "headset"
;; - "controller"

;; INTERP: represents each kind of peripheral kept at the store
Practise Exercises
  • 7) Write deconstructor templates for Media and Peripheral.

  • 8) Design a function called media? that given any Symbol return’s true if the value given is one of the Media values, and false otherwise.

  • 9) Design a function called peripheral? that given any String return’s true if the value given is one of the Peripheral values, and false otherwise.

  • 10) Design a function that given a Media item and its price returns the amount of sales tax.

    1. Dvd’s have 5% sales tax.

    2. CDs have 3% sales tax.

    3. Blue Ray’s have 7% sales tax.

    4. Floppies have 2% sales tax.

  • 11) Design a function that given a Peripheral item and its price returns the amount of sales tax.

    1. Keyboards have 2.5%.

    2. Mice have 1.5%.

    3. Driving Wheels have 3.5%.

    4. Headsets have 5.2%.

    5. Controllers have 6%.

  • 12) Design a new data definition called MediaOrPeripheral that can be one of Media or Peripheral.

  • 13) Design a new function that given a MediaOrPeripheral returns the amount of sales tax.