CompSci 100E- Classwork 8 - February 24, 2011

Start by snarfing Clwk8recursion.

Part 1 - Recursive Reverse

In the class SumItUp values are stored in an ArrayList. First look at the method RecursivePrintAll that prints the elements in the order they appear in the array, starting at position 0.

Complete the method RecursivePrintAllInReverse that prints the elements stored in the ArrayList in reverse order using recursion. You can start with the code from RecursivePrintAll and modify it. DO NOT MODIFY the call in main: RecursivePrintAll(0). That is k=0 the first time it is called.

HINT: Do the recursive call first, then print the element in slot k.


Part 2 - Listing Directories

Files on a computer are organized hierarchically in directories. Because directories can contain files or other directories, which can contain more files and more directories, the structure itself is recursive. This structure makes using recursive algorithms a natural technique for processing it.

Complete the following problems in the file DirReader.java with regards to directories on your computer. Note that this file also uses the file DirChooser.java, which you do not need to modify.

  1. Complete the method recursiveList that has one parameter. List all files in a directory recursively. Visit all of the files in the given directory and its sub-directories and return a list of File objects.

    Hint: You'll need to use the File method isDirectory. If it is a directory then you want to use a recursive call to return a list of all the files in that directory. The recursive call will return a List. You can use the ArrayList method addAll to add all the items in one list to another list...

    If the item is not a directory, then just add the file to the list of files.

  2. Complete the method recursiveList that has two parameters. List all files in a directory recursively, but only up to a certain depth. Instead of following contained directories indefinitely, one might want to only list files to a certain depth, that is the distance measured in nested sub-directories from the starting directory.

  3. Complete the method recursiveSize. Compute the total size of all files in a directory recursively. Visit all of the files in the given directory and its sub-directories and return an int representing the sum of the size of all the files found.

    Hint: You will need to use the File method length for the size of a file.

  4. Complete the method recursiveMax. Compute the maximum sized file in a directory recursively. Visit all of the files in the given directory and its sub-directories and return an int representing the largest size of all the files found.

  5. Complete the method printDirectory that has two parameters. (Note that it is called from the other printDirectory method that has one parameter.) Print list of directories, with sub-directories indented below their containing directory. For a directory, all the files in that directory should be printed directly below it, indented by a few spaces. This means, for example, that a directory named maindir containing files named file1, file2, file3, file4 and subdirectories subdir1, subdir2, subdir3, subdir4 might be printed as follows (where each sub-directory has files as shown).

       maindir
          file1
          subdir1
            xxx
              foo
              bar
            yyy
          file2
          file3
          subdir2
            zzz
            ppp
          subdir3
            ooo
          file4
          subdir4
            www
            nnn
    

    Hint: See the method named tab, it might be useful!

Submit

Create a README file with both of your names in it if you work with a partner.

your Java code and README file to the clwk08-0224 folder.