Java Format (Financial application: compute CD value) Suppose you put $10,000 in
ID: 3563900 • Letter: J
Question
Java Format
(Financial application: compute CD value) Suppose you put $10,000 into a CD with an annual % yield of 5.75%. After one month, the CD is worth
10000 + 10000 * 5.75 / 1200 = 10047.92
After two months the CD is worth
10047.91 + 10047.91 * 5.75 / 1200 = 10096.06
After three months the CD is worth
10096.06 + 10096.06 * 5.75 / 1200 = 10144.44
and so on.
- Write a program that prompts the use to enter an amount (e.g., 10000), the annual percentage yield (e.g, 5.75), and the number of months (e.g, 18) and displays a table as shown in the sample run.
____________________________________________________
Month CD Value
1 10047.92
2 10096.06
...
17 10846.57
18 10898.54
Explanation / Answer
//Compile: javac FinanceProgram.java
//Run : java FinanceProgram
/**
* The java program prompts user to enter
* amount,percentage and number of months
* then calculates the compound interest
* on each month upto number of months,
* */
import java.util.Scanner;
public class FinanceProgram
{
//static variables to store amount,percentage
//and months
static double amount;
static double percentage;
static int numberOfMonths;
public static void main(String[] args)
{
//scanner object to read input
Scanner reader=new Scanner(System.in);
//Read input from the user
System.out.println("Enter amount");
amount=reader.nextDouble();
System.out.println("Enter percentage");
percentage=reader.nextDouble();
System.out.println("Enter number of months");
numberOfMonths=reader.nextInt();
//call the static method with three inputs
//amount,percentage and number of months
//Then it displays the table of months and compoun
//interest
displayCDTable(amount,percentage,numberOfMonths);
}
//Method displayCDTable accepts amount,percentage
//and number of months .Then calculates the CD value
//Prints the month and CD value in each month over
//number of months
private static void displayCDTable(double amount, double percentage,
int numberOfMonths)
{
double CDValue;
System.out.printf("%-10s%-10s ","Month","CD Value");
//loop to calculate CD value
for (int month = 1; month <= numberOfMonths; month++)
{
//Calculate CD value
CDValue=amount+percentage*amount/1200;
System.out.printf("%-15d%-10.2f ",month,CDValue);
//set CD value as amount for next month
amount=CDValue;
}
}//end of the method
}//end of class
--------------------------------------------------------------------------
Sample output:
Enter amount
10000
Enter percentage
5.75
Enter number of months
18
Month CD Value
1 10047.92
2 10096.06
3 10144.44
4 10193.05
5 10241.89
6 10290.97
7 10340.28
8 10389.82
9 10439.61
10 10489.63
11 10539.89
12 10590.40
13 10641.14
14 10692.13
15 10743.37
16 10794.84
17 10846.57
18 10898.54
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.