Write a program that takes two numbers from the Java console representing, respe
ID: 3788307 • Letter: W
Question
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest rate is an annual rate and is compounded annually.
This is what I have so far- the program runs just fine I'm just having a hard time figuring out how to convert the future value into US dollars. Right now I'm getting back a long scientific number, so please help me figure out how to convert :)
import java.util.*;
public class calculate {
public static double getFutureValue(int investment,double interestRate,int year)
{
return Math.pow(investment*(1+interestRate),year);
}
public static void main(String[] args) {
int investment;
double dollars;
double interestRate;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the investment > ");
investment = keyboard.nextInt();
System.out.println("Enter the interest rate > " );
interestRate = keyboard.nextDouble();
System.out.println("With an investment of $"+investment);
System.out.println("at an interest rate of "+interestRate+"% compounded annually:");
double futureValue = getFutureValue(investment,interestRate,5);
System.out.println("The future value after 5 years is : "+futureValue);
futureValue = getFutureValue(investment,interestRate,10);
System.out.println("The future value after 10 years is : "+futureValue);
futureValue = getFutureValue(investment,interestRate,20);
System.out.println("The future value after 20 years is : "+futureValue);
}
}
Explanation / Answer
Note:
1) I made changes according to the requirement.
2) I have noticed some errors in the calculation part.So made changes in that area.
3) I formatted the output limiting to two decimal places.
Just run the program .If you find any thing wrong just tell me I will make changes.Thank You.
____________________
Calculate.java
import java.text.DecimalFormat;
import java.util.*;
public class Calculate {
//This method will calculate the future value based on compound interest
public static double getFutureValue(int investment, double interestRate,
int year) {
return (investment * Math.pow ((1+ interestRate / 12 ) , year*12));
}
public static void main(String[] args) {
//Declaring the variables
int investment;
double dollars;
double interestRate;
//Scanner class object is used to get the inputs entered by the user
Scanner keyboard = new Scanner(System.in);
//DecimalFormat Object is used To format the output
DecimalFormat df=new DecimalFormat("#.##");
//getting the investment entered by te user
System.out.print("Enter the investment : $");
investment = keyboard.nextInt();
//getting the investment rate entered by the user
System.out.print("Enter the interest rate : %");
interestRate = keyboard.nextDouble();
System.out.println("With an investment of $" + investment);
System.out.println("at an interest rate of " + df.format(interestRate*100)
+ "% compounded annually:");
double futureValue = getFutureValue(investment, interestRate, 5);
//Displaying the amount after 5 years
System.out
.println("The future value after 5 years is : $" + df.format(futureValue));
futureValue = getFutureValue(investment, interestRate, 10);
//Displaying the amount after 10 years
System.out.println("The future value after 10 years is : $"
+ df.format(futureValue));
futureValue = getFutureValue(investment, interestRate, 20);
//Displaying the amount after 20 years
System.out.println("The future value after 20 years is : $"
+ df.format(futureValue));
}
}
______________________
output:
Enter the investment : $650
Enter the interest rate : %0.068
With an investment of $650
at an interest rate of 6.8% compounded annually:
The future value after 5 years is : $912.34
The future value after 10 years is : $1280.56
The future value after 20 years is : $2522.82
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.