Java programing 1. Open the project named ch12_ex1_FutureValue in the ex_starts
ID: 3829323 • Letter: J
Question
Java programing
1. Open the project named ch12_ex1_FutureValue in the ex_starts directory. Then, review the code for this application and run it to make sure it works correctly.
2. Declare a variable at the beginning of the main method for an array list that stores strings.
3. After the code that calculates, formats, and displays the results for each calculation, add code that formats a string with the results of the calcuation and then stores the string in the array list.
4. Add code to display the elements in the array list at the console when ther user indicates that the program should end. Then, test the program by making at least 3 future value calculations.
Explanation / Answer
import java.util.*;
import java.text.*;
public class FutureValueApp
{
public static void main(String[] args)
{
ArrayList<String> lis = new ArrayList<String>();
// display a welcome message
System.out.println("Welcome to the Future Value Calculator");
System.out.println();
// perform 1 or more calculations
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 = getDoubleWithinRange(sc,
"Enter monthly investment: ", 0, 1000);
double interestRate = getDoubleWithinRange(sc,
"Enter yearly interest rate: ", 0, 30);
int years = getIntWithinRange(sc,
"Enter number of years: ", 0, 100);
// calculate the future value
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
// get the currency and percent formatters
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
// format the result as a single string
String results =
"Monthly investment: "
+ currency.format(monthlyInvestment) + " "
+ "Yearly interest rate: "
+ percent.format(interestRate/100) + " "
+ "Number of years: "
+ years + " "
+ "Future value: "
+ currency.format(futureValue) + " ";
// print the results
System.out.println();
System.out.println("FORMATTED RESULTS STORED");
lis.add(results);
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
Iterator<String> it = lis.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static double getDouble(Scanner sc, String prompt)
{
boolean isValid = false;
double d = 0;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}
public static int getInt(Scanner sc, String prompt)
{
boolean isValidInt = false;
int i = 0;
while (isValidInt == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValidInt = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public static double calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months)
{
double futureValue = 0;
for (int i = 1; i <= months; i++)
{
futureValue =
(futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
}
return futureValue;
}
}
/*
Tested with the following 3 future value calculations:
Welcome to the Future Value Calculator
DATA ENTRY
Enter monthly investment: 1000
Error! Number must be less than 1000.0.
Enter monthly investment: 100
Enter yearly interest rate: 12.5
Enter number of years: 5
FORMATTED RESULTS STORED
Continue? (y/n): y
DATA ENTRY
Enter monthly investment: 90
Enter yearly interest rate: 8.5
Enter number of years: 3
FORMATTED RESULTS STORED
Continue? (y/n): y
DATA ENTRY
Enter monthly investment: 85
Enter yearly interest rate: 11
Enter number of years: 9
FORMATTED RESULTS STORED
Continue? (y/n): n
Monthly investment: $100.00
Yearly interest rate: 12.5%
Number of years: 5
Future value: $8,363.50
Monthly investment: $90.00
Yearly interest rate: 8.5%
Number of years: 3
Future value: $3,701.88
Monthly investment: $85.00
Yearly interest rate: 11.0%
Number of years: 9
Future value: $15,712.79
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.