Our goals for this assignment are pretty simple:
- Understand why subtyping is useful
- Practice implementing subtypes in Racket
- Understand why methods are useful
- Practice implementing methods in Racket
In this tutorial, you’ll deal with these two concepts separately but in the associated exercise, you’ll have to deal with them at the same time while designing interactive quizzes.
Part 1. Fun at the zoo
Today we’ll be defining a whole zoo’s worth of animals. However, as we saw on Monday, we want to use subtyping to make our object design more efficient.
Activity 1.1
First define a abstract struct called animal that has the following fields:
nameageweight
Note: if you’re still struggling with structs, I HIGHLY recommend listing out all of the automagically defined functions you get as comments in your program like I did on Monday and in the demo video.
Activity 1.2
Now define a subtype of animal called cat which has an additional field:
sleeping-spot
Activity 1.3
Now define a subtype of animal called dog that has an additional field:
best-friend
Activity 1.4
Now define a subtype of animal called mouse that has an additional field:
hiding-spot
Activity 1.5
Now write a function called feed-animal! that takes as input a single animal and mutates its weight by adding 2 to it.
Make sure to write at least a couple of tests to make sure its working the way you think!
Hint: Remember, that now that we have imperatives…writing tests is a little confusing. You need to take into account what the weight WAS in order to find out what the weight will be AFTER calling the function.
Activity 1.6 (Challenge)
Write a function called feed-animals/faves! that takes in a list of animals (loa for short) and uses feed-animal! for each animal in the list with one big caveat…
Bob is Gizmo’s best friend. He is also the zoo keeper! Bob wants to feed all of the animals in the zoo, but he will feed any animal twice who lists him as their best-friend.
We’ve included some tests for you on this one.
Part 2. A roster of students
In the Racket file you downloaded scroll down until you see “PART 2”. Here you’ll find two different struct definitions.
; A student is...
; - (make-student string number string)
| Name | Type | Description |
|---|---|---|
| name | string | the student’s name |
| grad-year | number | what year the student will graduate in |
| major | string | the student’s major |
This struct does not have any methods.
; A roster is...
; - (make-roster string string (listof student))
; with...
; #:methods
; display : roster -> (void)
Th struct represents the roster of a course. Here are its fields:
| Name | Type | Description |
|---|---|---|
| course-name | string | the course’s name |
| instructor | string | the name of the instructor |
| students | (listof student) | the list of students in the class |
It also has a single method:
| Name | Inputs | Description |
|---|---|---|
display | takes as input ONLY a roster | Displays the roster in the interactions window, class name, instructor name, and then each of the students in the class. |
Activity 2.1
Your first job is to implement a new method for roster with the following specification:
| Name | Inputs | Description |
|---|---|---|
search-by-major | takes a roster and a string as input | searches through the roster, returning the list of students with the provided major |
Activity 2.2
Next (and last) implement one final method:
| Name | Inputs | Description |
|---|---|---|
names-by-grad-year | takes a roster and a number as input | searches through the roster, returning the list of students’ names with the provided grad-year |