There are three input numbers that are required from the user. The first is the target savings amount which will be expressed in a dollar amount representing the target number of dollars to be saved. We will call this targetSavings . The second piece of input is a positive integer with the number of months until the savings must be achieved. We will call this numberOfMonths . The final piece of input is a double with the annual interest rate (i.e. the number 5.3 would be an annual interest rate of 5.3%). We will call this interestRate .
You will then use these three numbers to calculate the amount that must be saved each month. You should store all intermediate amounts of money in integers denoting the number of cents. You will need at least two Java methods to calculate this amount. You may choose to have more in order to break up your code into manageable sized tasks.
The first Java method is a method called amountSaved that takes in a candidate monthly payment in cents candidatePayment and then calculates the amount that would be saved if the person sets aside candidatePayment cents each month for numberOfMonths months. The interest will be compounded monthly. That is, starting with zero saved, you will iterate for numberOfMonths months and each month you will then add in candidatePayment and multiply the amount saved by (1 + (interestRate/(12*100))) . At each point, the amount saved should be rounded down to an integer since the bank does not keep track of fractions of cents. For consistency, it is best for every one to round downwards instead of using the round method in class Math. (I bet the bank rounds down).
Once you have your method amountSaved working you need to calculate the correct amount monthlyPayment to set aside. This should be the smallest value of X such that amountSaved( X ) is at least targetSavings . To find this value, you will need to binary search for the desired monthlyPayment . In order to do a proper binary search, you will need an upper bound and a lower bound for the correct monthlyPayment . 0 will work for a lower bound. A reasonable upper bound would be targetSavings/numberOfMonths . This is the amount you would have to set aside if the interest rate were 0. At each iteration of your binary search, you will need to test if your current value for the monthly payment is greater or less than the desired value. This test will required your amountSaved method which you have written.
You may assume that the user inputs integers for the number of months and the target savings. You may also assume that a real number is input for the interest rate. However, this is all you can assume. You must check for unreasonable input values (like negative interest rates, etc.) You also need to check for more subtle problems, like if the number of months is so large that even setting aside a single penny a month will result in too much savings. In each case, you need to print out a reasonable and polite error message to the user. If there are no errors, you should output the monthly value to be saved in dollars and cents, e.g. $45.27.
You may use the code that I have provided below. The action starts when the user presses the Calculate button. Your code starts out in the paint method, but you will need to call other methods from there to do your calculations. You may also decide to have some data that is global to the entire class, although you should only need to keep the three input variables global. I have given you some sample code there to write a string to the window of your program which you can use for output.