Lab 7: Recursion Fun
Due Friday April 20, 2012 - 5:00pm
For your next lab, you will create two classes -- a class that contains several recursive methods, and a driver class that tests these methods. Your recursive methods should be in a class named RecursionFun, while your driver should be in a class named RecursionDriver. Your RecursionFun class should contain the following methods (plus any helper methods):
- public void traverseDirectory(File directory, HashMap<String,Integer> fileTypeCounts)
- traverseDirectory counts the number of each type of file in a given directory (and all sub-directories of that directory). The type of a file will be assumed to be the file extension (everything after the final . in the filename), or <unclassified> for files that have no extension. Thus the file RecursionFun.java would be of type java, the file readme.txt would be of type txt, and the file foo would be of type <unclassified>. When the function has finished executing, the HashMap fileTypeCounts will contain a mapping from file extensions to Integers, which denotes how many times each file type appeared in the directory (and sub-directories)
- Using HashMap class (java.util.HashMap): HashMap function definitions can be found here. The HashMap methods that you are most likely to need in your traverseDirectory are:
- boolean containsKey(Object key) Returns true if there is a value in the HashMap with the specified key
- Integer get(Object key) Returns the object associated with the given key, or null if no such key exists in the map
- Integer put(String key, Integer value) Inserts the key/value pair into the HashMap, returning the old value associated with this key (if any). You can ignore this return value.
- Note that we are assuming that you are using a HashMap<String, Integer> for the above definitions. The documentation for HashMap online is somewhat more general, to handle any kind of HashMap
- Using File class (java.io.File): The java File class represents either a file or directory on the disk. The File class has all sorts of helpful methods that give us information about the file. You can find the definition of all of the methods for the File class here, but the ones that you will find most helpful are
- boolean isDirectory() returns true if the File represents a directory, and false otherwise.
- boolean isFile() returns true if the File represents a regular (non-directory) file, and false otherwise
- String getName() returns the name of the file or directory
- File [] listFiles() returns an array of File objects that define all of the files (and directories!) in this directory. Returns null if this File object is not actually a directory.
- The constructor for a File object is the full path of the file (or directory) that you want to represent.
- Using HashMap class (java.util.HashMap): HashMap function definitions can be found here. The HashMap methods that you are most likely to need in your traverseDirectory are:
- As you would imagine given the name of this lab, your traverseDirectory function should be recursive. It would actually be quite difficult to write this function non-recursively, but the recursive version is much easier.
- traverseDirectory counts the number of each type of file in a given directory (and all sub-directories of that directory). The type of a file will be assumed to be the file extension (everything after the final . in the filename), or <unclassified> for files that have no extension. Thus the file RecursionFun.java would be of type java, the file readme.txt would be of type txt, and the file foo would be of type <unclassified>. When the function has finished executing, the HashMap fileTypeCounts will contain a mapping from file extensions to Integers, which denotes how many times each file type appeared in the directory (and sub-directories)
- void printPattern(int size)
- printPattern takes as input a size parameter, and prints out a triangle pattern, based on the size passed in. The pattern is easiest to describe using some examples.
- printPattern(0) would print out nothing at all.
- printPattern(1) would print out:
- 0
- printPattern(2) would print out:
- 0
- 01
- 0
- printPattern(3) would print out:
- 0
- 01
- 012
- 01
- 0
- printpattern(4) would print out:
- 0
- 01
- 012
- 0123
- 012
- 01
- 0
- printpattern(10) would print out:
- 0
- 01
- 012
- 0123
- 01234
- 012345
- 0123456
- 01234567
- 012345678
- 0123456789
- 012345678
- 01234567
- 0123456
- 012345
- 01234
- 0123
- 012
- 01
- 1
- You may use any helper methods that you like like, but neither printPattern nor any helper methods called by printPatern may use loops of any kind, and no helper method may take a string as a parameter.
- printPattern takes as input a size parameter, and prints out a triangle pattern, based on the size passed in. The pattern is easiest to describe using some examples.
The second class that you create must be named RecursionDriver. The RecursionDriver class contains only a main method. The main method should take the following two command line parameters, in this order:
Path of a directory
size
Your main should then:
- create an instance of RecursionFun
- create a File object, using the path string that is the first command line parameter, and a new (empty) HashMap
- Call traverseDirectory
- print out all of the file types in the traversed directory, with their counts
- Print a separating line: ------------------------
- call printPattern passing in the size parameter
- How can we print out the contents of a HashMap? The HashMap class has a method that returns a set of all the keys in the HashMap. We can use a for loop to go through all of those keys, as follows:
- HashMap<String,Integer>
- Set<String> keyset = fileTypes.keySet();
- for (String key:keyset)
- {
- // Do something with key
- }
Sample Output
If you were to run your program as follows:
java RecursionDriver /Users/galles/class/cs112/ 7
On my laptop, you would get the output:
File type: log, count = 15
File type: notXML, count = 1
File type: eml, count = 2
File type: html~, count = 4
File type: html, count = 136
File type: c, count = 1
File type: py, count = 1
File type: gif, count = 33
File type: txt~, count = 2
File type: prefs, count = 12
File type: xml, count = 2
File type: tex, count = 127
File type: aux, count = 10
File type: out, count = 3
File type: ps, count = 117
File type: obj, count = 46
File type: dat, count = 2
File type: dvi, count = 10
File type: pdf, count = 118
File type: tex~, count = 116
File type: c~, count = 1
File type: <unclassified>, count = 527
File type: htm, count = 3
File type: tmp~, count = 1
File type: sty, count = 2
File type: svn-base, count = 560
File type: jar, count = 12
File type: class, count = 227
File type: cls, count = 2
File type: eps, count = 42
File type: py~, count = 1
File type: txt, count = 62
File type: classpath, count = 32
File type: xml~, count = 1
File type: java, count = 514
File type: java~, count = 27
File type: doc, count = 1
File type: jpeg, count = 11
File type: dctheme, count = 1
File type: sty~, count = 1
File type: applet, count = 2
File type: project, count = 32
File type: DS_Store, count = 1
File type: css, count = 3
File type: tex#, count = 2
File type: xls, count = 2
File type: png, count = 13
File type: jpg, count = 6
--------------
0
01
012
0123
01234
012345
0123456
012345
01234
0123
012
01
0
Note that you will get different results running from different directories on different machines!
Submission
Please submit your work in an SVN directory https://www.cs.usfca.edu/svn/<username>/cs112/lab7.