Question: Exercise 7 - 2 Enhance the Future Value application: This exercise guides you through the process of modifying the Future Value application so it uses
Exercise Enhance the Future Value application:
This exercise guides you through the process of modifying the Future Value application so it uses classes that provide static methods.
Open the project named chexFutureValue that's stored in the exstarts directory. Then, review the code for the FutureValueApp class.
Start a new class named Console in the same package as the FutureValueApp class. Then, move the overloaded getDouble and getInt methods from the FutureValueApp class to the Console class, making sure the methods are public. For this to work, you will also need to add an import statement for the Scanner class to the Console class.
Modify the FutureValueApp class so it uses the methods in the Console class.
Run the application to make sure it still works correctly.
Start a new class named FinancialCalculations in the same package as the other classes. Then, move the FinancialCalculations class, making sure the method is public.
Modify the FutureValueApp class so it uses the static calculateFutureValue method that's stored in the FinancialCalculations class.
Run the application to make sure that it still works correctly.
Original FutureValueApp code:
import java.util.Scanner;
import java.text.NumberFormat;
public class FutureValueApp
public static void mainString args
System.out.printlnWelcome to the Future Value Calculator
;
Scanner sc new ScannerSystemin;
String choice y;
while choiceequalsIgnoreCasey
get the input from the user
System.out.printlnDATA ENTRY";
double monthlyInvestment getDoublesc
"Enter monthly investment: ;
double interestRate getDoublesc
"Enter yearly interest rate: ;
int years getIntsc
"Enter number of years: ;
System.out.println;
calculate the future value
double futureValue calculateFutureValue
monthlyInvestment, interestRate, years;
get the currency and percent formatters
NumberFormat c NumberFormat.getCurrencyInstance;
NumberFormat p NumberFormat.getPercentInstance;
psetMinimumFractionDigits;
format the result as a single string
String results
"Monthly investment: cformatmonthlyInvestment
"Yearly interest rate: pformatinterestRate
"Number of years: years
"Future value: cformatfutureValue
;
print the results
System.out.printlnFORMATTED RESULTS";
System.out.printlnresults;
see if the user wants to continue
System.out.printContinueyn: ;
choice scnextLine;
System.out.println;
public static double getDoubleScanner sc String prompt
while true
System.out.printprompt;
try
return Double.parseDoublescnextLine;
catch NumberFormatException e
System.out.printlnError Invalid decimal value.";
public static double getDoubleScanner sc String prompt,
double min, double max
while true
double value getDoublesc prompt;
if value min && value max
return value;
else
System.out.printlnError Number must be greater than
min and less than max ;
public static int getIntScanner sc String prompt
while true
System.out.printprompt;
try
return Integer.parseIntscnextLine;
catch NumberFormatException e
System.out.printlnError Invalid integer value.";
public static int getIntScanner sc String prompt,
int min, int max
while true
int value getIntsc prompt;
if value min && value max
return value;
else
System.out.printlnError Number must be greater than
min and less than max ;
public static double calculateFutureValuedouble monthlyInvestment,
double interestRate, int years
convert yearly values to monthly values
double monthlyInterestRate interestRate;
int months years ;
double futureValue ;
for int i ; i months; i
futureValue futureValue monthlyInvestment
monthlyInterestRate;
return futureValue;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
