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 7-2 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.
1. Open the project named ch07_ex2_FutureValue that's stored in the ex_starts directory. Then, review the code for the FutureValueApp class.
2. 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.
3. Modify the FutureValueApp class so it uses the methods in the Console class.
4. Run the application to make sure it still works correctly.
5. 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.
6. Modify the FutureValueApp class so it uses the static calculateFutureValue() method that's stored in the FinancialCalculations class.
7. 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 main(String[] args){
System.out.println("Welcome to the Future Value Calculator
");
Scanner sc = new Scanner(System.in);
String choice ="y";
while (choice.equalsIgnoreCase("y")){
// get the input from the user
System.out.println("DATA ENTRY");
double monthlyInvestment = getDouble(sc,
"Enter monthly investment: ",0,1000);
double interestRate = getDouble(sc,
"Enter yearly interest rate: ",0,30);
int years = getInt(sc,
"Enter number of years: ",0,100);
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();
p.setMinimumFractionDigits(1);
// format the result as a single string
String results
= "Monthly investment: "+ c.format(monthlyInvestment)+"
"
+ "Yearly interest rate: "+ p.format(interestRate /100)+"
"
+ "Number of years: "+ years +"
"
+ "Future value: "+ c.format(futureValue)+"
";
// print the results
System.out.println("FORMATTED RESULTS");
System.out.println(results);
// see if the user wants to continue
System.out.print("Continue?(y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
public static double getDouble(Scanner sc, String prompt){
while (true){
System.out.print(prompt);
try {
return Double.parseDouble(sc.nextLine());
} catch (NumberFormatException e){
System.out.println("Error! Invalid decimal value.");
}
}
}
public static double getDouble(Scanner sc, String prompt,
double min, double max){
while (true){
double value = getDouble(sc, prompt);
if (value > min && value < max){
return value;
} else {
System.out.println("Error! Number must be greater than "+
min +" and less than "+ max +".");
}
}
}
public static int getInt(Scanner sc, String prompt){
while (true){
System.out.print(prompt);
try {
return Integer.parseInt(sc.nextLine());
} catch (NumberFormatException e){
System.out.println("Error! Invalid integer value.");
}
}
}
public static int getInt(Scanner sc, String prompt,
int min, int max){
while (true){
int value = getInt(sc, prompt);
if (value > min && value < max){
return value;
} else {
System.out.println("Error! Number must be greater than "+
min +" and less than "+ max +".");
}
}
}
public static double calculateFutureValue(double monthlyInvestment,
double interestRate, int years){
// convert yearly values to monthly values
double monthlyInterestRate = interestRate/12/100;
int months = years *12;
double futureValue =0.0;
for (int i =1; i <= months; i++){
futureValue =(futureValue + monthlyInvestment)*
(1+ monthlyInterestRate);
}
return futureValue;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!