Recursive Method
KevinSarav
Posted on April 22, 2019
In this post, I will go over a project I just completed. It uses recursion to find out the different combinations of coins that can be done from a given number of cents. I will be going over the challenges from this project.
My first stumbling stone with this project is getting the recursion to work. For some reason, every time I tried to do recursion, I end up in an infinite loop even though I tried to make sure the recursion included a decremented value. I concluded this might be because Java is pass by value and not pass by reference since I was trying to use a value taken in from outside the method. I solved this by just making an object with my class and using that instead.
My next stumbling block is how to go about the program. The first logical step is to split up the coins in a way a logical person would. You would want the more valuable coins first. For example, you would choose to get 1 quarter instead of 2 dimes and 1 nickel. I made a separate method that aimed to do just that. On top of that, I used recursion for that method as well.
The main stumbling block with this logic is what to do after that point. Logically, you'll want to turn nickels to pennies first, and then turn dimes to nickels, and then quarters to dimes, and so on and so on. I made a method that did just that. The method worked for everything under 25 cents, but anything greater than 25 cents caused me to get wrong amounts of combinations. I concluded this may be because of my logic on how to deal with quarters. I initially made it so that once the dimes and nickels have been turned to pennies, then I decrement the quarters by 1, add 2 dimes and 1 nickel, logically. However, my program failed to account for values bigger than the current amount of dimes. For example, if I turn 1 quarter into 2 dimes and 1 nickel, and there's 17 pennies on the side, I am forgetting the combination of having 4 dimes and 2 pennies. My logic would never bring me back to that higher dime value and instead, I'm left with smaller values for dimes and nickels.
To solve this, I first added 25 pennies to the amount of pennies so that I can turn the pennies so far into dimes first, and then nickels. This way, I'm accounting for combos with the higher amount of dimes. Then I add the result of the pennies divided by 10 and then I turn pennies into the modulus of pennies so I get the remainder. Then I use the same logic for nickels and then finally decrement quarters by 1.
I revisited my first stumbling block and was able to do recursion without an object for some reason. It may be because my variable names were the same, which might have confused my program. Regardless, I made the program work without objects at the end.
In my next post, I hope to talk about my progress on my calculator project and advancements in my group project for my master's class. Now that the semester is ending, that group project will soon be due and I will need web development skills to succeed. I'm currently learning Angular on YouTube hoping it'll give me a head start.
Posted on April 22, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.