Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

7. 2 From Pseudocode to Java - File Search, Version 1.0 Pseudocode: while(more directories to examine) { Get a directory If a file, check for

7. 2 From Pseudocode to Java - File Search, Version 1.0

Pseudocode:

while(more directories to examine) {

Get a directory

If a file, check for match

If a directory,

for(each file and directory in the directory) {

if a file, check for a match and return if found

if a directory, save this in a structure

}

}

return not found

task:

Build a linear search following the pseudocode approach above . You can put this code in simply a main, or you could design a static helper function in your HP static class that searches for files. Did you notice we used a stack to accomplish this directory searching? Using the main below and the outline

above, complete the iterative file searching algorithm in the method searchFiles() . Use fileObject.listFiles(), fileObject.isDirectory(), and fileObject.getName() to accomplish this below.

public static void main(String[] args) {

System.out.println( searchFiles(new File(c: \ \ ), hw3.zip) );

}

public static String searchFiles(File path, String target) {

//todo

}

7. 3 File Search, Version 2.0

We made use of an explicit stack to track new direc tories as they appear in the above iterative code. Do you think we could use the method call stack in place of our Java stack to accomplish this recursively? If we remove the stack definition and code above and produce a recursive function, might that be shorter? Change the main function above to call recursiveSearch() instead, which is a new method that you will create. This method will take the same input as searchFiles, but instead of searching through files using explicit for loops and a Stack from j ava.util, well be using recursive function calls to facilitate some of the looping and use the method call stack to store our directory list instead. Build a recursive linear search following the pseudocode you have refined to this point, or using the st arter pseudocode approach below. Note that multiple distinct correct solutions exist to this problem, so dont worry if your pseudocode isnt exactly like the code below.

public static String searchFiles(File path, String target) {

check if path is actu ally a dir, abort if not

loop over all files & dirs in the current directory

if a file, check for a match and return if found

if a directory, repeat these steps

if found in the directory, return found

return Not found; //if we made it here, we didnt find it

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions

Question

Question Can plan participants borrow from a VEBA?

Answered: 1 week ago

Question

Question What is an educational benefit trust and how is it used?

Answered: 1 week ago