Java Reference
In-Depth Information
An
Entry
is an object that represents either a
String
, or a list of other
Entry
objects. The
Entry
interface is shown below.
7.33
interface Entry
{
// Return true if Entry repesents an integer
boolean isString( );
// Return the represented string, or throw an exception
// if this Entry is representing a List of other entries.
String getString( );
// Return the represented List, or throw an exception
// if this Entry is representing a String.
List<Entry> getList( );
}
An example of an
Entry
is a file entry in Windows or Unix, in which
the file entry is either a single file or a folder (and the folder can con-
tain other files or more folders). Implement the public driver method
expandEntry
, which will invoke a private recursive method that you
must also implement. The public method accepts an
Entry
as a param-
eter and returns all the
Strings
represented in the
Entry
(in a
Set
). If
the
Entry
is representing a single
String
, then the resulting
Set
will
have size 1. Otherwise,
Entry
is representing a list of other
Entry
s, and
you should recursively include the
String
s represented by those
Entry
s. (The logic stated above is most likely found in the private
recursive routine).
To simplify your code, you may assume that no
Entry
refers to itself,
either directly or indirectly.
Redo Exercise 7.33 so that it still works if an
Entry
refers to itself
either directly, or indirectly. To do so, have the private recursive rou-
tine accept a third parameter that maintains a set that stores all the
Entry
objects that have been processed. Your routine can then avoid
processing an
Entry
twice. You may assume that
Entry
objects have
implemented any operations required to store a
Set<Entry>
.
7. 3 4
Explain what happens if in Figure 7.15 we draw the central square
before making the recursive calls.
7. 3 5
Search WWH ::
Custom Search