On this page:
Introduction & Setup
Getting Started with Dr  Racket
Animations
Before You Go...
Stretch Goals
6.6

Lab 1 The Basics

lab!

Purpose: The purpose of this lab is to give you some hands-on experience with the BSL programming language and with the DrRacket programming environment for BSL.

Textbook references: Preface (DrRacket and the Teaching Languages), Prologue: How To Program, Chapter 1: Arithmetic, Chapter 2: Functions and Programs

Introduction & Setup

Goals: Read and sign the course contract.

Exercise 1 Follow the instructions on this page to set up an account on the handin server. You will be using the handin server to submit all your homeworks for the semester (including the homework that’s due Friday!).

Exercise 2 Read the course contract and sign it on the handin server.

Exercise 3 If you are using your own laptop, download and install DrRacket on your machine. If you are not, count to 10 in your head to keep things fair and balanced.

Getting Started with DrRacket

Goals: Interact with DrRacket’s definitions window, interactions window, and stepper. Create basic expressions and function definitions. Introduce different types of errors.

A drug company has realized that they can charge whatever they want for the medicine they are selling. Capitalism, ho! However, the more they charge, the less people will buy them. In a recent experiment the owner has determined a relationship between the price of his drugs and the average amount of customers. At a price of $500, 5000 people will buy the medicine. For each $1 increase in the price, 2 fewer people will buy it. That is, if the owner charges $501, 4998 people will buy the medicine (on average).

Exercise 4 Translate this idea into a mathematical formula. That is, how many people will buy the medicine at a given price? Then, transform this mathematical formula into a function definition. Remember that your function should not output a negative number, since that doesn’t make any sense in the context of this problem (the function max will be helpful to you here). Be sure to give your function a signature and at least two check-expects.

Unfortunately, the increased sales also come at an increased cost (since they have to produce more medicine). The production of the medicine comes at a fixed cost of $100 to the owner, plus a variable cost of $2 for each customer who buys it.

Exercise 5 The owner would like to know the exact relationship between profit and the medicine’s price in order to maximize the profit. Define a function profit which, given the price of the medicine, computes the profit the owner will make. Remember that profit is just the revenue the owner makes minus the cost of producing the medicine. Be sure to break the problem down into sub-tasks to make your life easier!

Animations

Goals: Get practice programming with images and writing a simple animation.

We want to create a cute little program where a bat moves towards some fruit. The bat will start in the upper, righthand corner of the screen, and will travel diagonally down and to the left until it reaches the fruit, located in the lower, lefthand corner of the screen.

Below is a video example of what we are trying to accomplish:

bat program

Starter Code: This line of code is required to program with images.

(require 2htdp/image)

Exercise 6 Define constants for the images you plan to use: the bat, the fruit, and the background.

Exercise 7 Define the function scene which takes in a number, representing the number of clock ticks since the program began, and outputs an image of the bat at the correct location, with the fruit in the lower lefthand corner.

Starter Code: These lines of code will run your animation.
(require 2htdp/universe)
(animate scene)

Exercise 8 Launch your animation by pressing run.

Exercise 9 Figure out how to edit your scene function to make the bat go faster or slower. Which part of your function controls the bat’s location? How can we make that location larger or smaller?

Exercise 10 Update your bat program so that a moon moves across the screen from left to right as the bat moves diagonally

Before You Go...

Make sure you can log into the handin server and sign the course contract if you didn’t already do that. If you have any questions regarding the lab or the course please feel free to ask a TA or tutor.

Stretch Goals

Goals: Use math to make an animation of the sun.

We want to create an animation of the sun rising and setting. The sun should start in the lower left corner of the screen and rise to the top of the screen and then down again to the lower right corner. It should follow the path of a parabola. While it’s rising, the color of the sky behind it should go from black to blue gradually over time, and while it’s falling the color should shift back to black. To do this you will need to use the color function. You can set the red and green values to zero and just change the blue value over time. You should start with a blue value of 0 and work your way up to a blue value of 255, then back to zero again. Like the y-coordinate of the sun, this value will be computed using a quadratic equation.

Here is a video showing what we are looking for:

sun program

In the following exercises we will use some notation to make the formulas clear:
  • x is the x-coordinate of the sun

  • y is the y-coordinate of the sun

  • t is the amount of time that has passed since the program began

  • S is the size of the screen (which is assumed to be a square)

  • D is the diameter of the sun (which is assumed to be a circle)

  • v is the blue value of the color of the sky

Exercise 11 Define the function sun-x which determines the x-coordinate of the sun based on the amount of time that has passed since the program began. The following formula should give you the correct x-coordinate of the sun:

x=[(S-D)t/S]+(D/2)

Exercise 12 Define the function sun-y which determines the y-coordinate of the sun based on the amount of time that has passed since the program began. The following formula should give you the correct y-coordinate of the sun:

y=[(4S-4D)t2/S2]-[(4S-4D)t/S]+S-(D/2)

Exercise 13 Define the function blue-value which determines the blue value for the color of the sky, given the amount of time that has passed since the program began. The following formula should give you the correct blue value at a given time:

v = [(-1020/S2)t2] + (1020/S)t

Exercise 14 Define the function sky-color which, given the amount of time since the program began, produces the correct color for the sky. This function should make use of blue-value but will also have to account for the fact that the blue value must be an integer between 0 and 255 (the functions min, max, and floor will be useful to you here). Remember that you can use the color function to create a color with given red, green, and blue values. So, for example (color 0 0 0) will create a black color and (color 0 0 255) will create a bright blue color.

Exercise 15 Define the function sun-scene which takes in a Number representing the amount of time since the program began and draws the sun at the correct x and y position with the correct sky color behind it. Run your animation using animate.