Question

In: Computer Science

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)

Solutions

Expert Solution

Answer) **No specific coding language mentioned, hence providing the Answer in Java.**

Code:

import java.io.File;
import java.util.Scanner;

//GetFiles Class
public class GetFiles {
   public static void main(String[] args) {
       //scanner instance to take input from user in console
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter path : ");
       //taking input
       String path=sc.next();
      
       //calling getFileInPath() method to get the number of files in the path and print it.
       System.out.println("No. of files in path -- "+path+" is : "+getFilesInPath(path));

       //closing scanner instance
       sc.close();
   }
   //private static method to get number of files in a given path
   private static int getFilesInPath(String path) {
       //new File(path) returns a directory,
       //.list returns a Array of Strings for each file in the path,
       //.length return the length of the array
       return new File(path).list().length;
   }
}

Output:

Enter path :
C:\sd
No. of files in path -- C:\sd is : 4


Related Solutions

JAVA Write a test program that prompts the user to enter a list and displays whether...
JAVA Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the program first prompts the user to enter the size of the list. Using Array My output should look like this Enter the size of the list: 8 Enter the contents of the list: 10 1 5 16 61 9 11 1 The list has 8 integers 10 1 5 16...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum and secondNum. The program then prints all palindromic primes (Links to an external site.) between firstNum and secondNum, inclusive. A palindromic number is a number whose digits are the same forward or backward (e.g., 12321). A palindromic prime is a prime number that is also a palindromic number (e.g., 101). You must implement and use the following functions as prototyped below: /// --------------------------------------------------------------- ///...
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to...
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is a number character 1, 2, 3 or 4, which indicates whether a student is a freshman, a sophomore, junior or senior. Suppose the following characters are used to denote the majors: M: Mathematics C: Computer Science I: Information Technology Here is...
Write a java program to let the user enter the path to any directory on their...
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT