Question
package murach.calculators; public class Financial { public static double calculateFutureValue(double monthlyInvestment, double yearlyInterestRate, int years) { // convert yearly values to monthly values double monthlyInterestRate
package murach.calculators;
public class Financial {
public static double calculateFutureValue(double monthlyInvestment,
double yearlyInterestRate, int years) {
// convert yearly values to monthly values
double monthlyInterestRate = yearlyInterestRate / 12 / 100;
int months = years * 12;
// calculate the future value
double futureValue = 0;
for (int i = 1; i <= months; i++) {
futureValue += monthlyInvestment;
double monthlyInterestAmount = futureValue * monthlyInterestRate;
futureValue += monthlyInterestAmount;
}
return futureValue;
}
}
package murach.ui;
import java.text.NumberFormat; import murach.calculators.Financial;
public class Main {
public static void main(String[] args) { // displayLine a welcome message Console.displayLine("Welcome to the Future Value Calculator"); Console.displayLine();
String choice = "y"; while (choice.equalsIgnoreCase("y")) {
// get input from user double monthlyInvestment = Console.getDouble("Enter monthly investment: "); double yearlyInterestRate = Console.getDouble("Enter yearly interest rate: "); int years = Console.getInt("Enter number of years: "); // call the future value method double futureValue = Financial.calculateFutureValue( monthlyInvestment, yearlyInterestRate, years);
// format and displayLine the result Console.displayLine("Future value: " + NumberFormat.getCurrencyInstance().format(futureValue)); Console.displayLine();
// see if the user wants to continue choice = Console.getString("Continue? (y/n): "); Console.displayLine(); } Console.displayLine("Bye!"); } }
package murach.ui;
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner(System.in); public static void displayLine() { System.out.println(); }
public static void displayLine(String s) { System.out.println(s); }
public static String getString(String prompt) { System.out.print(prompt); String s = sc.nextLine(); return s; }
public static int getInt(String prompt) { int i = 0; while (true) { System.out.print(prompt); try { i = Integer.parseInt(sc.nextLine()); break; } catch (NumberFormatException e) { System.out.println("Error! Invalid integer. Try again."); } } return i; }
public static double getDouble(String prompt) { double d = 0; while (true) { System.out.print(prompt); try { d = Double.parseDouble(sc.nextLine()); break; } catch (NumberFormatException e) { System.out.println("Error! Invalid decimal. Try again."); } } return d; } }
This exercise guides you through the process of adding a two-dimensional, rectangular array to the Future Value application. This array stores the valus for up to ten of the calculations. When the application ends, it prints a summary of those calculations tothe console like this:
Future Value Calculations
Inv/Mo. Rate Years Future Value
$100.00 8.0% 10 $18,416.57
$125.00 8.0% 10 $23,020.71
$150.00 8.0% 10 $27,624.85
1. Review the code and run the application to make sure it works correctly.
2. Declare variables at the beginning of the main method for a row counter and a rectangular array of strings that provides for 10 rows and 4 columns.
3. After the code that displas the results for eah calculation, add code that formats all values for the calculation and converts them to strings. Then, store these String values in the next row of the array. (Hint: You can use the toString method of the Integer class to convert the years value to a string.
4. Add code to display the elements on the console when the user exits the application. The output shold be formatted as shown above and should only include the rows that contain data.
5. Test the application by making up to 10 future value calculations.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started