Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (ellecullen):

Homework help in Java: Java Au Naturel Chapter 6 exercise 6.5 I need help on how to revise the GrowthRates class to compound monthly. Chapter is attached and class code in comments w exercise

OpenStudy (ellecullen):

Exercise 6.5 Revise GrowthRates to compound monthly. For instance, if the rate is 6% multiply by 1.005 each month, then tell the number of years and months to double.

OpenStudy (dan815):

@Kainui @ganeshie8 @Mimi_x3

OpenStudy (dan815):

these guys should know java

OpenStudy (ellecullen):

GrowthRates class public class GrowthRates { /** Calculate time to double your money at a given rate. */ public GrowthRates() { String input = JOptionPane.showInputDialog ("Annual rate? 0 if done:"); double rate = Double.parseDouble (input); while (rate > 0.0) { JOptionPane.showMessageDialog (null, "It takes " + yearsToDouble (rate) + " years for \nyour money to double."); input = JOptionPane.showInputDialog ("Another rate (0 when done) :"); rate = Double.parseDouble (input); } } /** Precondition: interestRate is positive. */ public int yearsToDouble (double interestRate) { double balance = 1.0; int count = 0; while (balance < 2.0) { balance = balance * (1.0 + interestRate / 100.0); count++; } return count; } }

OpenStudy (kainui):

You can type ``` before and after a block of code to make it look a lot nicer: ``` public class GrowthRates { /** Calculate time to double your money at a given rate. */ public GrowthRates() { String input = JOptionPane.showInputDialog ("Annual rate? 0 if done:"); double rate = Double.parseDouble (input); while (rate > 0.0) { JOptionPane.showMessageDialog (null, "It takes " + yearsToDouble (rate) + " years for \nyour money to double."); input = JOptionPane.showInputDialog ("Another rate (0 when done) :"); rate = Double.parseDouble (input); } } /** Precondition: interestRate is positive. */ public int yearsToDouble (double interestRate) { double balance = 1.0; int count = 0; while (balance < 2.0) { balance = balance * (1.0 + interestRate / 100.0); count++; } return count; } } ``` So can you show me what you've tried?

OpenStudy (ellecullen):

so far, I did research and tried to understand what is means to compound monthly

OpenStudy (ellecullen):

it sounds like the exercise wants this, meaning that the interest is added to the total

OpenStudy (kainui):

Well honestly I don't know much or anything about financial stuff, I can really only help you on the Java side of this question.

OpenStudy (ellecullen):

So I decided to revise the appropriate method. /** Precondition: interestRate is positive. */ /** Exercise 6.5 * Revising this method */ public int yearsToDouble (double interestRate) { double balance = 1.0; int count = 0; while (balance < 2.0) { balance = balance * (1.0 + interestRate / 100.0); count++; } if (interestRate == 6) // if the rate is equal to 6 percent { balance = balance * (1.0 + interestRate * 1.005); count++; } return count; }

OpenStudy (kainui):

Hmm I don't think this will solve your problem, since it is more general than just altering the individual value of when the interest rate is 6. For instance, the relationship between 6% and 1.005 is that 1.005 is 100% + (6%)/12 So it's taking that 6% and dividing it by every month in the year and now as opposed to compounding the interest in a single year.

OpenStudy (ellecullen):

Okay, How do you think I should type it, so that the program tells how many months for the money to double ? For 6 % specifically it takes 13 years to double. I am confused on "tell the number of year and months to double. Does it mean how many months in 13 years or how many ears and months altogether it takes for the money to double?

OpenStudy (ellecullen):

So now, I altered the code more public GrowthRatesV2() { String input = JOptionPane.showInputDialog ("Annual rate? 0 if done:"); double rate = Double.parseDouble (input); double months1 = 30 * yearsToDouble (rate); double months2 = 31 *yearsToDouble (rate); double months = months1 = months2; while (rate > 0.0) { JOptionPane.showMessageDialog (null, "It takes " + yearsToDouble (rate) + " years for \nyour money to double." + months + " months "); input = JOptionPane.showInputDialog ("Another rate (0 when done) :"); rate = Double.parseDouble (input); // double months1 = 30 * yearsToDouble (rate); // double months2 = 31 *yearsToDouble (rate); // double months = months1 || months2; if (months1 >= 30) if (months1 == 31) JOptionPane.showMessageDialog (null, " It also takes " + months2 + " months for your money to double. "); } } /** Precondition: interestRate is positive. */ /** Exercise 6.5 * Revising this method */ public int yearsToDouble (double interestRate) { double balance = 1.0; int count = 0; while (balance < 2.0) { balance = balance * (1.0 + interestRate / 100.0); count++; } if (interestRate == 6) // if the rate is equal to 6 percent { balance = balance * (1.0 + interestRate * 1.005); count++; } return count; }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!