1. (Find largest file) Write a program that finds the largest file in a directory. Pass the parameters from the command line as follows:
java Exercise18_01 dirName
If no directory is passed from the command prompt, prompt the user to enter it. Here is a sample run.
Sample run
Enter a directory: c:\book
The largest file is c:\book\image\world.gif 700,343KB
End Sample run
2. (0/1 knapsack problem) Given n items with weights w1, w2, …, and wn, and a bag with a weight capacity of w, the problem of finding the items with the maximum total weight to store in the bag is called the 0/1 knapsack problem. Let m(i, w) denote the total weight of the best solution of placing the first i items into a bag with weight capacity w. The problem can be solved using the following recursion:
m(0, weightLimit) = 0;
m(i, 0) = 0;
m(i, w) = m(i – 1, weightLimit); if wi > weightLimit
m(i, w) = max(m(i – 1, weightLimit), wi + m(i – 1, weightLimit – wi));
Write a recursive method for computing m(i, w) using following method header:
public static double m(int i, double weightLimit, double[] w)
where w is an a
ay of the weights for items. Write a test program that prompts the user to enter the number of the items and weight for each item and the weight capacity of the bag, and displays the maximum total weight of the items that can be placed in the bag. Here is a sample run:
Sample run
Enter the number of items: 5
Enter the weights for each item: XXXXXXXXXX
Enter the weight limit for the bag: 6
The maximum weight of the items placed in the bag is 6.0
End Sample run
Grading Criteria
Find Largest
1 Read Directory Name from command line
1 Read Directory Name from user if needed
1 Find and report largest size file
1 Documentation
Napsack Problem
2 Implement recursive function
1 Call function based on user inputs
1 Documentation