MITS4002 - Activity 03 - Future Investment Value Calculator Program

Verified

Added on  2022/08/21

|2
|528
|11
Homework Assignment
AI Summary
This assignment presents a Java program designed to calculate the future value of an investment. The program takes user inputs for the investment amount, annual interest rate, and the number of years. The core logic utilizes the formula `futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate)numberOfYears*12` and the `Math.pow()` method for calculations. The code includes input validation to ensure the inputs are valid. The program then calculates and displays the accumulated value for each year. The solution also includes a design overview, the complete Java code, and testing results to validate the program's functionality with different inputs, including screenshots of the program's output and invalid input handling. The program showcases a practical application of object-oriented programming principles within the context of financial calculations, suitable for an introductory computer science course like MITS4002.
Document Page
Design: (Describe the major steps for solving the problem.) 5 marks.
In the program, there are two methods one is for calculating the value of the future investment,
and another one is the main to calculate the future investment value. For this program, only
one .java file is created to perform calculations and used for taking user input. The main function
of this program calls a static method. The static method is for calculating the value of a future
investment based on user input. In this program, two double and one integer datatype variables
are used for user input.
Coding:
import java.util.Scanner;
public class FutureInvestmentValue {
public static void main(String[] args) {
// TODO Auto-generated method stub
double amount=0,rate=0;
int yr=0;
System.out.println("-----------------------------------");
System.out.println("Calculate Future Investment Value");
System.out.println("-----------------------------------");
Scanner sc=new Scanner(System.in);
System.out.println("Enter investment amount: ");
amount=sc.nextDouble();
System.out.println("Enter annual interest rate:");
rate=sc.nextDouble();
if(amount>0 && rate>0) {
rate=rate*0.01;
System.out.println("Number of years: ");
yr=sc.nextInt();
for(int i = 1; i <= yr; i++) {
double result=(futureInvestmentValue(amount, rate/12, i));
System.out.println("------------------------------------");
System.out.printf("Accumulated value is $"+"%.2f",result);
System.out.println();
System.out.println("-------------------------------------");
}
}else {
System.out.println("Invalid Input...!!");
}
}
public static double futureInvestmentValue(double investmentAmount,
double monthlyInterestRate, int years) {
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
return investmentAmount * Math.pow(1 + monthlyInterestRate,
years * 12);
}}
Output screenshot:
Screenshot-1
Screenshot-2: Invalid Input
Testing:
The above program is for calculating future investment value by applying a formula. The above
program is tested by different amounts and rates to check the program is giving accurate output
or not. As per the above screenshot, the program taking three different inputs from the user, then
based on the input, it generates the desired output. In the above program, it is necessary to check
the input given by the user is valid or not because if the input given by the user is invalid, then
the program generates the wrong output.
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]