Recursive Structures Lab
Categories:
2 minute read
Introduction
The goal of this lab is to practice using recursion to interact with recursively structured data. One example of a recursively structured collection is the file system of a computer. A file system is made up of directories, each of which may contain additional directories, and so forth.
Please watch this 6-minute video by Gayle Laakmann McDowell :
Source Files
The following files are provided:
- DirectorySearcher.java – This (unfinished) class is responsible for recursively traversing a directory tree starting from a designated directory.
-
DirectoryDriver.java
– This is a driver class for executing the methods provided by
DirectorySearcher
. As currently written, themain
method uses aDirectorySearcher
to list the contents of the current directory.
Instructions
- Download and test the two files above. Make sure you understand how the existing code works.
- Complete the
listDirectories
(plural) method of theDirectorySearcher
class. Test your method by modifying the driver so that it calls this method instead oflistDirectory
(singular).- Your solution should print a
"/"
after each directory name and indent the files under each directory. For example, if the starting path is"../P5"
, the output might look like this:
- Your solution should print a
- Complete the
searchDirectories
method of theDirectorySearcher
class. Test your method by modifying the driver; search for different patterns to make sure it works.- Your solution should print the path to each file that is found. (Just print the File object itself, rather than calling getName.) For example, if the search string is
"se"
, the output might look like this:
- Your solution should print the path to each file that is found. (Just print the File object itself, rather than calling getName.) For example, if the search string is
- Submit your completed
DirectorySearcher.java
file via GradeScope.
Last modified April 30, 2022: practice coding exam (a2ce8c8)