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, themainmethod uses aDirectorySearcherto 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 theDirectorySearcherclass. 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:P5/ Beetle.class Pose.java SwarmConstants.java StdDraw.java Actor.class StdDraw$RetinaImageIcon.class Point.java Bee.java beetles.txt Swarm.class Actor.java Insect.class Simulation.class .checkstyle all_examples.txt Beetle.java Flower.class Swarm.java SwarmConstants.class PoseUtils.class PoseUtils.java Simulation.java Pose.class Bee.class .settings/ org.eclipse.jdt.core.prefs SwarmDriver.class StdDraw.class swarm.txt flowers.txt bees.txt interesting.txt SwarmDriver.java .classpath .project Insect.java Point.class Flower.java
- Your solution should print a
- Complete the
searchDirectoriesmethod of theDirectorySearcherclass. 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:../P5/Pose.java ../P5/Insect.class ../P5/PoseUtils.class ../P5/PoseUtils.java ../P5/Pose.class ../P5/.settings ../P5/.settings/org.eclipse.jdt.core.prefs ../P5/Insect.java
- 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.javafile via GradeScope.
Last modified April 30, 2022: practice coding exam (a2ce8c8)