Recursive Structures Lab

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:

  1. DirectorySearcher.java – This (unfinished) class is responsible for recursively traversing a directory tree starting from a designated directory.
  2. DirectoryDriver.java – This is a driver class for executing the methods provided by DirectorySearcher. As currently written, the main method uses a DirectorySearcher to list the contents of the current directory.

Instructions

  1. Download and test the two files above. Make sure you understand how the existing code works.
  2. Complete the listDirectories (plural) method of the DirectorySearcher class. Test your method by modifying the driver so that it calls this method instead of listDirectory (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
      
  3. Complete the searchDirectories method of the DirectorySearcher 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:
      ../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
      
  4. Submit your completed DirectorySearcher.java file via GradeScope.
Last modified April 30, 2022: practice coding exam (a2ce8c8)