Question

Already been answer on the website, but I didn't understand the answer. I would appreciate a...

Already been answer on the website, but I didn't understand the answer. I would appreciate a better explanation, thank you:

Consider an algorithm in Java for determining the size of some nested folders/files recursively. (Imagine right clicking an item on your desktop and viewing it's size.) What would be the base case(s) for this problem?

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

Code :

public static long folderSize(File directory)
{
     long length = 0;
     for (File file : directory.listFiles())
     {
          if (file.isFile())
               length += file.length();
          else
               length += folderSize(file);
     }
     return length;
}

Algorithm :

  • Check the lists of files by using for (File file : directory.listFiles()).
  • Then check if the file if present by using if (file.isFile()).
  • If it returns true add the length of this file to final length,
  • Else the current item is a folder and hence recursively check for the sub folders/files of the current folder.

Here the base case if when the current folder is empty or when all the files/folders have been examined of the current folder.

******************NOTE******************

If there is any problem with the code.. or If you have some doubts... please ask in the comments. :)
If everything is good, please rate positively ... as it directly affects our payscale :)

P.S. If you need the explanation of the code answered to you in your question, please provide the code or the screenshot of the code.
Add a comment
Know the answer?
Add Answer to:
Already been answer on the website, but I didn't understand the answer. I would appreciate a...
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
  • a) Consider an algorithm for determining the size of some nested folders/les recursively. (Imagine right clicking...

    a) Consider an algorithm for determining the size of some nested folders/les recursively. (Imagine right clicking an item on your desktop and viewing it's size.) What would be the base case(s) for this problem? b) Consider the problem of reversing letters of a string. Would it be more appropriate to use recursion or iteration? Justify your answer. [8 points] Short Answer:

  • This question has already been answered however I do not understand the particulars of the answer....

    This question has already been answered however I do not understand the particulars of the answer. for an example why are we importing an average class I thought that we could solve the problem just inputting the scanner and doing the import command at the top. Also at the bottom of the problem they increment the counter I thought you increment the counter at the top. Is there any way someone could do a detailed step-by-step explanation of the actual...

  • Appreciate if you can answer this ONE QUESTION COMPLETELY and give me a detailed working with...

    Appreciate if you can answer this ONE QUESTION COMPLETELY and give me a detailed working with explanation for me to understand. Once completed so long as my doubts are cleared and the solutions are correct, I will definitely vote up. Some of the question are similiar to take a look carefully before you answer as it's very important for me. Thank you. Question 5 The management team of a US-based airline is considering making changes to its checked-in baggage allowance...

  • I did not completely understand the explanation or how to get to this answer, I would...

    I did not completely understand the explanation or how to get to this answer, I would really appreciate if it could be drawn out and explained more thoroughly. Thanks! air medium A beam of light shines into a transparent medium with parallel surfaces. Part of the beam is reflected back into the air as diagrammed above. (The figure is NOT to scale.) The index of refraction of the medium is 1.5. Which of the following is true? A. θ <...

  • Hello, please help me to answer my assignment, i highly appreciate your effort, kindly explain it...

    Hello, please help me to answer my assignment, i highly appreciate your effort, kindly explain it clearly. Kindly put your codes in (NOTEPAD). Thank you very much. Write a VB application that will facilitate a pizza order. 1.The user interface should have a form containing all components pertaining to pizza order presented in either option buttons or check boxes. 2.Controls must also be grouped in frames according to size, toppings, or crust type. 3.The summary of order must be presented...

  • I uploaded this question once already and got no answer so I would really appreciate it...

    I uploaded this question once already and got no answer so I would really appreciate it someone answered this bc I pay for this service. Thanks. i need all of it to check my work however im stuck figuring out the numbers for office equipment and computer equipment We were unable to transcribe this imageDec. 2 Paid $950 cash to Hillside Mall for Business Solutions' share of mall advertising costs. 3 Paid $450 cash for minor repairs to the company's...

  • i would really appreciate some help with this differential analysis! any correct answer will definitely recieve...

    i would really appreciate some help with this differential analysis! any correct answer will definitely recieve a thumbs up! thank you of Accounting Section 1 20. CengageNOWV2 Online teaching and learning resurrom quired: Chapter 24 Homework & Apf Calculator Print Item Continue/Discontinue WoolCorp Condensed Income Statement For the Year Ended December 31, 2048 Raw Wool Wool Yam $210,000.00 $155,000.00 Rug Yarn Total Company $532,000.00 2 Sales $167,000.00 Cost of goods sold Variable costs $48,000.00 $103,780.00 69,200.00 Fixed costs 32,000.00 518,600.00...

  • Please help me understand this question. If you could answer and explain I would really appreciat...

    please help me understand this question. If you could answer and explain I would really appreciate it. Thank you! or a galvanometer) is connected to a coil of wire. When there is no as the curremeter stays at the middle (zero current location) when coil current, indicator of th e current left to right ammeter scales to the right and when current flows righ oppositely it scales to the left. 15 points 1. If end is near the coil and...

  • I don't understand what calculations I would have made to create the buffer solution from Part...

    I don't understand what calculations I would have made to create the buffer solution from Part D of the lab. I understood the procedures I took to create the solution but when the report asks to show the calculations used to prepare the buffer solution I am not sure other than mixing the volumes indicated in the procedures section. Thank you pH of Butter Assigned by Instructor 5.12. Measured pH of Assigned B r .9 (Read the procedural Show the...

  • For each question below select the best answer from those listed and give your reasoning. Your...

    For each question below select the best answer from those listed and give your reasoning. Your reasoning need only be a sentence or two. It is not enough to get the right answer, you must know why it is the right answer. Question 5 Fred's friend claimed that Canadians tend to be jerks. Fred wondered if that was true, and tested it by checking to see how many Canadian jerks he could think of. Fred's cognitive strategy is             ["the availability...

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
ADVERTISEMENT