In this module we introduce and apply the Design Recipe to a series of example problems. The examples introduce new kinds of data definitions, scalars and itemizations, and demonstrate how to use rackunit to develop tests for BSL functions.
- Use rackunit to write tests.
- Use rackunit to debug a function.
- List the four goals of testing.
- Explain the four goals of testing.
- Use the syntactical conventions for function definitions.
- Write a code example of scalar data.
- Define the term itemization.
- Use a bulleted list for an itemization data definition.
- Write the interpretation for simple data.
- List the six steps of the design recipe in order.
- Explain what each step of the Design Recipe does.
- Given a solution, label each step of the design recipe.
- Given a signature, write down its inputs.
- Given a signature, write down its outputs.
- Append predicate function names with the question mark character.
- Use the arrow to separate input and output in function signatures.
- DUE DATE: September 25, 2017 @ 11:59pm
Instructions
All problems must be solved using the Design Recipe and you must show all your work for every step in the Design Recipe. You code must also follow the class' coding style.
Problem 1
Design a function called logical-xor
that consumes two booleans
and has the same behaviour as the logical
exclusive or connective
Problem 2
A friend of yours came up with the following data definition
;; A MaybeNumber is one of
;; - Number
;; - 'undefined
- The data definition is incomplete. Please complete it.
-
Design a function that performs addition on
MaybeNumber
s. Your function should return aMaybeNumber
. If the two inputs are not the symbol'undefined
the function should retrun the sum of the two numbers. Else the function should return'undefined
.
Problem 3
Design a function that consumes the current time as 2 numbers, the first number denotes hours and the
second number denotes minutes. Your function should add 1 minute to the current time and return the update
time as a string, e.g., "12:44"
. The value for hours should be between
0 to 23. The value for minutes should be between 0 and 59.
Problem 4
Design a function that given a letter grade returns your GPA. Use Northeastern's mapping from letter grade to GPA.
Problem 5
Design a function that consumes a file size and its units as a symbol and returns the total number of bytes. For example when the function is given 10 and 'MB returns 10485760 (1 MB = 1,048,576). You can use this table for your conversions. Use the Traditional Units table.
- DUE DATE: October 2nd, 2017 @ 11:59pm
Instructions
- All solutions must follow the Design Recipe
-
You are not allowed to use the built-in functions
-
eq?
-
equal?
-
eqv?
-
Problem 1
For each of the following structure definitions provide
- Signatures for all selectors
- Signature for the constructor
- Signature for the predicate
- A template
(define-struct student [name id grade])
;; A Student is (make-student String PosInt NonNegInteger)
;; WHERE: 0 <= grade <= 100
;; INTER: represents a student with the student's name
;; the student's id and the student's grade.
(define-struct container [width height depth capacity label])
;; A Container is (make-container PosInt PosInt PosInt PosInt Symbol)
;; WHERE: capacity = width * height * depth
;; INTERP: represents a container with its width, height, depth
;; in centimeters, it's capacity in cubic centimeters and
;; it's label
(define-struct sweater [material size producer])
;; A Sweater is (make-sweater Symbol PosInt String)
;; INTERP: represents a sweater with the sweater's material
;; it's size and the name of the manufacturer
(define-struct game [name min-ram min-graphics-ram online?])
;; A Game is (make-game String PosInt PosInt Boolean
;; INTERP: represents a game with it's name, the minimum ram
;; capacity needed , the minimum graphics
;; card memory needed and whether it is an online game or not
Problem 2
Design a program that consumes two Cartesian
coordinates as Posn
s and returns the distance between the two
coordinates.
Distance between two points explains how to calculate the distance between two Cartesian coordinates.
Problem 3
You started working for a small company that produces cash registers. The company is currently designing their new model and one of your colleagues has provided you with the following data definitions
;; Dollars is a PosInt
;; Cents is a NonNegInteger
;; WHERE: Cents is greater or equal to 0
;; and less or equal to 99
(define-struct amount [dollars cents])
;; An Amount is (make-amount Dollars Cents)
;; INTERP: represents total amount in dollars and cents.
You have been tasked to design the add-to-amount
function that will consume the current amount, amt
, the number of
dollars, dollars
,
to add and the number of cents, cents
, to add. Your
function should return the total amount after
adding dollar
and cents
to
amt
.
Problem 4
You are given the following data definition
;; A Style is one of
;; - 'outline
;; - 'solid
(define-struct disk [radius style location])
;; A Disk is (make-disk PosInt Style Posn)
;; WHERE (and (< (+ radius (posn-x location)) 500)
;; (>= (- (posn-x location) radius) 0)
;; (< (+ radius (posn-y location)) 500)
;; (>= (- (posn-y location) radius) 0))
;; INTERP: represents a disk with its radius, its style
;; and its location as an (x,y) coordinate
Design the function connect-disks that consumes two disks and draws the two disks and a red line from starting at the center of one disk and ending at the center of the second disk on an empty 500x500 canvas.
For example, evaluating
(connect-disks (make-disk 50 'outline (make-posn 100 100))
(make-disk 50 'outline (make-posn 300 400)))
produces,
Note: for functions that return Images you are allowed to not have tests
using check-expect
. You should still test these
functions manually using the interactions window.