JAVA
Using the FutureValue.java file found here
(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 Calculatorn");
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) + "n"
+ "Yearly interest rate: " + p.format(interestRate / 100) + "n"
+ "Number of years: " + years + "n"
+ "Future value: " + c.format(futureValue) + "n";
// 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 = 0; i <= months; i++) {
futureValue = (futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
}
return futureValue;
}
}
)
application This exercise guides you through the process of using NetBeans or Eelipse to test and
debug an application. Test the Invoice a.
4.16.24 21st Century Movements for Black Lives.pptx
Calculate Future Investment Value in Java
1. JAVA
Using the FutureValue.java file found here
(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 Calculatorn");
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) + "n"
+ "Yearly interest rate: " + p.format(interestRate / 100) + "n"
+ "Number of years: " + years + "n"
+ "Future value: " + c.format(futureValue) + "n";
// 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();
}
}
2. 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 " +
3. 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 = 0; i <= months; i++) {
futureValue = (futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
}
return futureValue;
}
}
)
application This exercise guides you through the process of using NetBeans or Eelipse to test and
debug an application. Test the Invoice application with invalid data 1. Use NetBeans or Eclipse to
open the ch06_ex1_Invoice project that's in the ex_starts directory. Then, test the Invoice
application with an invalid subtotal like $1000 (enter the dollar sign too). This should cause the
application to crash with a runtime error and to display an error message in the console. 2. Study
the error message. Then, note the line number of the statement in the InvoiceApp class that
caused the crash. 3. Click on the link to that line of code. This should open the Invoice.App.java
file in the code editor and highlight the line of code that caused the error. From this infomation,
determine the cause of the problem and fix it. Set a breakpoint and step through the application 4.
Set a breakpoint on this line of code: double dilacountpercent = awitch... Then, click on the Debug
button in the toolbar. This runs the project with the debugger on. 5. Display the console and enter
a value of 100 for the subtotal when prompted by the application. When you do, the application
runs to the breakpoint and stops. 6. Click the Variables tab to display the Variables window. Then,
note that the choice, customerType, and subtotal variables have been assigned values. 7. Press
the keyboard shortcut for the Step Into button (F7 in NetBeans, F5 in Eclipse) repeatedly to step
through the application one statement at a time. After each step, review the values in the Variables
window to see how they have changed. Note how the application steps through the switch
statement based on the customerType value and the if/else statements based on the subtotal
value. 8. As you continue stepping through the code, note that the IDE steps through the methods
of the Java API as well. Step through this code as much as you'd like. Then, click the Continue
(NetBeans) or Resume (Eclipse) button in the toolbar to continue the execution of the application.
9. Display the console again and enter " y " to continue. Then, enter a value of 50 for the subtotal.
4. 10. Display the Variables window again and inspect the values of the variables.