Purim-themed Midterms, 2023
In my Computer Science classes at Stern College, I like to integrate some Jewish-related themes. For exams, it makes midterms and finals more fun to write and, hopefully, more fun for students to take. Plus, it’s Torah UMadda!
So here is a selection of this year’s Purim-themed questions, and some answers, from my Programming Languages and Object Oriented Software Design courses.
1] From Programming Languages:
Kotlin Question: Write a Kotlin class called Cookie which takes into its constructor a string filling and an integer numCorners. These properties should be accessible for reading (thus equal to getting but not for setting).
Write the main function which will create a list (mutable or non-mutable, your choice) of cookies, some of which will have three corners and be filled with poppy seed, prune, or apricot. main() should call getHamentashenFillings() to get that smaller list, and then loop to print them out.Write the getHamentashenFillings() function that will take in a list of cookies and return, as a list of Strings, the fillings of only those that have three corners. For this purpose, it should be a relatively short function (where you can use = instead of {} and should use functional Kotlin methods which work on lambdas.
Here’s my answer:
2]
A) Getting tired of people who every year mix up their Arur Hamans (Cursed Be Haman) and Baruch Mordechai (Blessed Be Mordechai), where people mix up different parts and end up saying (chas veshalom) Baruch Haman, the Sages have hired you to develop a Context Free Grammar where such mixups are syntactically impossible.
Valid sentences should include (with comma literals, and without worrying specifically about gender):
Arura Zeresh , Haman , Kol HaReshaim
Arur Haman
Arur Kol HaReshaim , Zeresh
Arur Zeresh , Zeresh , Zeresh
Baruch Mordechai , Esther
Berucha Charvona
Berucha Esther
Baruch Kol HaTzadikimWrite the CFG in full that would generate all these strings (and others), but would NOT generate Baruch Zeresh or Arur Esther.
5 PTS E.C. If we did want to impose gender restrictions, so that only e.g. Berucha Esther and Arur Haman are valid, but not e.g. Berucha Mordechai and Arur Zeresh, briefly explain (with a picture and around five to six sentences) how you could use an attribute grammar to impose these restrictions, rather than adding new CFG rules.
Here is my answer (not including the extra credit):
The extra credit would involve an attribute grammar assigning inherent gender (masculine, feminine) attributes to terminals such as haman and baruch, with synthesized attributes moving upward in the parse tree, and predicates confirming matching gender attributes at the BlessList and CurseList levels.
[3] Next, for Object Oriented Software Design
(I gave them the Kotlin question above, but in Java.)
As a separate question, I first generated Java classes based on the first Mishnah in Megillah (about when various towns read) using ChatGPT. Thus:
That code was:
Then, I asked the students to refactor the code, using the Strategy Pattern instead. Thus:
Apply the Strategy design pattern, with different behaviors for reading the Megillah.
a) List what concrete classes, abstract classes, interfaces you want to have, and how they will interact with one another. If it is easier to draw UML boxes and arrows to show this, please do so. (1/2)
b) Rewrite the abstract Settlement class and also provide the code for one concrete reading strategy, so that I can see your approach. (1/2)
Just to show you what that might look like, here is how ChatGPT reorganized the code upon request.
I asked ChatGPT to swap out the interface for an abstract class, and it did so. Then I asked it to give me the UML, and here is what it generated.
Pretty good performance for ChatGPT here. The UML is slightly off but pretty close. (E.g. the LargeTown is incorrectly depicted as extending WalledCity, not Settlement.)
Which is why the purpose of my class is to make sure they understand the patterns and how / where they might be useful to apply, so that they gain the vocabulary and competence to see such code and confirm it is correct. But I imagine that as future professional programmers they will likely guide ChatGPT to generate it and act in a supervisory fashion. 5 minutes to work through this code, rather than, say, an hour.
[4] Another question, to explore the is-a and has-a relationships:
A mishloach manot Basket contains at least two different Food Items. Let us also believe the myth that they need to be two different Brachot (blessings), such as Shehakol (for fish) and Hamotzi (for eggs). A Person can send() a basket to another Person.
Draw the class hierarchy using UML. Also, which classes can (and therefore should) be abstract? (1/3)
Write the code for Person (1/3)
Write the code for Basket (1/3)
Here ChatGPT went totally off the rails, in terms of code generation, in terms of UML of its own code, and UML of code I provided to it. I’ll show my own code instead:
And here’s some UML: