Could someone help with recursion?
Make change for a dollar amount 0 - 999 using $100 bills, $20 bills, $5 bills and $1 bills with the total number of bills being as small as possible using a recursive algorithm. Now writing this non-recursivly is super easy, however the assignment calls for a recursive impementation. I can not for the life of me figure it out. I know the problem with my algorithm is that I keep making new instances of Change, but i can't figure out a way to keep track of the previos instances and combine totals. Heres what I have so far
* Change for a dollar amount.
*require:
* 0 <= amount && amount <= 999
static public Change changeFor(int amount){
Change c = new Change();
if (amount == 0 ){
c.setHundreds(0);
c.setTwenties(0);
c.setFives(0);
c.setOnes(0);
if (amount == 100){
c.setHundreds(1);
}else if(amount >100){
c.setHundreds(c.hundreds() +1);
return changeFor(amount%100);
if (amount == 20){
c.setTwenties(1);
}else if(amount > 20){
c.setTwenties(c.twenties() +1);
return changeFor(amount%20);
if (amount == 5){
c.setFives(1);
}else if(amount >5){
c.setFives(c.fives() +1);
return changeFor(amount%5);
if (amount == 1){
c.setOnes(1);
}else if (amount >1){
c.setOnes(amount);
return c;
return c;
The method must be static. any help would be greatly apreciated.
Joey
Thanks for the help but im still not there yet. the assignment also says to define a class Change with properties for the number of 100 bills, 20 bills 5 bills and 1 bills. I can't access these variables from a static method. The only solution I can see is to make a new Change instance in my method, but since i can't pass the old change instance into the method i can't see how its possible to keep track of the count of each bill. I was able to make the algorithm work recursivly in 2 ways. One of the ways i removed the static scope. The other way was to make the Change properties for the number of bills static. Both ways are not good enough, heres why. The professor wants the method to work with his tester file that looks like this:
for (int amount = 0; amount <= 999; amount = amount + 77)
System.out.println("Change for dollar amount " + amount + " is " + Change.changeFor(amount));
Therefor when i make the scope of the property values static, this loops keeps adding the old bill numbers from the pervious amount to the new bill numbers, which really throws off the count =P. When the method is not static, the compiler catches a fit because there was no instance of Change created in the tester file. heres a working version of the static method with static property values:
* Change for a dollar amount.
*require:
* 0 <= amount && amount <= 999
public static Change changeFor(int amount){
if (amount >= 100){
amount-=100;
hundreds+=1;
return changeFor(amount);
if (amount >= 20){
amount-=20;
twenties+=1;
return changeFor(amount);
if (amount >= 5){
amount-=5;
fives+=1;
return changeFor(amount);
if (amount >= 1){
amount-=1;
ones+=1;
return changeFor(amount);
and the class defines static private int hundreds, twenties, fives, ones;
the non-static versions of the method is about the same except i use this.object instead of the static scope properties. any help more help is apreciated greatly and thanks to those who have helped some so far.
joey
Similar Messages
-
Could someone help with a software to create android apps on mac
Hi
Could someone help with a software to create android apps on mac.
ThanksFound this on the Google:
http://beta.appinventor.mit.edu/learn/setup/setupmac.html
Know nothing of it. -
My Ipad reboots over and over again could someone help with my problem?
My Ipad reboots over and over again, could someone help with my problem?
Try a reset. Hold the Sleep and Home button down for about 10 seconds until you see the Apple logo.
-
I am using BB8820, when I reinstalled my computer system, one problem occurred to my blackberry desktop manager. when I connect my BB to computer,there is no response with blackberry desktop manager. And when I open the BB device manager ,it showed me that LIBBBLAUNCHAGENT.DLL is to be needed. So I do not have LIBBBLAUNCHAGENT.DLL in my computer.
Could someone pls tell me where can I download this file? I would really appreciate.I have the same issue after updating Blackberry Desktop software......can anyone help please?
iPhone for fun, Blackberry for Work........
I'm not looking for Kudos but if my post helped then give me the Kudos and maybe it will help others -
Could someone help with issue please
Hello
I have posted this issue twice. I desperately need help with this.
I have installed j2sdk 1.4.1 first and then installed SunONE Community Edition on my XP Home Edition laptop. I am able to write and run programs. But when I close SunONE, it crashes XP. I cannot bring up the task bar to kill the processes or does the CTRL+ALT+DEL work.
The same problem happens when I tried with Netbean 3.5.I can't give you an answer, but I can help you find one:
o) You don't need to say hello, we assume that you're there seeing as you made the post
o) Don't cross / multi post
o) Use a better title - pretty much everyone here wants help with an issue
o) http://www.google.com
http://www.mektrix.2ya.com/answers.html -
Could someone help with question about PNG export from Illustrator?
I am having issues with PNG exports from Illustrator not opening up once exported.
ANd exactly sorry, this is useless. You have not provided any details about your export settings, version of AI, system info, how you are trying to view and so on. Just saying that it doesn't work is simply not good enough.
Mylenium -
Hi, could someone help with append message TextArea????
// a program by me
// a class MailOrder
// java core packages
import java.text.NumberFormat;
import java.util.Locale;
// java extension packages
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
public class MailOrder {
public static void main( String args[] )
int product;
double amount, product1 = 2.98, product2 = 4.50, product3 = 9.98, product4 = 4.49, product5 = 6.87;
String input;
// create DecimalFormat to format floating-point numbers
// with two digit to the right of the decimal point
NumberFormat moneyFormat =
NumberFormat.getCurrencyInstance( Locale.US );
JTextArea outputTextArea = new JTextArea();
input = JOptionPane.showInputDialog(
"Enter the product number" );
product = Integer.parseInt( input );
switch ( product ) {
case 1:
amount = product1;
case 2:
amount = product2;
case 3:
amount = product3;
case 4:
amount = product4;
case5:
amount = product5;
case6:
amount = product1 * product2 * product3 * product4 * product5;
break;
outputTextArea.append( product + "\t" +
moneyFormat.format( amount ) + "\n" );
} // end switch structure
JOptionPane.showMessageDialog(null, outputTextArea,
"The total retail prices", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
If anyone can explay why the append code aren't working.
The compiler displays this message.
unreachable statement
outputTextArea.append( product + "\t" +
^
Tnx
regards
giuseppeHi,
You need to put the statement
outputTextArea.append( product + "\t" + moneyFormat.format( amount ) + "\n" );
outside the switch case loop and then initialise amount variable.
-Amol -
Could someone help with some pathfinding game code using Director please?
Hey guys, i`m new to director and am trying to create a very basic graffiti game with pathfinding code. any links to online tutorials or code sites would be much appreciated! thanks in advance!
I don't understand how pathfinding relates to graffiti, but here is a Lingo implementation of the A* algorithm
-
I have a Mac OS X 10.6 When I try to send an attachment with email it won't send - keeps saying 'fail'. Please could someone help? Thank you.
Hi lllaass! I replied to your post with the information you required but haven't heard from you since. If you don't, or can't help any further please would you let me know. Thanks.
-
Could someone help me with instructions for doing a clean install for Could someone help me with instructions for doing a clean install for an early 2009 iMac 20" with Mountain Lion 10.8.4 ?
Thank You,
leetruittBarney, William, nbar, Galt\'s Gulch:
A nephew stayed with us to take a course at the local community college last semester. I allowed him use of my mac for research just after I installed Mountain Lion. He decided to "help out" and ran a 3rd party clean and tune up app., Magician. The tune up wiped over 1200 titles of my music in iTunes, also a path of folders with names containing nothing but more empty folders with names. Almost every program I open and run for a short time crashes, also folders and programs are in wrong places, or cannot be located (the nephew, for instance). Up to this time I have always been able to track a problem far enough to resolve it by careful persistence and good luck, but I do not have the tech savvy to solve this one without getting in deeper. I have run disk utility saveral times and always get variations of this:
ACL found but not expected on
"private/etc/resolve.conf"
"private/etc/raddb/sites-enabled/inner-tunnel"
"private/etc/rabbd/sites-enabled/control-socket"
Also, after four years of intense daily use it is cluttered with much junk, and every app that I was curious about, and I am a very curious guy.
So I know my limits, (and now my nephew). I dumped my pc for a Strawberry iMac a few years ago, and so far every problem I've encountered, or brought on myself, the Mac has resolved with its own inner elegance - in spite of my novice bungels - and now I honestly feel ashamed, in addition to the need to apologize to a machine for allowing a peasant into the Royal Grounds - so I am calling the Wise Ones in for this one.
Crazy, I know, but to me a Mac represents something far beyond the ordinary input ➛ process ➛ output title/function customarily assigned to a machine - I sense an odd kind of morality involved, one that I need to respect.
So, these are my reasons for wanting to do a clean install, correctly.
Thank you, Truitt -
Hello, could someone help me with lightroom 6. I can't seem to buy the upgrade from lightroom 5 to six on my desktop? Thanks.
Products
if that link doesn't work for you contact adobe support by clicking here and, when available, click 'still need help', https://helpx.adobe.com/contact.html -
Could someone help me with comparing the contents of 2 folders?
I'm trying to compare my iTunes music folder which is located in it's default location with a external USB hard drive. I was reading this article on how to do it using Terminal but I have NO CLUE on what to type in the command line. I'm clueless on this! Could someone help me with the commands I need to compare the 2 folders? I've included the link about what I'm trying to accomplish.
http://www.macworld.com/article/132219/2008/02/termfoldercomp.htmlI tell you how. But remember that Terminal isn't a good place to be kicking around if you're not sure of what your doing. You can issue many commands as root that are irreversible. Tread very carefully, or get yourself a good GUI utility that can do that.
It's asking you to cd (change directory to the folder that your files for comparison are in.
So, you type, cd (leave a space) then you can just drag the folder to the Terminal and it will fill in the rest of the path.
Then press Return
Then type this at the prompt:
diff -rq folder1 folder2
(replacing "folder1 folder2 with the name of your folders. Leave spaces where indicated.
Press return.
-mj -
Hi im having trouble with downloading an album off my computer. i have forgotten the security questions, but have already sent them to my email. could someone help me in how to get the answers?
Hello Carpets,
Thanks for the question. You can reset your security questions with your rescue email address, as outlined with this article:
Apple ID: All about Apple ID security questions
http://support.apple.com/kb/HT5665
If you do not see the option for resetting your questions with a rescue email address, see this excerpt:
Note: The option to send an email to reset your security questions and answers will not be available if a rescue email address is not provided. You will need to contact iTunes Store support in order to do so.
Rescue email address and how to reset Apple ID security questions
http://support.apple.com/kb/HT5312
Additional Information:
Apple ID: Contacting Apple for help with Apple ID account security
http://support.apple.com/kb/HT5699
Thanks,
Matt M. -
Hi, I bought the programme DocumentsToGo-Suite; I don't find where the software was loaded with my ITunes ; could someone help me , please ?
This link may be of help -
http://download.dataviz.com/pdf/quickstartbooklets/DXTG9-GSM_E.pdf -
Whenever I send a message to another iPhone user via iMessage it is sending my Apple ID instead of my phone number. Could someone help me with this???
Check this article: iOS: About Messages
Additional Information
You can change your iMessage Caller ID setting on iOS devices in Settings > Messages > Receive At > Caller ID. Note that the Caller ID setting is used only for new conversations. If you would like to change the address from which messages are sent, first change your Caller ID, and then delete the existing conversation and start a new one.
iMessage responses will be sent from the address the recipient most recently messaged. For example, on iPhone you can receive messages sent to your Apple ID and phone number. A friend sends you a message to your Apple ID. Responses in this conversation will be sent from your Apple ID, even if your Caller ID is set to your phone number.
Maybe you are looking for
-
Destination directory for draft messages in Apple Mail (Mountain Lion)
Hi, Please could someone possibly tell me location of the Apple Mail draft messages directory. I'm trying to restore these from Mountain Lion, but am unable to find the folder, where the messages are located.
-
is there any Swing component available from third parties which handles html in a better way. i know JEditorPane is available but it onlyhandles simple tags and couldn't handle style tags and javascripting and other advanced html features also if the
-
Webservice to XI. Pls suggest urgent
Hi, At source system I have webservice. I need to communicate to XI. I have imported that wsdl to XI as External Def and made Soap Sender Adapter and all XI part. Now my question is how to trigger from source system webservice to XI and how to make w
-
Any ideas? In the history may iPad never crashed but after updating to iOS 5 it crashed every 30 minutes, and browsing is very slowAny ideas? In the history may iPad never crashed but after updating to iOS 5 it crashed every 30 minutes, and browsing
-
Message resources from page flow
How can I access the ResourceBoundle declared at the top of the page flow, from the page flow?