Pseudocode is a useful tool for mapping out complex algorithms in a language-independent way before even writing one line of code. GoogleStepwise Refinement and Divide-and-Conquer. We’re going to practice this approach in detail here. We’ll start with a very high level description of the program, and flesh out bits of detail in each stepwise refinement. Given the small amount of pseudocode below, can you take a small, incremental step to describe in more detail the steps required to accomplish this task?
Pseudocode Refinement Step 1–The Problem Statement
This should be as short as possible, yet convey the full requirement of the program (however vague). Consider the following starting point for your stepwise refinement, which was pulled from the homework description.
“Given a target file to find and a starting directory, determine if and where the target file exists.”
Pseudocode Refinement Step 2
Using the above sentence as an incomplete guide split the embedded concepts into 4 distinct steps, such as:
- Setup & Initialization (for the program, or a specific function, similar to preconditions)
2. Input
3. Processing
4. Output
Pseudocode Refinement Step 3 to (N-1)
Using Step 2 above as a slightly more complete guide, split up the processing phase into more discrete steps required to searcha directory. For example, a start on the next step might be...
//just for the (3) step above (Processing))
while(more directories to look at) {
look at one file or directory and check for a match and return it if found
if the current item is a directory, we must repeat these steps for this directory as well
}
target not found at this point
(Spoiler Alert!)Stepwise Refinement, Step N
Compare your final stepwise refinement to the refinement below of the processing step we examined above. Does the logic appear similar? Submit your stepwise refinement as a separate file in your homework.
7.2 From Pseudocode to Java -File Search, Version 1.0
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
7.3 File Search, Version 2.0
We made use of an explicit stack to track new directories 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 java.util, we’ll 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 starter pseudocode approach below. Note that multiple distinct correct solutions exist to this problem, so don’t worry if your pseudocode isn’t exactly like the code below.
Hello, I have implemented the required file searching method, which will recursively search for a file starting from a given path, satisfying all the requirements in the question. I have written enough comments in every statements to let you understand how things work. Thanks.
// FileSearch.java
import java.io.File;
public class FileSearch {
public static void main(String[] args) {
System.out.println("File is being searched, please wait...");
// replace with your directory name and target file
System.out
.println(searchFiles(new File("D:\\work"), "marshmallow.txt"));
}
/**
* method to search for a file recursively
*
* @param path
* - starting directory
* @param target
* - target file name
* @return - 'Not a valid directory' if the path is invalid, 'Not found' if
* the file is not found, else will display the path in which the
* file is found
*/
public static String searchFiles(File path, String target) {
if (!path.isDirectory()) {
/**
* path is not a valid directory
*/
return "Not a valid directory";
} else {
/**
* listing all files in the current path
*/
File files[] = path.listFiles();
/**
* checking if the array is null. if the array is null, it means
* that there are no files in the path
*/
if (files != null) {
/**
* looping through all files
*/
for (File f : files) {
if (f.getName().equalsIgnoreCase(target)) {
/**
* found
*/
return "File " + target
+ " is found in the directory: " + path;
} else if (f.isDirectory()) {
/**
* it is a directory, so we perform an inner search and
* store the result in a variable
*/
String innerSearch = searchFiles(f, target);
/**
* if the result doesn't return 'Not found' then the
* file is found, so we will return the result (or else
* it will move on to the next file in the loop)
*/
if (!innerSearch.equalsIgnoreCase("Not found")) {
return innerSearch;
}
}
}
}
}
/**
* if the control reach here, it means the file is not found in the
* current path, so returning not found message
*/
return "Not found";
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- Can you help me with the pseudocode and not the actual code? I haven't yet reached there. Just the pseudocode, so I can understand better?arrow_forwardWrite the python3 code for the given problem.... Please write C++ code also.... It's urgent!!!!!!!!!!arrow_forwardWrite a C program for the image attached. Thank youarrow_forward
- The time complexity of this program is not O (log n). This actually looks like it was pulled from another website.. In other programming languages, we use a matrix (shown below) as a means of getting the Fibonacci nth number. We raise it to (n-1), though I don't know how to create this in Oz since I don't think it uses matrices. Can this be re-analyzed please? Thanks! 1 1 1 0arrow_forwardWrite a small piece of pseudo-code for generating a histogram H[L] for an intensity image I[YDIM][XDIM], where L is the total level of intensities of the image, H is the histogram array, XDIM * YDIM are the size of the image, and I[i][j] is the intensity value of the pixel at location (i,j), with i = 1, …, YDIM, and j = 1,…, XDIM. You might choose to use a certain program language that you are familiar with to write the code. You don’t have to compile the code.arrow_forwardIn the algorithmic world, the steps are very similar, but we can use simple loops to dowhat we are doing manually.This would translate to a set of steps as below:Step 1: Open a new spreadsheetStep 2: Create the header row in every worksheetStep 3: For each workbook from a storeFor each worksheet in the workbookRead rows 2 to last row that contains dataMove this row to the current rowAdvance to next rowNext worksheetNext workbookStep 4: Save workbook Write code for given algrithm.arrow_forward
- Debugging was performed, however the problem persisted. In this situation, what choices do you have?arrow_forwardFind on the internet (or use a camera to take) three different types of images: an indoor scene, outdoor scenery, and a close-up scene of a single object. Write python code to implement an adaptive thresholding scheme to segment the images as best as you can and write a brief summary.arrow_forwardThere are various sorting algorithms available to sort data of different sizes. Three of these algorithms are Bubble sort, Shell sort, and Quicksort. In Python, write a program to generate random integer numbers of multiple sizes; 10000, 30000, 40000, 50000, and 70000, and find out which of these sorting algorithms perform the fastest sorting technique. Provide data to prove and support your findings or results by plotting a graph showing the time each takes to sort data of various sizes. It should be written in a single program. Write the code in python and also show the graphical result. Also do the proper identation of the code.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education