Question

Problem Description: Write a program that prompts the user to enter a directory and displays the...

Problem Description:

Write a program that prompts the user to enter a directory and displays the number of the files in the directory.

Analysis:

(Describe the problem including input and output in your own words.)

Design:

(Describe the major steps for solving the problem. How do you use recursion to solve this problem.)

Coding: (Copy and Paste Source Code here. Format your code using Courier 10pts)

Name the public class Exercise18_29

Testing: (Describe how you test this program)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Problem: To get count of files present inside a given directory path (including any of its sub-directories). Input to the program should be a valid directory absolute path. For every valid input, the program outputs the total count of files present inside the input directory path including any of its sub-directories). The count excludes directory count, if any.

Design: Below are the major steps in solving this problem using Java:

  1. Reading the directory path from user as input (text string).
  2. Determining if the input path is valid and is a directory.
  3. Creating a Java File object using given directory path to access required functions under File class.
  4. Using a function to iterate through sub-directories, if any, present in the input directory path and sum up the count of files in the directory tree.
  5. Print the total count of files.

Recursion is used under point 4 since we also want to include files inside sub-directories in the total count. A "for" loop iterates through the list of files/directories inside "getFilesCount" function. During iteration, if another directory is found, the function "getFilesCount" get called recursively and the whole thing repeats for list of files/directories found in the sub-directory.

Code:

import java.io.File;
import java.util.Scanner;
public class Exercise18_29 {
    public static void main(String[] args) {
        //Ask user to enter directory
        System.out.print("Specify a directory path:");
       //Read the user input
        Scanner input = new Scanner(System.in);
        String path = input.nextLine();
       File dir = new File(path);
       //Check if input path is of a directory (also is assumed valid)
       if(dir.isDirectory())
           System.out.println(getFilesCount(new File(path)) + " files");
       else
           System.out.println("Specified path is not a valid directory path.");
    }
   public static int getFilesCount(File file) {
       //Initialize a file object array for file/directory list
       File[] files = file.listFiles();
       //Count variable that will be returned finally to the caller
       int count = 0;
       //For loop to iterate through list of files/directories
       for (File f : files)
           if (f.isDirectory())
               //Recursive function call
               count += getFilesCount(f);
           else
               count++;
       //Return the count to the caller
       return count;
   }
}

Testing: For testing this code, below process is followed:

  1. Specified path at user prompt is of an empty directory. Output is 0 as there are no files.
  2. Specified path at user prompt is of an directory containing only a blank directory. Output is 0 as directories are excluded from count.
  3. Specified path at user prompt is of an directory containing some files with no directories. Output is equal to number of files present in the directory.
  4. Specified path at user prompt is of an directory containing some files with some blank directories. Output is equal to number of files present in the directory. No directory is included in final count.
  5. Specified path at user prompt is of an directory containing some files with some directories having files. Output is equal to number of files present in the directory plus the count of files present in its sub-directories.
  6. Specified path at user prompt is of a file (instead of a directory). Output displayed is "Specified path is not a valid directory path." since input path is of a file and not a directory and gets skipped.
  7. Specified path at user prompt is invalid (neither a directory nor a file). Output displayed is "Specified path is not a valid directory path." since input path is invalid and not a directory and gets skipped.
Add a comment
Know the answer?
Add Answer to:
Problem Description: Write a program that prompts the user to enter a directory and displays the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT