CS 110 Fundamentals of Computing
Homework Assignment 9
Bolker and Rodriguez
Spring 2004

Due date: Thursday, April 15, when labs close.

  1. JOI Exercise 6-21 (improved help).

  2. Improve Juno's list command so that it looks a lot like the output from the Windows dir command:
    C:\Documents and Settings\eb\My Documents\cs110\users\eb> dir
    
      Directory of C:\Documents and Settings\eb\My Documents\cs110\users\e
    
     Volume in drive C has no label.
     Volume Serial Number is 8015-068A
    
     Directory of C:\Documents and Settings\eb\My Documents\cs110\users\eb
    
    04/05/2004  08:34 AM    <DIR>       .
    04/05/2004  08:34 AM    <DIR>       ..
    04/05/2004  08:31 AM    <DIR>       backup
    04/05/2004  08:33 AM		    13 Foo.java
    04/05/2004  08:32 AM                17 memo.txt
                   2 File(s)             30 bytes
                   3 Dir(s)   3,864,780,800 bytes free
    
    For the same JFile structure in Juno, list should output
    mars:\users\eb> list
    
    Directory of \users\eb
    
    04/05/2004  08:34 AM            .
    04/05/2004  08:34 AM            ..
    04/05/2004  08:31 AM          2 backup\
    04/05/2004  08:33 AM         13 Foo.java
    04/05/2004  08:32 AM         17 memo.txt
                   2 File(s)     30 bytes
                   3 Dir(s)
    

    Note that you can't use the toString from JFile. In fact that toString is not really a good design. If we really wanted a toString for a JFile (to replace the default) it should probably return the contents of a TextFile, and something else for a Directory.

    Note how the full path name is part of the Juno prompt. Make that improvement too, in Shell.java.

    Note that Directories are listed first, followed by TextFiles. Where Windows says "<DIR>" Juno will add the '\' suffix to the JFile name - the getSuffix() method will handle that task for you. To tell whether a JFile is a Directory or a TextFile you will need to use the Java keyword instanceof .

    Juno Directories also list their size: two more than the number of JFiles they contain. That's because by convention we always count . and .., the Directory itself and its parent. That means that in the example above, Directory backup must empty: the two JFiles "in it" are just . and .. .

    Output formatting is tedious but important. Practice it here. Learn to use NumberFormat and DateFormat from the Java library to get column alignments right.

    Start with ListCommand.java from Juno 6.5.

    The NumberFormat class does not provide right justification. I think it should, but since it doesn't I've written class Justify (source in Justify.java) to do the work for you. The source code and the class file are in juno6.5). You don't need to read the source code, but you can. We'll study the ideas there in Chapter 8.

    Class Justify provides two static methods that justify Strings. So convert the numbers you need to justify to Strings and then invoke these methods.

    
        /**
         * Right justify.
         *
         * @param text the String to justify.
         * @param length the length of the output String.
         *
         * If text is too long, don't truncate - better to destroy
         * formatting than to destroy information.
         *
         * @return text, padded on the left with blanks as needed.
         */
        public static String right( String text, int length ) 
    
    
    and
        /**
         * Left justify.
         *
         * @param text the String to justify.
         * @param length the length of the output String.
         *
         * If text is too long, don't truncate - better to destroy
         * formatting than to destroy information.
         *
         * @return text, padded on the right with blanks as needed.
         */
        public static String left( String text, int length ) 
    

    Formatting dates is no fun either, since the JFile API gives you the modDate only as a String. Here's a hack for the client (ListCommand.java) that will solve that problem for you:

    	String modDateString = jfile.getModDate();
    	Date modDate = new Date( modDateString );
    
    That way you recover the actual Date object from the String. The compiler will warn you that you have used a deprecated method. Don't worry.

    
     > From: a classmate
     > To: eb@cs.umb.edu
     > Subject: hw9
     > Date: Tue, 13 Apr 2004 12:59:55 -0700 (PDT)
     > 
     > hello Prof. Bolker,
     >  I have got a question about the formatting. Right now my output is
     > 
     > 04/13/2004  03:51 PM             .
     > 04/13/2004  03:50 PM             ..
     > 04/13/2004  03:50 PM          2 exam\
     > 04/13/2004  03:50 PM          2 test\
     > 04/13/2004  03:51 PM        16 a
     > 04/13/2004  03:51 PM     1624 h
     > 04/13/2004  03:51 PM         20 memo
     >                        3 File(s)   1660 bytes
     >                        4 Dir(s)
     >  
     > In the size column, the numbers do not have a group separator, like
     > 1,660, do we need to keep a group separator?
    
    Since Windows uses one a separator and the goal is to mimic Windows,
    you should have a separator if you can figure out how to do it. So
    
         1,624
    
    is better than
    
         1624
    
    If you can't do it (or need to spend your time on other parts of the
    assignment) then your memo should say that you just didn't or couldn't
    get to this feature.
    
    

  3. Optional. Internationalize Juno by writing the new shell command locale. The command
    	>mars:> locale france
    
    should cause all dates and numbers displayed for this Shell to appear in the format appropriate for France. Class Locale in the Java API is a good place to begin reading.

    This is a newly invented exercise. I've never done it and I don't know how hard it is, or the best way to go about it. You will have design choices to make, and may have to touch code in several classes. Write about your choices in your memo.