Problems
power
Question 1: Write a public static function named power that takes in two integers, a and b, and returns a^b, the first argument to the power of the second argument. You may NOT use Math.pow(), because that would be boring.
Question 2: The base case in this method is when the second argument is 0: n^0 = 1, by definition.
Hint: 3^5 = 3*(3^4)
Question 3: Why is recursion not the best solution for this problem? If you do not know, ask a TA.
Question 4: Create a similar method named power2 that is functionally identical but does not use recursion. It should still NOT use Math.pow().
fileCount
Question 1: Write a public static function named fileCount that takes as input a directory (as a File object) and returns the total number of files in all sub-directories
Directories count as files, too.
Question 2; Your method should look through all files and sub-directories in the directory that was passed in. While searching through the directory, use the java.io.File.isDirectory() method to check if the File you are looking at is a file or directory. If it is a normal file, count it. If it is a directory, count it and use a recursive call to count its contents.
Hint:
- What is the base case?
- What is the recursive step?
- What is the combination?
- You should consult the java.io.File API for more information.