Question
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)) {
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 = 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); System.out.println();
// 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 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.next(); sc.nextLine(); // discard any other data entered on the line System.out.println(); } }
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 double getDouble(Scanner sc, String prompt) { double d = 0.0; boolean isValid = false; while (isValid == false) { System.out.print(prompt); if (sc.hasNextDouble()) { d = sc.nextDouble(); isValid = true; } else { System.out.println("Error! Invalid number. Try again."); } sc.nextLine(); // discard any other data entered on the line } return d; }
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 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. Try again."); } sc.nextLine(); // discard any other data entered on the line } 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); //System.out.println("month " + i + " futureValue: " + futureValue); } return futureValue; } }
In Java I am trying to test and debug this The get Double method contains syntax errors
use netbeans to find and fix them.
Then go to the calculateFutureValue method and add a println statement within the loop that prints the value of of the month and the future value each time the loop is exucuted
the Comment out the println so it no longer prints to the output window
then fix the logic error
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started