Practice with the Design Recipe

Make sure you start your file with:

#lang htdp/bsl

Be sure to follow the Design Recipe and write test cases for each function. When you need to use \(\pi\), just use the built-in constant named pi.

Also, whenever your expected answer is an inexact number, DrRacket won’t let you use check-expect. Because inexact numbers always have a “fudge factor,” you have to tell DrRacket how close the answers should be to be counted correct. In those cases, you have to use the function check-within. It consumes three values: the function call you’re checking, what you expect the answer to be, and how close the two numbers should be to one another.

Here are some examples that use check-within and \(\pi\):

(check-within (area-of-circle 1) pi 0.000001)
(check-within (area-of-circle 2) (* 4 pi) 0.000001)
(check-within (area-of-circle 5) (* 25 pi) 0.000001)

Here are some formulas you may need:

Write the functions below, making sure to follow the Design Recipe. You can put all of them in the same file, just skip a line or two between functions.

  1. Write the function volume-of-right-rectangular-prism, which consumes the length, width, and height of a rectangular solid and produces the volume. Write test cases for dimensions 2 by 3 by 7, 3.5 by 4 by 8, and 6 by 8 by 10.
  2. Write the function total-surface-area-of-right-cylinder which consumes the radius and height of a cylinder and produces its total surface area. Test it for a cylinder with radius 3 and height 4, another with radius 5 and height 2, and finally one with radius 5 and height 6.
  3. Write the function perimeter-of-regular-polygon. It consumes the number of sides in a polygon and the length of each side and produces the perimeter. Test it for a square with side length 3, a hexagon with side length 7, and a dodecagon with side length 15.
  4. Write the function lateral-area-of-regular-pyramid which consumes the number of sides the pyramid has, the length of each side, and the pyramid’s slant height. (Note that \(P\) in the formula for area is the perimeter of the base.) Test it for a three-sided pyramid where each side is 7 and the slant height is 10, a five-sided pyramid where each side is 3 and the slant height is 2, and a thirteen-sided pyramid where each side is 7 and the slant height is 23.
  5. Write the function slope. It consumes the \(x\) and \(y\) coordinates of two points and figures out the slope of the line connecting them. You may assume that the line is not vertical, so the slope will be defined. (Try your function with a vertical line in the Interactions window, just to see what happens.)
  6. Look up Heron’s Formula for finding the area of a triangle given the length of the three sides. Write a function triangle-area that uses the formula to find the area of a triangle.
  7. A movie theater has a simple profit function. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per customer. Develop the function total-profit. It consumes the number of customers who buy tickets for a show, and produces how much income the theater makes for that show.
  8. The local supermarket needs a program that can compute the value of a bag of coins. Define the function sum-coins. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag.