On this page:
4.1 Follow-up from Lab
4.2 Visitor pattern for Binary Trees
4.3 Fixing Coffee
4.4 Yahtzee
4.5 Same XML
4.6 Same XML with unorder attributes
4.7 Concrete XML
Version: 5.2.1.6

4 5/29: Visiting and Equality

Due: 5/29, midnight by svn.

Language: Java.

You must complete this assignment with your partner and should not discuss solutions with anyone but your partner and the course staff.

This assignment is due Tuesday at midnight.

4.1 Follow-up from Lab

Complete exercises 5-9 from Lab 4 and 1-12 from Lab 5.

4.2 Visitor pattern for Binary Trees

Develop the visitor pattern for the binary trees data definition you developed in Lab 4.

Re-develop the methods of exercise 6-8 of Lab 4 as visitors.

4.3 Fixing Coffee

Take a look at the code we wrote showing the perils of instanceof and casts, which is on the Blog. Develop two corrections to that code—one using safe casting and one using double dispatch—so that all of the test cases pass. (Your solutions should be in two separate files.)

4.4 Yahtzee

There is yet another approach to structural equality that we didn’t discuss in class, but which you have all the concepts needed to carry out. We can define equality by way of a visitor that computes whether the visited value is the same as some given value.

Develop the visitor pattern for the Drink data definition and design an implementation of DrinkVisitor<Boolean> that computes whether the visited drink is structurally equal to a given drink. You may find it useful to develop helper visitors.

4.5 Same XML

Revisit your XML assignment and develop a same method that determines whether this XML is the same as some given XML. Two XML documents should be considered the same if they are structurally equal. You may use any approach you’d like, so long as it does not involve instanceof and casts (“safe casts” are OK).

4.6 Same XML with unorder attributes

Real XML considers the order of attributes to be irrelevant, so for example <img src="mom.jpg" alt="the best">...</img> is considered to equivalent to <img alt="the best" src="mom.jpg">...</img>, assuming the “...” are equivalent.

Modify your same method so that it ignores the order of attributes when considering whether two XML documents are the same.

4.7 Concrete XML

Develop a method for XML that produces the string representation of an XML document, i.e., the toXMLString method should produce the string "<a href=\"google.com\">Search</a>" from the representation of <a href="google.com">Search</a>. Note that \" is how to write a double-quote inside of a string, so "I said \"Hello\"" is the string representation of the text I said "Hello".