Future Investment Value Prediction Program using Java

Verified

Added on  2025/04/23

|3
|418
|152
AI Summary
Desklib provides past papers and solved assignments for students. This assignment demonstrates building a Java program to calculate future investment values.
Document Page
1. Analysis
In this program we have to develop code that can print future investment using a given
formula. Initialization of variables like investment amount, annual rate and number of
years will act as inputs for the program and future investment will be the output.
2. Design
Declaration of variables was the first step, for example: double amt for investment
amount, double rate for annual rate, int years for years and double futureinv for
future investment output.
Declaring a function for generating the output is the next step. Using the given
formula and replacing the formula coefficients with the declared variables so the
output can be generated.
Next step of the program will be importing scanner class so that, input can be
taken from the user, then creating sc object of scanner class which will be used to
take input for all the variables.
Implementing exception handling because if user enter an input which is of wrong
datatype then error message can be shown and program will be continued.
After the exception handling the program will be returned to the main function.
3. Coding
package oop;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FutureInvestment {
static double amt;
static double rate;
static int years;
static double futureinv=0.0;
public static void investmentValue(double amt, double rate, int
years) {
rate =( rate/100 + 1);
double newrate= (double) Math.pow(rate, 12*years);
futureinv= amt * newrate;
System.out.println(futureinv);
}
public static void main (String[] args) {
try {
Scanner sc= new Scanner (System.in);
System.out.println("Enter investment amount: ");
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
double amount= sc.nextDouble();
System.out.println("Enter interest rate (%): ");
double rt= sc.nextDouble();
System.out.println("Enter number of years: ");
int yr=sc.nextInt();
System.out.println("Accumulated value is ");
investmentValue(amount, rt, yr);
}
catch (InputMismatchException e) {
System.out.println("Enter numerical values only.");
main(null);
}
catch (Exception e) {
System.out.println(e);
main(null);
}
}
}
4. Output screenshot
5. Testing
First enter the investment amount, if the value is of the correct data type then the
program will continue and if it’s not then exception will be executed to return the
program to the main function.
Then enter the interest, if the value is of the correct data type then the program
will continue and if it’s not then exception will be executed to return the program
to the main function.
At this step the year value will be given as input, again exception handling will
run to check if any exception are present.
Document Page
At last step the input values will be passed into the method
investmentvalue(amount, rt, yr) and the output will be calculated and will
be printed on the console.
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]