Note: Unfortunately, there’s no advanced tutorial this week. If you have an idea for one (related to subtyping or inheritance…let me know! I tried a couple of variations of adding this sort of thing to our interpreter…but they were way too hard to complete in one 50 minute period…). Speaking of which… If you were working on the adv tutorial last week and didn’t finish…why not work on that now? Advanced Tutorial 7. It’d probably be more fun. But who am I to say what’s fun. Especially since I’m having a conversation with myself. In a blockquote…that maybe no one will read. Oh well. Who knows. Come find me if you’re bored, we can find something for you to do!
Submitting
Regardless of how you choose to complete the assignment, you MUST submit the file you worked on to Canvas.
If you’re submitting remotely, your submitted file will be graded for completeness and correctness via an autograder. Make sure your functions are named correctly and that all of the built-in check-expects pass.
If you’re submitting in-class, make sure to check-in (there will be one question called CHECK-IN Q only available for the first 7 minutes of class) and check-out (there will be a three question survey called CHECKOUT SURVEY only available for the last 10 minutes of class) using PollEverywhere with your location verified.
Getting Started
Our goals for today 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 this week’s 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)
If you’re in attendance and it’s already the 25 minute mark, move on to Part 2. Come back to this question if you have time.
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 |