On this page:
Posns
Custom Structures
A Simple Game
Before You Go...
7.5

Lab 3 Structured Data

lab!

Purpose: The purpose of this lab is to practice designing and programming with Posns and custom structures.

Textbook references: Chapter 3: How to Design Programs Chapter 5: Adding Structure

Posns

Recall the data definition for a Posn:

; A Posn is a (make-posn Number Number)
; and represents the x and y coordinate of a point in 2d space

Exercise 1 What is the difference between a Posn and a make-posn? If you aren’t sure, ask a staff member for help before you continue.

Exercise 2 Design the function posn-adder which takes two Posns and produces a single Posn whose x-coordinate is the sum of the two x-coordinates and whose y-coordinate is the sum of the two y-coordinates. Be sure to follow all the steps of the design recipe for functions.

Exercise 3 Below is a data definition for a book. What is the problem with this data definition?
; A Book is a (make-posn String String)
; - where the first String is the book's title
; - and the second String is the book's author

Custom Structures

Exercise 4 The Franklin Park Zoo wants to keep better track of their animals but they need your help! Each animal at the zoo has a name, a species, an indicator of whether they are kept outside or inside, and a diet which can be either herbivorous, carnivorous, or omnivorous. Design appropriate data definitions to describe an animal at the zoo. Be sure to follow all the steps of the design recipe for data.

Consider the following data definitions:

(define-struct address [num st city us-state zip])
; An Address is a (make-address Nat String String String Nat)
; - where num is the number of the building on the street
; - st is the name of the street
; - city is the city the building is in
; - us-state is the state the city is in
; - and zip is the zipcode of the building
 
(define-struct student [first last nuid local perm])
; An NEUStudent is a
;  (make-student String String PositiveNumber Address Address)
; - where first is the student's first name
; - last is the student's last name
; - nuid is the student's NUID #
; - local is the student's local address
; - and perm is the student's permanent address

Exercise 5 Complete the data design recipe for the above data definitions.

Exercise 6 Design the function student-email which takes an NEUStudent and produces a string representing that student’s email address. For simplicity we will say that a student’s email address is always their last name (all lowercase), followed by a period, followed by the first initial of their first name (also lowercase), and finished with "@husky.neu.edu".

Exercise 7 Design the function update-zipcode which takes an NEUStudent and a number, representing the new zip code of the person and updates both their addresses to have that zip code. Be sure to follow the template!

A Simple Game

In the remaining time we will work on designing a small program which allows you to move a dot by clicking the mouse. The program should display a black background with a white dot at some given initial location and then when the user clicks the dot should appear at the mouse’s location instead. To do this, we will follow the same steps for designing programs as we did in the previous lab.

Step 1: What stays the same?

Exercise 8 Define some constants to describe what stays the same throughout the game. What images will you need in order to draw the program?

Step 2: What changes?

The only thing that changes in this program is the location of the dot. How can we best represent this? Think about the types of data you know and choose one that represents a location in 2d space. This will be your world state.

Step 3: Which handlers do we need?

Exercise 9 Write the signature and purpose statements for the handler functions you will need. Recall that big-bang always requires a to-draw clause. Which other clauses will you need based on the program description? If you are having trouble take a look at the documentation, and if you are still confused, ask a tutor or TA for assistance.

Step 4: Main Function

Now that we’ve decided on our handler functions, we can put together our main function which will call big-bang. Recall that we write this function first because in this course we follow a top down programming approach. That means larger functions go at the top of our program and smaller functions go underneath.

Exercise 10 Design the main function dot-clicker which, given an initial position for the dot, starts up the big-bang program.

Step 5: Design your handlers

Exercise 11 Design the handlers you decided on in step 3.

Give your program a try! Recall that the dot should only change positions when the mouse is clicked, and not every time the mouse moves. Good work!

Before You Go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.