0% found this document useful (0 votes)
5 views

Project 4

Uploaded by

Harris Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Project 4

Uploaded by

Harris Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Student Name: _______Khan Muhammad Haris (8002L23005)___________

Class and Section _______Java Programming___________


Total Points (100 pts) __________________
Due: April 2, 2024

Project: Computing Future Investment Value


Introduction to Programming Principles
Nanchang University

Problem Description:

Write a method that computes future investment value at a


given interest rate for a specified number of years. The
future investment is determined using the following
formula:
futureInvestmentValue =
investmentAmount x (1 + monthlyInterestRate)numberOfYears*12

Use the following method header:


public static double futureInvestmentValue(
double investmentAmount, double monthlyInterestRate, int years)

For example, futureInvestmentValue(10000, 0.05/12, 5)


returns 12833.59.

Write a test program that prompts the user to enter the


investment amount (e.g., 1000) and the interest rate
(e.g., 9%) and prints a table that displays future value
for the years from 1 to 30, as shown below:
The amount invested: 1000
Annual interest rate: 9%
Years Future Value
1 1093.80
2 1196.41
...
29 13467.25
30 14730.57

Analysis:

The problem entails developing a program that assists users in understanding the growth
potential of their investments over time.
1. Input Gathering: The program prompts users to input the initial investment
amount and the annual interest rate as a percentage. This input is crucial for
determining the future value of the investment.
2. Calculation: Upon receiving the input, the program calculates the future value of
the investment for each year over a 30-year period. This calculation involves
1
converting the annual interest rate into a monthly rate and using it to compound
the investment over time.
3. Output Presentation: The program then presents the calculated future values in a
tabular format, displaying the investment's growth over the specified period. This
output allows users to visualize how their investment will evolve and make
informed decisions regarding their financial planning.
Overall, the program facilitates understanding of investment growth by providing a clear
and structured representation of future value projections based on user input.

Design:

1. Input Gathering: The program should prompt me to enter the investment amount
and validate that it's a positive number.
2. Interest Rate Input: Next, the program should prompt me to enter the annual
interest rate as a percentage and validate that it's also a positive number.
3. Calculation: After gathering the necessary information, the program should
calculate the monthly interest rate using the provided formula.
4. Future Value Calculation: Using the calculated monthly interest rate, the
program should compute the future value for each year from 1 to 30.
5. Output Display: Finally, the program should display the future value for each
year in a tabular format, providing a clear overview of the investment's growth
over time.

Coding: (Copy and Paste Source Code here. Format your code using Courier 10pts)
2
import java.util.Scanner;

public class Project_4 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// investment amount
double investmentAmount = PositiveDoubleInput(scanner, "Enter the amount invested:
");

// annual interest rate


double annualInterestRate = PositiveDoubleInput(scanner, "Enter the annual interest rate
(in percentage): ");

// Calculate monthly interest rate


double monthlyInterestRate = annualInterestRate / 12 / 100;

// Calculate and display future values for each year


displayFutureValues(investmentAmount, monthlyInterestRate);

// Close the scanner


scanner.close();
}

// for a positive double input with validation


private static double PositiveDoubleInput(Scanner scanner, String Message) {
double input;
do {
System.out.print(Message);
// Check if input is a valid double
while (!scanner.hasNextDouble()) {
System.out.println("Error: Please enter a valid number.");
scanner.next(); // Clear the invalid input
}
input = scanner.nextDouble();
// Check if input is positive
if (input <= 0) {
System.out.println("Error: Please enter a positive number.");
}
} while (input <= 0);
return input;
}

// Method to calculate and display future values for each year


private static void displayFutureValues(double investmentAmount, double
monthlyInterestRate) {
System.out.println("\nYears Future Value");
// Loop through each year from 1 to 30

3
for (int years = 1; years <= 30; years++) {
// Calculate future value for the current year
double futureValue = calculateFutureValue(investmentAmount, monthlyInterestRate,
years);
// Display future value with proper formatting
System.out.printf("%-7d%.2f\n", years, futureValue);
}
}

// Method to compute future investment value for a given number of years


public static double calculateFutureValue(double investmentAmount, double
monthlyInterestRate, int years) {
// Calculate future investment value using the provided formula
return investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
}
}

You might also like