|
-
Feb 25th, 2007, 11:46 AM
#1
Thread Starter
New Member
Java Newbie Needs Help
Hi there, I am fairly new to Java and am doing my best to work with it, but I am having a major problem with one of my programs.
What the program does is, the user will enter an initial balance and a rate of interest to be compounded annually. The program then returns the balance the user will have at the end of each year until it reaches ten years.
However, according to my adventures in grade 12 math, the results are seriously wrong. Here is my code:
Code:
import java.io.*;
public class CompoundInt {
public static void main(String[] args) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
System.out.print("What is the initial balance?" + '\t');
String initialBalance = myInput.readLine();
double inBalance = Double.parseDouble(initialBalance);
System.out.print("What is the rate of interest(annually compounded)?" + '\t');
String stringRateOfInterest = myInput.readLine();
double rateOfInterest = Double.parseDouble(initialBalance);
System.out.println(" ");
compound(inBalance, rateOfInterest);
}
static public void compound (double deposit, double rate)
{
double endDeposit;
int loopNumber = 0;
System.out.println("Year" + '\t' + '\t' + "Final Balance");
rate = 1 + (rate / 100);
while (loopNumber < 10)
{
loopNumber++;
endDeposit = (deposit * (Math.pow(rate, loopNumber)));
System.out.println("Year " + loopNumber + ":" + '\t' + endDeposit);
}
}
}
EDIT: I don't know how clear it may be in the code, but the output is totally wrong. For example, say that the initial balance is 100 and the rate of interest is 5%. According to my EXTENSIVE calculations, at the end of year 1, the user should have $105. The next year they should have $110, etc. However, according to this program, the user will have 200 the first year, 400 the next, 800 the third year, etc.
Last edited by lonlonmalon; Feb 25th, 2007 at 11:50 AM.
-
Feb 25th, 2007, 01:47 PM
#2
Re: Java Newbie Needs Help
Your extensive calculations are wrong. (That doesn't make your program right - just saying.) After one year, the deposit is 105. But the next year, the 5% are calculated from that, so the third year should show 110.25.
I don't see an obvious error in the program, though. Perhaps store the result of Math.pow() in a temporary and write it out, so you can check what your program does.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 25th, 2007, 03:05 PM
#3
Thread Starter
New Member
Re: Java Newbie Needs Help
I admit I was a little lazy after doing the first year's calculation. Thanks for the correction. :P
But I will try out your idea ASAP.
-
Feb 26th, 2007, 12:46 AM
#4
Addicted Member
Re: Java Newbie Needs Help
I was checking the math and it seems to be alright, then I noticed this part:
double rateOfInterest = Double.parseDouble(initialBalance);
the bold should be replaced with stringRateOfInterest
-
Feb 26th, 2007, 07:01 AM
#5
Re: Java Newbie Needs Help
Whoa, good catch! The dreaded copy&paste.
That's why I always use one variable for all temporary strings. I call it "t" or "temp" or something like that.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 26th, 2007, 09:58 AM
#6
Thread Starter
New Member
Re: Java Newbie Needs Help
Ahhh, I see. Thanks a lot!
That's what I get for programming late at night :P
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|