- Jun 23, 2001
- 27,730
- 8
- 0
It seems to output the way I want it to, but someone is embezzling from the CD. This Java course I'm taking has proven to have much more math than I would have preferred.
Here's the pastebin for code, if you prefer to glance at that.
http://pastebin.com/dPf3xVTX
My output ends with these.
14 10456.627999170305
15 10490.031116389877
16 10523.540938011678
17 10557.157804896993
18 10590.88205899597
And according to the text book, verified with tools on USBank, it should end like this.
17 10846.56
18 10898.54
I'm pretty sure I've screwed up my math somewhere. Also, I'm not sure how to cut my outputs off at 2 decimal places. I haven't bothered in previous exercises, but this is a financial exercise. The bank owns everything after the third decimal point.
Edit - I added the "// DecimalFormat df = new DecimalFormat("0.00"); double d = .4; System.out.println(df.format(d));" line after some Googling after creating that Pastebin, but I can't get it to work, so I've commented it out for now
Here's the pastebin for code, if you prefer to glance at that.
http://pastebin.com/dPf3xVTX
Code:
package chapter4;
import java.text.DecimalFormat; // Eclipse made me add this during attempt to correct formating.
import java.util.Scanner;
public class Exercise431 {
public static void main(String[] args) {
// TODO Auto-generated method stub
{
Scanner s = new Scanner(System.in);
// Get initial deposit amount, set to var 'calc'
System.out.println("Please enter the initial deposit amount: ");
double calc = s.nextDouble();
// Get the yield %, set to var 'percentage'
System.out.println("Please enter the annual percentage yield: ");
double percentage = s.nextDouble();
// Get the maturity period.
System.out.println("Please enter maturity period in months: ");
int months = s.nextInt();
double monthly[] = new double[months];
for(int i = 0; i < months; i++)
{
// I think this arithmetic is broken, gets different results than textbook.
// Internet suggests this to format to 0.00, but issues.
// DecimalFormat df = new DecimalFormat("0.00"); double d = .4; System.out.println(df.format(d));
monthly[i] = (calc+calc * percentage /(months*100));
calc = monthly[i];
}
System.out.println("Month" + " " + " CD Value" + "\n");
for(int i = 1; i <= months; i++)
{
System.out.println(" " +i+ " " + monthly[i-1]);
}
}
}
}
My output ends with these.
14 10456.627999170305
15 10490.031116389877
16 10523.540938011678
17 10557.157804896993
18 10590.88205899597
And according to the text book, verified with tools on USBank, it should end like this.
17 10846.56
18 10898.54
I'm pretty sure I've screwed up my math somewhere. Also, I'm not sure how to cut my outputs off at 2 decimal places. I haven't bothered in previous exercises, but this is a financial exercise. The bank owns everything after the third decimal point.
Edit - I added the "// DecimalFormat df = new DecimalFormat("0.00"); double d = .4; System.out.println(df.format(d));" line after some Googling after creating that Pastebin, but I can't get it to work, so I've commented it out for now