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.
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>
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.
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.
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 *
***
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)
WcCommand.java
:
int words = new StringTokenizer(contents).countTokens();
words
,
StringTokenizer
, contents
,
countTokens
int
,
new
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();
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
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]"
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.)
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.
public boolean myMethod() { if (allIsWell) { // do the job return true; } return false; }
public void myMethod() throws ABadThingHappenedException() { if (!allIsWell) throw new ABadThingBadHappenedException(); // do the job }
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.
For most of the answers, check the glossary (MSWord).