I need to manipulate my current code for a program called FutureValue App. Heres
ID: 3566159 • Letter: I
Question
I need to manipulate my current code for a program called FutureValue App.
Heres what I got.
import java.util.*;
import java.text.*;
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"))
{
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);
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
String results =
"Monthly investment: "
+ currency.format(monthlyInvestment) + " "
+ "Yearly interest rate: "
+ percent.format(interestRate/100) + " "
+ "Number of years: "
+ years + " "
+ "Future value: "
+ currency.format(futureValue) + " ";
System.out.println();
System.out.println("FORMATTED RESULTS");
System.out.println(results);
System.out.print("Continue? (y/n): ");
choice = sc.next();
sc.nextLine(); // discard any other data entered on the line
System.out.println();
}
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
{
double d = 0.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)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = 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;
}
}
The Printed Results Is This
I Have 2 Assignments.
1st is to add a rectangular array:
2nd is to add an ArrayList:
Hope its clear! thanks
This exercise will guide you through the process of adding a rectangular array to the Future Value application. This array will store the values for up to ten of the calculations that are performed, and print a summary of those calculations when the program ends that looks something like this: Open the Future Value App application stored in the ch10 directory. 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. After the code that calculates, formats, and displays the results for each calculation, add code that stores the formatted values as strings in the next row of the array (you need to use the toString method of the Integer class to store the years value). Add code to display the elements in the array at the console when the user indicates that the program should end. The output should be formatted as shown above. Then, compile and test the program by making up to 10 future value calculations. When you've got this working right, close the program. This exercise will guide you through the process of adding a rectangular array to the Future Value application. This array will store the values for each calculation that is performed, and print a summary of those calculations when the program ends that looks something like this: Open the FutureValueApp class stored in the chi IVFutureValueArrayList directory. Declare a variable at the beginning of the main method for an array list that stores strings. After the code that calculates, formats, and displays the results for each calculation, add code that formats a string with the results of the calculation, then stores the string in the array list. Add code to display the elements in the array list at the console when the user indicates that the program should end. Then, test the program by making at least 3 future value calculations.Explanation / Answer
Please check the changed/added code which is in bold letters.
Program code:
//FutureValueApp.java
import java.util.*;
import java.text.*;
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";
String [][]rectangular=new String[10][4];
int i=0;
int j=0;
while (choice.equalsIgnoreCase("y"))
{
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);
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue( monthlyInvestment, monthlyInterestRate, months);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
//adding a each column value to a each row.
rectangular[i][j]=""+ currency.format(monthlyInvestment) + " ";
rectangular[i][j+1]="" + percent.format(interestRate/100) + " ";
rectangular[i][j+2]="" + years + " " ;
rectangular[i][j+3]="" + currency.format(futureValue) + " ";
i++;
System.out.println();
System.out.print("Continue? (y/n): ");
choice = sc.next();
sc.nextLine(); // discard any other data entered on the line
System.out.println();
}
System.out.println("FORMATTED RESULTS");
System.out.println("inv/month Rate Years Future value");
//print the rectangular array format using double dimension array
for(int k=0;k<rectangular.length;k++)
{
for(int l=0;l<rectangular[k].length;l++)
{
System.out.print(rectangular[k][l]);
}
}
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println
("Error! Invalid double 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.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)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = 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;
}
}
----------------------------------------------------------------------------------------------
Sample output:
Welcome to the Future Value Calculator
DATA ENTRY
Enter monthly investment: 100
Enter yearly interest rate: 5
Enter number of years: 15
Continue? (y/n): y
DATA ENTRY
Enter monthly investment: 200
Enter yearly interest rate: 7
Enter number of years: 8
Continue? (y/n): y
DATA ENTRY
Enter monthly investment: 300
Enter yearly interest rate: 10
Enter number of years: 25
Continue? (y/n): n
FORMATTED RESULTS
inv/month Rate Years Future value
$100.00 5.0% 15 $26,840.26
$200.00 7.0% 8 $25,789.33
$300.00 10.0% 25 $401,367.10
Note:
Here in the above code the out put includes the "null" values because the String [][] is size of 10 rows and 4 columns
------------------------------------------------------------------------------------------------------------------------------
//FutureValueArrayList.java
import java.util.*;
import java.text.*;
public class FutureValueArrayList
{
public static void main(String[] args)
{
System.out.println("Welcome to the Future Value Calculator ");
Scanner sc = new Scanner(System.in);
String choice = "y";
ArrayList<String> arrList=new ArrayList<String>();
while (choice.equalsIgnoreCase("y"))
{
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);
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue(monthlyInvestment, monthlyInterestRate, months);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
//store the values into a string of single array
String results =""+ currency.format(monthlyInvestment) + " "
+ percent.format(interestRate/100) + " "
+ years + " "
+ currency.format(futureValue) + " ";
//add the results string into array list
arrList.add(results);
System.out.println();
System.out.print("Continue? (y/n): ");
choice = sc.next();
sc.nextLine(); // discard any other data entered on the line
System.out.println();
}
System.out.println("FORMATTED RESULTS");
System.out.println("inv/month Rate Years Future value");
//print the rectangular format output using Arraylist
for(int i=0;i<arrList.size();i++)
{
System.out.print(arrList.get(i));
}
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println
("Error! Invalid double 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.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)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = 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;
}
}
Sanple Output:
Welcome to the Future Value Calculator
DATA ENTRY
Enter monthly investment: 100
Enter yearly interest rate: 4
Enter number of years: 10
Continue? (y/n): y
DATA ENTRY
Enter monthly investment: 150
Enter yearly interest rate: 6
Enter number of years: 10
Continue? (y/n): y
DATA ENTRY
Enter monthly investment: 200
Enter yearly interest rate: 8
Enter number of years: 15
Continue? (y/n): n
FORMATTED RESULTS
inv/month Rate Years Future value
$100.00 4.0% 10 $14,774.06
$150.00 6.0% 10 $24,704.81
$200.00 8.0% 15 $69,669.03