Purim-themed Midterms from last year (2022)
Every year, I make up a new midterm for my CS classes at Stern. And I like to tie-in the closest chag. I recently posted questions from my midterms for Programming Languages and OOP from this year.
I thought I’d share last year’s questions and answers as well, in honor of Purim. Why not give it a shot? (I’ll provide a possible answer after each question.) These are from my OOP / Design Patterns course. Full exam here.
1] Question:
Design Pattern - Strategy
You are the vizier in the Persian court, and the king has promoted you to a higher rank than all the other princes. Indeed, the king has commanded that all his servants bow down to you as you pass. However, there is one pesky Jewish man, who just does nothing when you pass. Here is the Java code describing the unpleasant situation.
Now, the non-bowing movement has spread to other servants! Some TypicalServants are bowing and some are not, influenced by Mordechai. You, as a programmer, will apply the Strategy Pattern to the above code, so that bowing vs. not bowing can change dynamically.
a) Draw the UML diagram for the new version and structure of the new code. Make sure to show all interfaces, classes, methods, attributes, as well as is-a and has-a arrows. (15 pts)
b) Write the new code for the abstract Servant class and the NoBowingBehavior class. Make sure that the behavior can be set in the Servant dynamically. (15 pts)
Answer: For the answer, I passed the code above into ChatGPT and asked it to refactor it using the Strategy Pattern, so that bowing is encapsulated in a BowingBehavior abstract class, with concrete classes for NoBowing and RegularBowing, and the Servant's bowing behavior can be set dynamically.
It did pretty well, just using an interface rather than abstract class that I specified for Bowing Behavior. Thus:
For the UML, I’ll ask it to provide Mermaid encoding of the classes and pass that into an online UML generator. (Hat tip to Iair Salem for the suggestion.)
It gives me this:
From which I can generate this:
2] Question: Design Pattern - Twin
As the Megillah relates, indeed, Queen Vashti was composed of two women. (“Gam vashti hamalka aseta mi-shetei nashim”).
To elaborate, one of those women was a Queen, who could do things such as throwing a royal banquet, wearing a crown, and commanding her maidservants to work, and so on. The other of these women was a RegularWoman, who did regular things such as eating, walking, wearing regular clothing, and so on. We would really like to be able to put Vashti in collections with other queens (such as Esther, Helen, Victoria, Cersei) to perform queenly actions, or pass her into functions that expect a queen. We would also like her to be placed into collections with other women (such as Zeresh, Sarah, and Yocheved). We want multiple inheritance. Use the Twin Pattern for this question.
a) Draw the UML diagram to show the structure of the code. Make sure to show all interfaces, classes, methods, attributes, as well as is-a and has-a arrows. (15 pts)
b) Write the code for one of the interfaces required for this, as well as for the combined QueenWoman class.
Answer: Here is my ChatGPT prompt:
I didn’t like its first response, but after a bit of cajoling, this is what I got.
3) Question. Long Coding: A religious obligation on Purim is to read, or hear the megillah read, from a kosher megillah Scroll. The Scroll contains the contents of the Purim story, returned as a String from getText(). The Reader has the Scroll and reads() from it, and when this happens, every Listener hears() the content of the megillah. Write the full code for the Reader (10 pts) and the main() function in the Main class (5 pts)
Write UML with the classes involved in this Java program. (15 pts)
Write the full code for the Reader (10 pts) and the main() function in the Main class (5 pts)
Answer: With some firm guidance, here is what ChatGPT generated for me, in code:
This is, of course, the Observer Pattern. I’ll skip the UML.
4] Question:
When people hear the name “Haman” during the reading of the Megillah, they make noise. There are various NoiseMakers that exist, such as Grogger, Whistle, LoudShoes, and NoNoise. When the Listener comes to shul (that is, in the main() function), he or she is provided with a concrete noisemaker.
Write UML with the classes involved in this Java program. (15 pts)
Write the following code (15 pts total):
i. main(), which creates two Listeners, gives each a different noisemaker (dependency injection), and has each listener make noise. (10 pts)
ii. the Listener class, which will have the Noisemaker and use delegation to make noise. (5 pts)
Answer, from ChatGPT, slightly modified:
5a] Question:
Once again, you are the misunderstood vizier of question 1. You have decided to launch an attack, but are unsure when the best month would be. You decide to write some Java code to implement this lottery. You call it a Pur, or alternatively you can call it a Goral. The possible random value to return is a Babylonian month, which are the following: Tishrei, Cheshvan, Kislev, Tevet, Shevat, Adar, Nisan, Iyar, Sivan, Tammuz, Av, and Elul. This should all be encapsulated inside an enum. (15 pts)
Answer, with some ChatGPT back-and-forth.
5b] Question:
On Purim, people drink all sorts of intoxicating beverages, such as wine, beer, and vodka. Different beverages have different levels of intoxication, which the client can get via a getter. IntoxicatingBeverage is an interface. Write the full code for IntoxicatingBeverage and for Wine. (15 pts)
Answer:
After some discussion with ChatGPT to simplify it to the level I wanted students to implement — it had initially given all beverages a name and intoxication level — here is what I got:
That’s it! Happy Purim!