In: Computer Science
Write a Java method that computes the future
investment value at a given interest rate for a specified number of
years. Your method should return the future investment value after
calculation.
The future investment is determined using the formula below:
futureInvestmentValue = investmentAmount *
(1+monthlyInterestRate)numberOfYears*12
You can use the following method header:
public static double futureInvestmentValue (double
investmentAmount, double monthlyInterestRate, int years);
The required java method and the complete source code are given below.Detailed comments are included for better understanding the code.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Method to calculate future investment value
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years) { //calculating futureInvestmentValue //pow() method is used to find the power double futureInvestmentValue=investmentAmount *Math.pow((1+monthlyInterestRate),(years*12)) ; //returning the result return futureInvestmentValue; }
Formula used
Source code of the whole program
//Scanner class to read user input import java.util.Scanner; public class Main { public static void main(String[] args) { //creating scanner object to read user input Scanner input=new Scanner(System.in); //prompt user to enter different values and read it using appropriate methods of Scanner class System.out.print("Enter Investment Amount: "); double amount=input.nextDouble(); System.out.print("Enter Annual Interest : "); double annualRate=input.nextDouble(); //calculating monthlyInterestRate in decimal form //dividing annualRate by 12 gives monthlyRate and dividing it by 100 gives its decimal form double monthlyRate=annualRate/1200; System.out.print("Enter Number of Years: "); int years=input.nextInt(); //calling futureInvestmentValue()function double futureInvestValue=futureInvestmentValue(amount,monthlyRate,years); //printing the value System.out.printf("Future Investment Value: %.2f",futureInvestValue); } //futureInvestmentValue() function definition public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years) { //calculating futureInvestmentValue //pow() method is used to find the power double futureInvestmentValue=investmentAmount *Math.pow((1+monthlyInterestRate),(years*12)) ; //returning the result return futureInvestmentValue; } }
Screen shot of the code
Screen shot of the output