Future Investment Value Prediction Program using Java
VerifiedAdded 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.

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: ");
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: ");
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

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.
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.

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.
investmentvalue(amount, rt, yr) and the output will be calculated and will
be printed on the console.
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 3
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.