Write a program that calculates the amount of money that you must invest In a sa
ID: 3778626 • Letter: W
Question
Write a program that calculates the amount of money that you must invest In a savings account at a given interest rate for a given number of years to have a certain dollar amount at me end of the period Y our program will ask the user for the amount the user wants to have at the end of the term (future value). the number of years the user plans to let the money stay in the account, and the annual interest rate. The program will calculate the amount of money that the user needs to deposit (present value). Use the following formula to calculate the present value: P = F/(1 + r/100)^n P is the present value, or the amount of money deposited F is the future value that the user wants to have at the end r is the annual interest rate n to the number of years that the user plans to let the money stay in the account Your program must contain/use the following functions: getFutureValue - A function that gets a nonnegative future value from the user getAnnualRate - A function that gets a nonnegative annual interest rate from the user getYears - A function that gets the nonnegative number of years of the investment from the user CalcPresentvalue - A function that calculates and returns the amount that must be deposited given the future value, annual interest rate, and number of years as arguments You can add other functions as needed. Your program must implement the prototypes given below (exactly as given): double getFutureValue (); double getAnnualIntRate (); int getYears (); double calcPresentValue (double futureValue, double rate, int years); Sample output of a program that satisfies the requirements of this assignment is given below.Explanation / Answer
import java.util.*;
public class Value{
static int getYears(){
Scanner sc = new Scanner(System.in);
System.out.print("Number of Years: ");
int years = sc.nextInt();
sc.close();
if (years>=0)
return years;
else
return 0;
}
static double getAnnualIntRate(){
Scanner sc = new Scanner(System.in);
System.out.print("Annual Interest Rate: ");
double rate = sc.nextDouble();
sc.close();
if (rate>=0)
return rate;
else
return 0;
}
static double getFutureValue(){
Scanner sc = new Scanner(System.in);
System.out.print("Future Value: ");
double futureValue = sc.nextDouble();
sc.close();
if (futureValue>=0)
return futureValue;
else
return 0;
}
static double calcPresentValue(double futureValue, double rate, int years){
return futureValue/Math.pow(1.0+rate/100.0, years);
}
public static void main(String[] args) {
double futureValue = getFutureValue();
double rate = getAnnualIntRate();
int years = getYears();
System.out.println("You need to deposit " + calcPresentValue(futureValue, rate, years));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.