CS 110 Fundamentals of Computing - Answers to Final Exam
Professors Bolker and Rodriguez
Open book
December 19, 2001
login name ________________________        real name_________________________

1	2	3	4	5	6	7	total
  /50	 /20	 /20	 /15	 /15	/15	/30	    /165

Note that some of the answers go on this exam, some in your blue book. If you need more space continue in the blue book.

  1. (50 points) In order to answer this question you will need to read and understand the code in file WcCommand.java (attached, with line numbers). That code compiles and runs correctly (We have tested it).

    Here is a fragment of a Juno session using wc:

    notMars> ls
    3 files:
    greeting	root	12	Tue Dec 18 13:06:35 EST 2001
    greeting2	root	13	Tue Dec 18 13:06:35 EST 2001
    users/	        root	5	Tue Dec 18 13:06:35 EST 2001
    notMars> cat greeting
    hello, world
    notMars> cat greeting2
    hello,
    world
    notMars> wc greeting
    
    12	2	1
    
    notMars> wc greeting2
    
    13	2	2
    
    notMars> wc foo
    
    JFile foo not found.
    
    notMars> wc users
    
    JFile users not a TextFile
    
    notMars> wc
    
    Usage: wc filename                        
    
    notMars> help
    shell commands
      cat: display contents of a TextFile
      cd: change current directory
      help: display ShellCommands
      logout: log out, shut down the Shell
      ls: list contents of current directory
      mkdir: create a subdirectory of the current directory
      newfile: create a new TextFile
      wc: count characters, words, lines in a TextFile 
    notMars> 
    

    1. (5) How many registered users are there in this Juno system (not counting root)?

      Probably 5, since that's the size of the users directory where the users' home directories live. But you can't be sure (as a classmate pointed out) because someone might have gone to users and created some ordinary TextFiles and Directories.

    2. (5) Explain why there's a 12 in the listing for greeting but a 13 for greeting2.

      This question is ambiguous. If there's no space after the comma on the first line in greeting2 then the character count for both files would be 12, with a newline replacing the space. So either there is a space there, or the newline is really (carriage return) + (line feed), or something else as yet unexplained is happening. In fact I destroyed some evidence. I created greeting2 using Juno's getfile command, with input file twolines , which has no space after the comma but does have a newline after the second line. That showed up as an extra empty line in the original Juno output but I deleted it.

      Whatever the explanation, since there's a 13 in the listing for greeting2 there needs to be a 13 here.

      Almost any reasonable answer gets full credit.

    3. (10) Provide appropriate text for lines 4, 8-13, 19, 20 and 23-27 in WcCommand.java .
           4	// Modified: December 19, by Ethan Bolker, for the exam
      
      
      Note: some people got an earlier version of the exam that did not call
      for editing line 4. They lost no credit.
      
           8	/**
           9	 * A WcCommand object's doIt counts and reports the number
          10	 * of characters, words and lines in a TextFile
          11	 *
          12	 * @version 7
          13	 */
      
          18	    {
          19		super( "count characters, words, lines in a TextFile", 
          20		       "filename" );  // argument expected
      
          23	    /**
          24	     *  Implement the word count command.
          25	     *
          26	     *  Get filename from next token, count chars, words, lines.
          27	     *  Print result to Shell's console.
          28	     *
      
    4. (10) At each of the six lines marked *** in the script above, show what Juno's response would be. Your answer should take into account your answer to the previous question. (See above)
    5. (20) Here are lines 44 and 45 of WcCommand.java:
          int words = 
      	new StringTokenizer(contents).countTokens();
      

      • (2) How many tokens are there on those lines 13

      • (3) Which tokens are identifiers? words, StringTokenizer , contents , countTokens

      • (3) Which tokens are Java keywords? int , new

      • (12) In what class would you expect to find method countTokens declared? Write the declaration header, and a javadoc comment to accompany it.

        In class StringTokenizer :

        
        	/**
        	 *  Get the number of tokens in this StringTokenizer.
        	 *
        	 *  @return the number of tokens. 
        	 */ 
        
        	public int countTokens();
        

  2. (20 points) What is the call stack when method retrieveJFile in Directory.java is executing because it was called from line 39 in WcCommand.java ? Your answer (in the blue book) should list the methods and the classes they are in:
    	Class			method
    
    	Juno			main
    	Juno			constructor
    	LoginInterpreter	CLIlogin
    	LoginInterpreter	interpret
    	Shell			constructor
    	Shell			CLIShell
    	Shell			interpret
    	DcCommand		doIt
    	Directory		retrieveJFile
    
  3. (20 points) Draw a box-and-arrow picture (in the blue book) showing the state of the variable commandTable declared on line 36 of Juno.java (Program Listing 7.11) after line line 79 in method fillTable in ShellCommandTable.java has executed.

    Here's the answer in words arranged as a picture. Drawing the boxes in emacs is too hard.

    
      Juno object - a box containing:
      commandTable field (type ShellCommandTable)
         arrow to
    
      ShellCommandTable object - a box containing:	 
      table field of type TreeMap (or Map)
         arrow to
    
      TreeMap object - a box containing
         key field   - arrow to ------ "ls"
         value field - arrow to ------ LsCommand object - a box containing
                                        helpstring field - arrow to
                                           "list contents of current directory"
                                        argstring field - arrow to ""
                                       
         key field   - arrow to ------ "cd"
         value field - arrow to ------ CdCommand object - a box containing
                                        helpstring field - arrow to
                                           "change current directory"
                                        argstring field - arrow to "[directory]"
    
  4. (15 points) After you retrieve an object from a Java collection sometimes you must cast it to its own type before sending it a message. Sometimes casting it to the type of a superclass is sufficient.

    Discuss (in the blue book) each of these constructions, referring to explicit examples from programs you have worked on this semester. We should be able to understand your answer without having the code in front of us.

    Answer. When you want to send a message that only the particular class can understand you must cast it to that type. cd in Juno provides an example. After retrieving the named JFile from the current Directory you must cast it to (Directory) before you can assign it to the dot field of the Shell. (There are many other examples.)

    Sometimes you only want to send messages that any of the stored objects can respond to since all are instances of subclasses of a common superclass. newmonth in the Bank application provides an example. That method retrieves the BankAccounts sequentially from the TreeMap or ArrayList field, casts each to type (BankAccount) and sends each a newmonth message, without knowing or caring what particular kind of account it is,doIt messages sent to ShellCommands retrieved from the table provide another example. (Polymorphism at work.)

  5. (15 points) It is often useful to override a method in the Object class in order to change how instances of a subclass respond to some message. We have studied two such methods. Discuss one of the two in detail, giving examples from our work.

    Answer. The Object class declares and implements the toString method that returns the way Java will represent that Object as a String of characters. The default is to concatenate the name of the object's class with an @ sign and then a strange hexadecimal number. If you want your object to represent itself as a String in some more sensible way, override that method. We did that in the User class in Juno. Strings, ArrayLists and TreeMaps override toString too.

    The Object class declares and implements the equals method that Java uses to compare this object with another object. The default is that two objects are equal if they are references to the same place (the box and arrow diagrams make this clear). The String class overrides that method, so that equals compares Strings a character at a time as it should. Two ArrayLists are equal if they contain equal elements in equal order. Two TreeMaps are equal if they store equal elements with equal keys.

  6. (15 points) When handling an error in a program you can either Which is better? Illustrate (in the blue book) with examples from code we have studied. It depends ...

    When the error is easily detected and can be dealt with locally, the first is better. Shell's interpret method uses it to deal with a command that's not found. When the program is complex and detection and dealing are far apart on the call stack the second is best. In hw7 you threw a JFileNotFoundException in Directory's retrieveJFile method, but didn't catch that exception until Shell's interpret mathod.

  7. (30 points) Use each of the following words or phrases in a sentence or short paragraph describing something we covered in this course. Your answer (in the blue book) should show that you know the meaning and the significance of the concept. Do not give a formal definition or simply paraphrase something from the text.

    For most of the answers, check the glossary (MSWord).

    1. OOP
    2. API
    3. JVM
    4. delegation
    5. unit test
    6. signature
    7. io redirection
    8. linking files
    9. grep