CS 110 Fundamentals of Computing - Hour Exam 2
Professor Bolker
November 20, 2001
Closed book and notes


login name ______________________________Name ________________________________

This space reserved for the grader

	1	2	3	4	5	6	 total
	  /15	  /10	  /15	  /15	  /25	  /20	    /100

Some of the questions call for discussions (like your memos). None of the answers should be long. One or two accurate sentences will usually do the job, if you know what you want to say. Be sure to read the question before you answer it.

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.

  1. (15) Here is line 27 in WordList.java (the full text is appended).
    	Iterator i = myWords.keySet().iterator();
    

    1. (3) How many tokens are there on that line? 13

    2. (4) Identify the tokens that refer to objects, and give the type of the object referred to in each case.

      i is of type Iterator , myWords is of type TreeMap .

    3. (4) In what class would you expect to find method iterator declared?

      It's declared in class Set , which is the type of object returned by the TreeMap's keySet method.

    4. (4) Write the declaration header for the iterator method.
      	public Iterator iterator()
      

  2. (10) Lines 19 and 27 each contain a declaration for variable named "i ".

    1. (5) What is the scope of each of those variables?

      The i on line 19 has scope lines 19-21, that on line 27 has scope 27-33.

    2. (5) Why is i a good name in each case?
    3. 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.

  3. (15) What will be printed in response to following command?
    	% java WordList zero one two three
    	two	three	
    	zero	one
    
    (The TreeMap stores its entries in alphabetical order by key.)
  4. (15) "Polymorphism makes it possible to design software that works with collections of objects of similar but not identical types."

    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).

  5. (25) Here is how Juno's version of the Unix finger command behaves:
    	% java Juno
    	Welcome to notMars running Juno version 6
    	help, register, , 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
    
    At the end of this exam you will find a stub version of file 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

  6. (20) Suppose Juno is running and these commands have been typed so far:
    	% 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
    

    1. Draw a picture of the complete JFile hierarchy at this moment.
      Hint: your picture should show three directories.
      	root
      	   users
      		jack
      		    .cshrc
      		    memo.txt
      
    2. Draw a box-and-arrow picture of the JFiles that have been created. Your picture should show the 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.)