1 Problem Setup: More Detailed Menus
2 Passing a Method Over Variants as an Argument
3 Eliminating instance Of through Overloading
4 Fixing the problem through Visitors
5 What To Turn In

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;

  }

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:

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.