

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


The follow are exercises to get you familiar with writing design patterns. We’ll post answers to these exercises on Tuesday.
BTree is a simple implementation of trees with arbitrary numbers of children, where a value is stored at each node in the tree. Write a method that returns an implementation of java.util.Iterator that returns the value at each node using a pre-order traversal. Write another method that returns an iterator doing post-order traversal instead.
Expression is an abstract superclass for arithmetic expressions on integers, where each expression can have up to two arguments. There are various sub- classes of this class, including Constant, for representing integer constants; AddExpression, for representing the sum of two expressions, etc. Write two visitors for this class. The first will print out an expression, and the second with compute its result. To do this, you will have to write accept methods for each subclass of Expression, and write two implementations of ExpressionVisitor, one for printing expressions (called PrintVisitor), and one for computing expressions (called ComputeVisitor). Each visitor should maintain its own state that it accumulates while visiting. For printing, this will be a string, while for computation this will be a integer, holding the result of the computation. Each visitor will define a method getResult that will return its state. Try writing these visitors in two ways (you’ll have to copy the code into two separate directories). First, do internal traversal. That is, have the accept method in each subclass of Expression invoke the visitor on each of its children (if it has any). Second, do an external traversal, within the visitor. In this case, the accept method will simply call the visitor on itself, while the calls to visit
an Expression subclass’s children will occur as part of the visit method for that kind of expression. See the lecture notes for more description on these two kinds of traversal.
The StringGenerator interface has a single method getString, which returns a String. One concrete implementation of StringGenerator is ConstString, which simply returns the same string each time it is called. Write two deco- rators for StringGenerator. The first, called EscapeQuotes, should, when its getString method is called, retrieve the string s from the StringGenerator it decorates, and then create a new string that is the result of replacing all oc- currences of " in s with " instead. The second, called MakeBold, should wrap prepend s with and append to it .
Create a type-safe enumeration class called Color that represents the six colors red, blue, green, yellow, purple, and orange. Follow the example for suits of cards given in the lecture notes, so that Color instances have a toString method. (Question: do you need to define a separate equals method? Why or why not?). Think about adding a compareTo method having the signature int compareTo(Color c), which could implement the following ordering: red < blue < yellow < purple < green < orange. In what ways might you do this? Think about doing this by defining new subclasses to implement the colors, as opposed to just keeping the class that you have.
Create a proxy for StringGenerator objects described above, called SecureGenerator. This class should have an extra method (that is, in addition to the getString method) called void authenticate(int key). By default, the SecureGenerator.getString method always return the empty string "". However, if the user calls authenticate with the “magic number,” then the proxy will from then on delegate calls to getString to the object that it’s a proxy for. Question: can you characterize the difference between a proxy and a deco- rator?
Project 2 used abstract factory in the GraphFactory class. If you wanted to make this factory potentially return multiple implementations of Graph, how