CMPE 100 - Week 2 - Lab worksheet

Basics

(30 minutes)

  1. Make sure DrRacket is installed and running on your computer. If you are using your own laptop, install the program from http://racket-lang.org/
  2. Run DrRacket. Choose language: Beginning Student. Then activate teachpacks: image.ss and universe.ss
  3. Try producing the following symbols with your keyboard, make sure you understand "Shift" and "Alt Gr" modifiers:

    @$&`'"\<>|"
  4. Click DrRacket program menu, Help--Help Desk.
  5. In help desk click "The Racket Guide"-"Racket Essentials"-"Simple Values". Run the following and make sure you understand the value expressions:

    (* 1/2 -3) ;multiply a rational with an integer
    (* 3+2i 2.1) ;multiply a complex number with a real number
    (sqrt 2) ;produces and 'inexact' number
    (< 2 3) ;a logical output value
    (and (< 2 3) (> 2 1)) ;a logical operation between logical values
    (string-length "abc")
  6. In Help Desk click "How to Design Programs Languages" link and examine the contents of "Beginning Student" section, "Pre-defined functions". Try the following:

    ; Contract: sayHello: name:String -> String
    ; Outputs a greeting message specific to given name
    (define (sayHello name)
        (format "Hello ~a, how are you ~aday?" name 2))
    (sayHello "Ali")
    (check-expect (sayHello "Ali") "Hello Ali, how are you 2day?")

Exercise 1

(20 minutes)

Write a function which computes the area of a circle, given its radius. Make sure to add contract comments and a test. Once finished save your program!

Show your program to your friend and ask her/him to assess your program using the provided peer assessment sheet, and the solution given by your assistant.

Images

(15 minutes) 1. Try the following commands:

    (circle 50 "solid" "red")
    (triangle 50 "solid" "red")
    (flip-vertical (triangle 50 "solid" "red"))
    (beside
       (square 50 "solid" "red")
       (square 50 "solid" "green"))
    (above
       (square 50 "solid" "red")
       (square 50 "solid" "green"))
    (overlay 
        (circle 50 "outline" "red") 
        (square 250 "solid" "green"))
    (place-image 
        (circle 50 "outline" "red") 
        50 70
        (square 250 "solid" "green"))

Exercise 2

(20 minutes)

Write a function which draws a shape as follows:

    (define (myFunction size shape)
        ... )
    (myFunction 200 (circle 40 "solid" "red"))
    (myFunction 150 (triangle 50 "solid" "red"))

images images

Exercise 3

(15 minutes)

Write a function which computes the area of a ring, given its inner and outer radius. Make sure to add contract comments and a test. Once finished save your program!