This Year's Shavuot-Themed OOP Final (2023)
Here is my final at Stern College for Object Oriented Software Design. We do Torah UMadda here. If you want to see last year’s final, see here:
Instructions: Answer four questions for 100 points, at 25 points each. You may mix and match parts. Answer an additional full (25 point) question for half points extra credit (13 points).
1) State Pattern
Rabbi Freifeld (of Shaar Yashuv) would say: “A Jew is at his freest when he is upgedavent and pareve.”
Using classes, inheritance and composition, implement the following. A Jewish Person can do all sorts of things, like talking, walking, and learning. She can also eat() Food. But if that food is of a Meat type, then she enters the Fleishig state for six hours. If of a HardCheese type, then she enters the Milchig state for six hours. If regular Dairy or something Neutral, then she remains in whatever state she is. If she tries to eat something Dairy or HardCheese while Fleishig, or something Meat while Milchig, we should not do it and print an error message. On any attempted action, check how much time has elapsed and change the state.
How to implement the six hours? Well, every action, such as walk, talk, learn, eat, all will cause one hour to elapse.
Draw the UML for all classes, interfaces involved (10pt)
Write the full code for the JewishPerson class (15pt)
2) Command Pattern
Fed up with missing a day of Sefirat HaOmer for the 10th year in a row, you have decided to prepare your recitations in advance. Using a for loop, a lambda (where the contents of the function, taking no parameters and returning no return value, will be System.out.println(“Today is the ” + ___ + “day of the omer”); ), and an appropriate SAM interface, generate an arraylist of Recitation objects, which the client can later use to recite() the appropriate day. Write all the code for this. (25 points)
3) Decorator Pattern
Shani really wants to learn on Shavuot night. However, like other members of the Person class, she can eat, walk, and learn. But her performShavuotNight() method just does println(“zzzzzz!”);
Historians have discovered that the widespread adoption of staying up all night for a Tikkun Leil Shavuot coincided with the sudden availability of coffee in Europe.
You will use the Decorator pattern to change the behavior of performShavuotNight(). See, it is possible to create a Caffeinated Person, who is able to stay up Shavuot night and learn, and otherwise behave entirely like a normal Person.
Draw the UML for your classes, interfaces, etc. (10 pts)
Write the code for the Caffeinated Person class. (15 points)
4) Builder Pattern
A follow-up to the Decorator Pattern question (3). Instead of directly creating a Person and her caffeinated decoration, we want build() a Person. We will assign name, age, and optionally that she is caffeinated (and has such decoration / strategy) or inebriated (and has such decoration / strategy). So Vered might be caffeinated, Daphne can be undecorated and sleep; Sarale can be doubly caffeinated.
Write the code for the Person class (10 points)
Write the code for the PersonBuilder class. Don’t forget to write build()! (15 points)
5) Template Pattern.
The shul has a process for learning on Shavuot night: Namely,
There is some choice in terms of which shiur to attend, e.g. a shiur on gemara, halacha, hashgacha, whether you are going to learn alone, with a single partner, or in a group. Also, the coffee breaks are optional – a hook. You will use the Template Method pattern for this.
Draw the UML for all classes involved in this.
Either: Write the full code for the class implementing Tamar, a shtark GPATS student who will actually deliver the gemara be’iyun shiur for Shiur 1, will learn with a chavruta, and will only opt in to coffee break 2, and will attend a gemara shiur for shiur 2
Or: Write the full code for the abstract template class.
6) Short Coding questions (10 points each)
A Turkey has int numFeathers and String name. Write a comparator for Turkey specifically using an anonymous class, not with a named class or a lambda. It should compare via numFeathers. But if the numFeathers are equal, break the tie with the name. Note: people got this wrong on the midterm. Recall that numFeathers is a primitive int, so unlike Integer, you cannot call compareTo directly on it.
Write a JUnit test for BubbleSort.
Write Guava code to split() based on “ ,” that is, space followed by comma. Ignore empty elements.
7) Iterator Pattern
Consider this code for a Node and a LinkedList.
a. Make the LinkedList class Iterable. What would you have to add? (Just tell me the new lines or what lines to change, you don’t need to copy all the code.) (10 points)
b. Write the code for the LinkedListIterator. (15 points)
8] Concept questions (5 points each). Answer in at least 3 sentences.
Describe an example we covered in class of the Null Object Pattern.
What is the Law of Demeter?
Describe the homework problem of the Multi-Armed Bandit, and how it differed from the One-Armed Bandit.
Does Java have real pass-by-reference? Explain.
Explain how a predicate function can be used to filter a stream.
What is delegation?
[Midterm Makeup Question]
Strategy Pattern
For those five of you who misunderstood question 1 on the midterm and wrote the code that was already there / drew the wrong UML, here is an opportunity to show that you know Strategy. First read question 3, to get in the right frame of mind. Deena wants to stay up all Shavuot night, while Dina would rather sleep. Rather than using typical inheritance, isolate and encapsulate the performShavuotNight() behavior and allow it to be assigned dynamically. The typical behavior will be to sleep (“zzzzz!”), but we can dynamically (that is, during runtime) assign a different shavuot night behavior, namely to stay up and learn. Maybe this would even be after Deena drinks a cup of coffee.
Draw the UML for your classes, interfaces, etc. (10 pts)
Write the full code for the Person Person class. (15 points)