login name ______________________________Name ________________________________
This space reserved for the grader
1 2 3 4 5 6 total /15 /10 /15 /15 /25 /20 /100
If you need more space than I have allowed for an answer, turn the paper over and use the other side. Indicate when you have done that.
Iterator i = myWords.keySet().iterator();
i
is of type Iterator
,
myWords
is of type TreeMap
.
iterator
declared?
It's declared in class Set
, which is the type of object
returned by the TreeMap's keySet
method.
iterator
method.
public Iterator iterator()
i
".
The i
on line 19 has scope lines 19-21, that on line 27
has scope 27-33.
i
a good name in each case?
Each of the types int
and Iterator
begins
with that letter. A variable with that name usually stands for a loop
counter of some kind. A short name is good for a variable with small
scope.
% java WordList zero one two three two three zero one(The TreeMap stores its entries in alphabetical order by key.)
Discuss this assertion, using at least two examples from the Bank, Shapes and Juno packages. Be sure your discussion makes clear your understanding of polymorphism, inheritance and collections.
The command table in Juno is a collection of polymorphic ShellCommands. Each has its own doIt, implementing the abstract method declared in the ShellCommand superclass. That makes it possible to retrieve a command from the table and send it a doIt message without knowing which particular kind of ShellCommand it is.
A Directory maintains a collection (a TreeMap) of polymorphic JFiles, each of which can be a Directory or a TextFile. When listing a Directory we loop on the contents of that TreeMap, counting on each JFile's toString method to take advantage of polymorphism to produce its length and its suffix. Those are provided by methods implementing an abstract method in the superclass.
The big and little banks maintain collections of abstract BankAccount objects. The Bank's newMonth method can loop through the accounts, sending each a newMonth message. Each particular kind of BankAccount responds in its own way - that's polymorphism.
There is polymorphism in the shapes package: each particular shape (like HLine or Box) inherits properties from the Shape superclass but implements its own paintOn method. But there is no use of collections in this example (except for an optional part of hw5).
finger
command
behaves:
% java Juno Welcome to notMars running Juno version 6 help, register,At the end of this exam you will find a stub version of file, exit Juno login: register jill Jill Q. Public Juno login: jill notMars> finger jill jill Jill Q. Public notMars> finger root root Rick Martin notMars> logout goodbye Juno login: exit
FingerCommand.java
. Complete it.
Since you don't have access to the rest of Juno on this exam, here is
some of the API (generated with grep
) that you may find useful:
class method Juno public User lookupUser( String username ) Shell public User getUser() Shell public Directory getDot() Shell public Terminal getConsole() Shell public Juno getSystem() User public String getName() User public String getRealName()
Here is a working
FingerCommand.java
% java Juno Juno login: register jack John Q. Public Juno login: jack notMars:/users/jack> newfile .cshrc this is my .cshrc file notMars:/users/jack> newfile memo.txt Here is my memo
root users jack .cshrc memo.txt
jfiles
field of each
Directory
and the parent
field of each
JFile
. You need not show owners or dates or the
contents of text files.
Directory +---------------+ | TreeMap | TreeMap | +-----+ | +----+ |jfiles | --+-+----> | | | +-----+ | | --+-----> "users" | | | | | Directory | | --+--+ | +-----+ | | | | |parent | null| | | | | | +-----+ |<+ | +---------------+ | | | | +-----+------------+ | | Directory V | +---------------+ | | TreeMap | | TreeMap | +-----+ | | +----+ |jfiles | --+-+-+--> | | | +-----+ | | | --+-----> "jack" | | | | | | Directory | | | --+--+ | +-----+ | | | | | |parent | --+-+-+ | | | | +-----+ | | +---------------+<+ | | | +-----+------------+ | | Directory V | +---------------+ | | TreeMap | | TreeMap | +-----+ | | +----+ |jfiles | --+-+-+--> | | | +-----+ | | | --+-----> ".cshrc | | | | | | Directory | | | | TextFile | +-----+ | | | --+-----> +------------+ |parent | --+-+-+ | | | Directory| | +-----+ | | +---+| +---------------+ |parent | --++--+ ^ | +---+| | | +------------+ | +---------------------------------------+ (I should draw the memo.txt here too but it's too painful.)