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.
?
![public static void main(String[] args) { // Prompt the user to enter](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2022/11/636a73c0425f5_832636a73c032430.jpg)
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
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
Get step-by-step solutions from verified subject matter experts
