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.
- if you run it as it is when you first download it, you should see something like:
bin (Directory) .classpath .settings (Directory) .project src (Directory)
- if you run it as it is when you first download it, you should see something like:
- 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
"."
, the output might look like this:./ bin/ DirectorySearcher.class DirectoryDriver.class .classpath .settings/ org.eclipse.core.resources.prefs .project src/ DirectorySearcher.java DirectoryDriver.java
- if the starting path is
"../PA4"
, the output might look like this:PA4/ bin/ GameUtils.class AsteroidsGame.class Bullet.class Pose.class Asteroid.class Enemy.class Vector2D.class Ship.class AsteroidSize.class Playable.class Point.class Saucer.class Drawable.class Updatable.class GameElement.class StdDraw$RetinaImageIcon.class GameDriver.class StdDraw.class Star.class NumericDisplay.class .classpath .settings/ org.eclipse.core.resources.prefs .project src/ Playable.java Drawable.java Saucer.java Star.java Enemy.java Point.java AsteroidSize.java Ship.java GameElement.java GameUtils.java AsteroidsGame.java Bullet.java GameDriver.java NumericDisplay.java Pose.java Asteroid.java Vector2D.java StdDraw.java Updatable.java
- if the starting path is
- 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:../PA4/bin/Pose.class ../PA4/.settings ../PA4/.settings/org.eclipse.core.resources.prefs ../PA4/src/Pose.java
- while if you searched for empty string
""
, the output might be:../PA4 ../PA4/.DS_Store ../PA4/bin ../PA4/bin/GameUtils.class ../PA4/bin/AsteroidsGame.class ../PA4/bin/Bullet.class ../PA4/bin/Pose.class ../PA4/bin/Asteroid.class ../PA4/bin/Enemy.class ../PA4/bin/Vector2D.class ../PA4/bin/Ship.class ../PA4/bin/AsteroidSize.class ../PA4/bin/Playable.class ../PA4/bin/Point.class ../PA4/bin/Saucer.class ../PA4/bin/Drawable.class ../PA4/bin/Updatable.class ../PA4/bin/GameElement.class ../PA4/bin/StdDraw$RetinaImageIcon.class ../PA4/bin/GameDriver.class ../PA4/bin/StdDraw.class ../PA4/bin/Star.class ../PA4/bin/NumericDisplay.class ../PA4/.classpath ../PA4/.settings ../PA4/.settings/org.eclipse.core.resources.prefs ../PA4/.project ../PA4/src ../PA4/src/Playable.java ../PA4/src/Drawable.java ../PA4/src/Saucer.java ../PA4/src/Star.java ../PA4/src/Enemy.java ../PA4/src/Point.java ../PA4/src/AsteroidSize.java ../PA4/src/Ship.java ../PA4/src/GameElement.java ../PA4/src/GameUtils.java ../PA4/src/AsteroidsGame.java ../PA4/src/Bullet.java ../PA4/src/GameDriver.java ../PA4/src/NumericDisplay.java ../PA4/src/Pose.java ../PA4/src/Asteroid.java ../PA4/src/Vector2D.java ../PA4/src/StdDraw.java ../PA4/src/Updatable.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.java
file via GradeScope.
Last modified May 1, 2023: student-sourced updates (1f18b77)