Question

In: Computer Science

You are implementing a brand new type of ATM that provides exact amounts of cash (bills...

You are implementing a brand new type of ATM that provides exact amounts of cash (bills only, no coins). In order to maximize potential revenue an algorithm is needed that will allow for using different denomination amounts as needed by the local currency (value of bills will vary, but are integers).

  1. The program shall graphically prompt the user for a file.

  2. The program shall read the selected file of which the first line will be a space separated list of the

    bill denomination sizes.

  3. The program shall then read each line in the rest of the file containing an integer and output the

    number of different ways to produce that value using the denominations listed in the first line.

  4. The program shall indicate after that the number of milliseconds the program spent calculating

    the answer.

  5. The program shall implement 2 different forms of the algorithm: 1 recursive and 1 using dynamic

    programming.

  6. The program shall have 2 sets of output, 1 for each implementation.

  7. The program shall write the output to a file in the same directory as the input file.

  8. The program must be written in Java.

Solutions

Expert Solution

import java.io.*;
import java.util.*;
class Main {

    public static int calculate_dp_bottomup_approach(int denominations[], int amount) {
        // Dynamic programming approach
        int dp[] = new int[amount + 1];
        dp[0] = 1;
        for (int i = 0; i < denominations.length; i++)
            for (int j = denominations[i]; j <= amount; j++)
                dp[j] += dp[j - denominations[i]];
        return dp[amount];
    }

    public static int calculate_recursive(int denominations[], int index, int amount) {
        // recursive brute force approach
        if (amount == 0)
            return 1;
        if (amount < 0 || (index < 0 && amount >= 1))
            return 0;
        return calculate_recursive(denominations, index - 1, amount ) +
               calculate_recursive(denominations, index, amount - denominations[index] );
    }
    public static void main(String...args) throws Exception {
        System.out.println("Please enter file path");
        Scanner input = new Scanner(System.in);
        String path = input.next();
        File file = new File(path);
        while (!file.exists()) {
            System.out.println("File path entered is incorrect. Please specify correct path");
            System.out.println("Note enter file path in forward slash notation or use two back slash notation");
            file = new File(input.next());
        }
        Scanner readFile = new Scanner(file);
        PrintStream output = new PrintStream(new File(file.getParent() + "/output.txt"));
        System.setOut(output);
        String[] denominationsString = readFile.nextLine().split(" ");
        int []denominations = new int[denominationsString.length];
        for (int i = 0; i < denominationsString.length; i++)
            denominations[i] = Integer.parseInt(denominationsString[i]);
        long begin, end, duration1, duration2;
        int ans1 = 0, ans2 = 0;
        while (readFile.hasNext()) {
            int amount = readFile.nextInt();
            begin = System.currentTimeMillis();
            ans1 = calculate_recursive(denominations, denominations.length - 1, amount);
            end = System.currentTimeMillis();
            duration1 = end - begin;

            begin = System.currentTimeMillis();
            ans2 = calculate_dp_bottomup_approach(denominations, amount);
            end = System.currentTimeMillis();
            duration2 = end - begin;
            System.out.println("For amount " + amount);
            System.out.println("Using recursion number of ways is " + ans1 + " and time taken is " + duration1 + " ms");
            System.out.println("Using dynamic programming number of ways is " + ans2 + " and time taken is " + duration2 + " ms");
            System.out.println();
        }

    }
}

Sample Input:

path provided :  C:/folder1/folder2/input.txt

input.txt

2 3 4 5 6 7 8 9
1
4
5
6
7
8
100

output.txt

For amount 1
Using recursion number of ways is 0 and time taken is 0 ms
Using dynamic programming number of ways is 0 and time taken is 0 ms

For amount 4
Using recursion number of ways is 2 and time taken is 0 ms
Using dynamic programming number of ways is 2 and time taken is 0 ms

For amount 5
Using recursion number of ways is 2 and time taken is 0 ms
Using dynamic programming number of ways is 2 and time taken is 0 ms

For amount 6
Using recursion number of ways is 4 and time taken is 0 ms
Using dynamic programming number of ways is 4 and time taken is 0 ms

For amount 7
Using recursion number of ways is 4 and time taken is 0 ms
Using dynamic programming number of ways is 4 and time taken is 0 ms

For amount 8
Using recursion number of ways is 7 and time taken is 0 ms
Using dynamic programming number of ways is 7 and time taken is 0 ms

For amount 100
Using recursion number of ways is 212713 and time taken is 31 ms
Using dynamic programming number of ways is 212713 and time taken is 0 ms


Related Solutions

You are the Brand Manager of a new yoghurt brand ‘Yo-Yum’ (please note: this is a...
You are the Brand Manager of a new yoghurt brand ‘Yo-Yum’ (please note: this is a fictional brand) that will compete with the market leader Yoplait. With reference to evoked sets and levels of decision-making, consumer involvement, perceived risk, and learning strategies, provide some strategies and tactics that could be used by Yo-Yum to trial their new brand.
You own 2 restaurants serving the exact same type of food.   The menus of meals and...
You own 2 restaurants serving the exact same type of food.   The menus of meals and drinks served are exactly the same. Suppose that the costs of each restaurant are exactly the same. Suppose further that the number of potential customers for each restaurant is exactly the same. One restaurant is located in a wealthier section of town; one restaurant is located in a poorer section of town. Should each restaurant have the same prices on their menus or different...
Identify a problem at a company that you feel can be improved by implementing a new...
Identify a problem at a company that you feel can be improved by implementing a new process or technology. Note: This must be a real company and a real process that you feel needs improvement but the focus should NOT be on the company but rather on the process to be improved. Note: You may want to start with either a problem that you have identified at a company OR a technology advancement or tool that you feel could help...
Given the two brand names Pepsi and BMW, choose two potentially new brand extensions that you...
Given the two brand names Pepsi and BMW, choose two potentially new brand extensions that you believe will make sense and two that do not for each of them. Explain why?
of the eight steps in implementing a new AIS, which do you feel is the most...
of the eight steps in implementing a new AIS, which do you feel is the most important and why?
1.Suppose you are implementing a new employee developmental plan. You are requested to design a complete...
1.Suppose you are implementing a new employee developmental plan. You are requested to design a complete form that you would use to implement this new program. Ensure that it contains all of the elements of a good developmental plan including employee’s personal particulars. 2.Now you are required to establish a personal developmental plan for yourself by filling ALL required information in the form that you have designed in Task 1. In your personal developmental plan, you have to cover the...
You are interested in buying a brand new Jalopy and expect the purchase price to be...
You are interested in buying a brand new Jalopy and expect the purchase price to be $19,000. The car dealership can offer financing at a 6% interest rate over 6 years. If you put $1,000 down towards the purchase and accept the financing terms,what will your monthly payment for the loan be? Please Show the excel formula
A firm considers taking on a new project which will cost EUR 342 and provides cash...
A firm considers taking on a new project which will cost EUR 342 and provides cash flows of EUR 107,109,119,84 in the next 4 years. What is the internal rate of return of the project?
You are about to purchase a brand new car for $23.400. You have $14.400 to put...
You are about to purchase a brand new car for $23.400. You have $14.400 to put down and will need to finance the rest. The dealership has two options. 1) Full price of the car and a loan of 0% interest for 3 years. 2) $1000 off the price of the car (known as cash back) and a loan for the rest with an interest rate of 5.9% for 3 years Which is the better option? (you must show work...
Pretend you are the coordinator in a brand new home for late adults. How would you...
Pretend you are the coordinator in a brand new home for late adults. How would you set the home up so that the residents are getting their physical, cognitive, and psychosocial needs met? What would the structure of the home be like? What kinds of activities would you implement, if any? What kinds of social interactions would you promote? What would you want to make sure to emphasize? What might some of your concerns be?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT