Assignment 4: Visitors and Functions as Values/Arguments
Due: Tuesday, Nov 22 at 11:59pm via Turnin
This week, there is one common assignment for standard and advanced students.
1 Problem Setup: More Detailed Menus
In lecture, we introduced the process of passing functions as arguments by bundling them up in objects. Visitors are another example of the same concept, though in a more complicated context.
In this assignment, we use a more detailed example of menu items to practice working with both of these concepts. Rather than just have a single class of MenuItem, we want a richer class hierarchy that separates menu items into starters, entrees, and desserts. This file contains the MenuItem code from the methods-as-arguments lecture with three new classes for different kinds of menu items.
2 Passing a Method Over Variants as an Argument
Assume we want to count lemony desserts and garlicy entrees. One might try to do this using an isMatch method of the following form:
public boolean isMatch(MenuItem m) { |
if (m instanceof Dessert) |
return m.name.startsWith("lemon"); |
if (m instanceof Entree) |
return m.name.startsWith("garlic"); |
if (m instanceof Starter) |
return false; |
} |
Just to make sure you can pass a method as an argument, get the starter code running with this isMatch method. Confirm that your work is correct by writing a test case.
This implementation of isMatch is awful by OO coding practices. Why? Put your answer in a file questions.txt. A good answer to this question will be crisp and technical (2-3 sentences), stating explicit guidelines that this code violates and/or design goals for OO programs that this code complicates.
You do not need to submit the code or test case developed for this question. For this part, we are only looking for your answer in the questions.txt file. That said, you will likely find section 4 easier if you develop the code for this part first.
3 Eliminating instanceOf through Overloading
Java supports a feature called overloading, in which you can define multiple methods with the same name but different types (Java will call the appropriate method at run-time based on the types of the arguments). Overloading suggests a possible approach to eliminating instanceOf in the isMatch function. Rather than provide one isMatch method that takes a MenuItem, we can try providing multiple isMatch methods, each with one of the more specific types. For example:
public boolean isMatch(Dessert m) { |
return m.name.startsWith("lemon"); |
} |
|
public boolean isMatch(Entree m) { |
return m.name.startsWith("garlic"); |
} |
|
public boolean isMatch(Starter m) { |
return false; |
} |
Try replacing your current isMatch method with these three methods (be sure to remove the isMatch method that takes a MenuItem as an input). This version won’t compile. Explain why, in a precise technical comment (2-3 sentences will suffice). A good answer will not just restate the error message, but instead explain what the error message is complaining about relative to the particular code. Put your answer in your questions.txt file.
4 Fixing the problem through Visitors
To pass a method over variants as an argument, you need to use the Visitor Pattern (what we did in class on Monday and Tuesday).
Edit the starter file to include a visit method on each of the three MenuItem subclasses (visit here is analogous to traverse from the class notes). You will need to define the interfaces and classes that make up the rest of the Visitor pattern. Use visit (and any corresponding classes/interfaces you created along with it) to provide test cases with each of the following methods as arguments:
A test case that counts lemony desserts and garlicy entrees (the problem we started with in this assignment).
A test case that counts the number of Starters on the menu.
A test case that returns a boolean indicating whether there are any Starters that cost more than $15.
[Optional] For an extra check that you see how this works, also implement a test case of a method that takes a price as an input and determines whether any Starters cost more than the given price.
5 What To Turn In
Submit your .java files for section 4 (the Visitor implementation) along with your questions.txt file (with answers from sections 2 and 3). Do not submit the .class files. You do not need to submit any Java files related to the exercises in sections 2 and 3.