Question

In: Computer Science

Problem 4: Coins Used java please You are given a target value in cents and some...

Problem 4: Coins

Used java please

You are given a target value in cents and some coins: quarters, dimes, nickels, and pennies (in that order). Your program should tell which coins add up to that target.

input:

63 3 2 5 3

output:

63 cents = 2 quarters, 1 dimes, 0 nickels, 3 pennies

Hints:

Your recursive function can have ten int parameters: targetValue, valueSoFar, quartersLeft, dimesLeft, nickelsLeft, penniesLeft, quartersSpent, dimesSpent, nickelsSpent, penniesSpent.

Base cases:

1. you hit the target value. print how many of each coin type you used and return

2. you're over the target value. return.

Recursive cases

            Handle the four coin types in order: quarters, dimes, nickels, pennies

            If you have quarters, and a quarter won't put you over the top, call the recursive function, adjusting valueSoFar, quartersLeft, and quartersSpent appropriately

            Do the same for the other coin types

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You.


FindNumberOfChangeRecurse.java

package c8;

public class FindNumberOfChangeRecurse {


   void calcualteChange(int targetValue,int valueSoFar,int quartersLeft,int dimesLeft,int nickelsLeft,int penniesLeft,int quartersSpent,int dimesSpent,int nickelsSpent,int penniesSpent) {
       if(valueSoFar==targetValue) {
           System.out.printf("%d cents = %d quarters, %d dimes, %d nickels, %d pennies",
                   targetValue,quartersSpent,dimesSpent,nickelsSpent,penniesSpent);
       }else if(valueSoFar<targetValue) {
           int required = targetValue - valueSoFar;
           /* System.out.println("Required is : "+required); */
           if(quartersLeft>0 && required>=25) {
               valueSoFar += 25;
               quartersSpent++;
               quartersLeft--;
           }else if(dimesLeft>0 && required>=10) {
               valueSoFar += 10;
               dimesSpent++;
               dimesLeft--;
           }else if(nickelsLeft>0 && required>=5) {
               valueSoFar += 5;
               nickelsSpent++;
               nickelsLeft--;
           }else if(penniesLeft>0 && required>=1) {
               valueSoFar += 1;
               penniesSpent++;
               penniesLeft--;
           }else {
               System.out.printf("Error!!! There is not enough coins for exact change of %d!!!",targetValue);
               return ;
           }
           calcualteChange( targetValue, valueSoFar, quartersLeft, dimesLeft, nickelsLeft, penniesLeft, quartersSpent, dimesSpent, nickelsSpent, penniesSpent);
       }
   }


   private void calcualteChange(int i, int j, int k, int l, int m) {
       calcualteChange(i,0, j, k, l, m,0,0,0,0);
   }

   public static void main(String[] args) {
       FindNumberOfChangeRecurse changeFinder = new FindNumberOfChangeRecurse();
       changeFinder.calcualteChange(63, 3, 2, 5, 3);
   }


}

outputs


Related Solutions

Abox containing pennies, nickels, and dimes has 13 coins with a total value of 83 cents....
Abox containing pennies, nickels, and dimes has 13 coins with a total value of 83 cents. How many coins of each type are in the box? Is the economy productive? Using (gauss elminations)
Problem 4) Five coins are flipped. The first four coins will land on heads with probability...
Problem 4) Five coins are flipped. The first four coins will land on heads with probability 1/4. The fifth coin is a fair coin. Assume that the results of the flips are independent. Let X be the total number of heads that result. (hint: Condition on the last flip). a) Find P(X=2) b) Determine E[X]
In java please A boolean must be used for this problem! Directions: * Initialize a Boolean...
In java please A boolean must be used for this problem! Directions: * Initialize a Boolean variable "first" to true. * Repeat (another value has been read successfully) *        If first is true *            Set the minimum to the value. *            Set first to false. *        Else if the value is less than the minimum *            Set the minimum to the value. * Display the minimum. So...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a word and print out every combination of letters in that word. For example, "sam" would print out sam, sma, asm, ams, mas, msa (the ordering doesn't matter) input: cram output: cram crma carm camr cmra cmar rcam rcma racm ramc rmca rmac acrm acmr arcm armc amcr amrc mcra mcar mrca mrac macr marc Hints: Your recursive function should have no return value and...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false. Also write a program to test your method. 2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
Please answer in c++ 6.Define a function to find a given target value in an array,...
Please answer in c++ 6.Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible. 7.Write a swap function, that swaps the values of two variables in main, but use pointers instead of reference parameters. 8.Write a function that takes an array of ints and its size as arguments. It should create a new array that is the same size as the argument. It should set the values in...
C++: A string construction problem defined as follows: - You are given as input a target...
C++: A string construction problem defined as follows: - You are given as input a target string - Starting with an empty string, you add characters to it, until your new string is same as the target. You have two options to add characters to a string: - You can append an arbitrary character to your new string, with cost x - You can clone any substring of your new string so far, and append it to the end of...
In exercises 1–4, verify that the given formula is a solution to the initial value problem....
In exercises 1–4, verify that the given formula is a solution to the initial value problem. 2. Powers of t. b) y ′ = t^3 , y(0) = 5: y(t) = (1/5)t^(4) + 5 3. Sines and cosines. a) x′ = −y, y′ = x, x(0) = 1, y(0) = 0: x(t) = cost, y(t) = sint
In java please Question: You are given a string s. Your task is to count the...
In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:
Target ROE problem You are given the following information regarding KTC for 2019: RETURN ON ASSETS...
Target ROE problem You are given the following information regarding KTC for 2019: RETURN ON ASSETS = 7.5% NET PROFIT MARGIN = 6.0% DEBT EQUITY RATIO = 1.5x SALES = $550,000.00 GROSS PROFIT RATE = 50.0% TAX RATE = 34.0% 1) What must KTC project as its 2020's sales in order to generate an additional 5% Return on Equity above last year’s levels (2019’s ROE + 5%, not 2019’s ROE x 105%) 2) Prepare a projected 2020 Profit and Loss...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT