Question: Rewrite Programming Exercise 18.28 using a stack instead of a queue. DirectorySize.java, without using recursion. ? 1 import java.io.File; 2 import java.util.Scanner; 3 4 public

Rewrite Programming Exercise 18.28 using a stack instead of a queue.

DirectorySize.java, without using recursion.

1 import java.io.File; 2 import java.util.Scanner; 3 4 public class DirectorySize {?

public static void main(String[] args) { // Prompt the user to enter

1 import java.io.File; 2 import java.util.Scanner; 3 4 public class DirectorySize { public static void main(String[] args) { // Prompt the user to enter a directory or a file System.out.print("Enter a directory or a file: "); Scanner input = new Scanner (System.in); String directory = input.nextLine(); 5 10 11 12 13 // Display the size System.out.println(getSize(new File(directory)) + " bytes"); 14 public static long getSize(File file) { long size = 0; // Store the total size of all files 15 16 17 18 19 20 21 22 23 24 25 26 if (file.isDirectory()) { File[] files = file.listFiles(); // A1l files and subdirectories for (int i = 0; files != null & i < files.length; i++) { size +- getSize(files[i]); // Recursive call else { // Base case size +- file.length(); 27 28 return size; 29 30 }

Step by Step Solution

3.38 Rating (160 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program Plan Create a class FileSizeStack Create an object of Scanner class and name it input Create a String object myDirectory and store the directo... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!