Modify the application 1. Open the Main class. Within the while loop, use the in
ID: 3884886 • Letter: M
Question
Modify the application
1. Open the Main class. Within the while loop, use the increment operator (++) to increment the counter variable named i.
2. Combine the first three statements in the while loop into a single statement that calculates the future value. To get this to work, you can use parentheses to control the order of precedence of the operations. (First, add the monthly investment. Then, add the monthly interest.)
3. Run the application to make sure it still works correctly.
Add a yearly fee
4. Before the while loop, declare a constant that stores a yearly fee of $50.
5. Within the while loop, add an if statement that uses the modulus operator to check whether the current month is a multiple of 12. If so, subtract the yearly fee from the future value.
6. After the while loop, add a statement that applies currency formatting to the yearly fee and prints it to the console.
---------------------------
package murach.fv;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// display a welcome message
System.out.println("Welcome to the Future Value Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// get input from user
System.out.print("Enter monthly investment: ");
double monthlyInvestment = Double.parseDouble(sc.nextLine());
System.out.print("Enter yearly interest rate: ");
double yearlyInterestRate = Double.parseDouble(sc.nextLine());
System.out.print("Enter number of years: ");
int years = Integer.parseInt(sc.nextLine());
// convert yearly values to monthly values
double monthlyInterestRate = yearlyInterestRate / 12 / 100;
int months = years * 12;
// calculate the future value
double futureValue = 0;
int i = 1;
while (i <= months) {
futureValue = futureValue + monthlyInvestment;
double monthlyInterestAmount =
futureValue * monthlyInterestRate;
futureValue = futureValue + monthlyInterestAmount;
i = i + 1;
}
// format and display the result
System.out.println("Future value: " +
NumberFormat.getCurrencyInstance().format(futureValue));
System.out.println();
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
System.out.println("Bye!");
}
}
Explanation / Answer
//Here is your modified code as per changes required
package fv;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// display a welcome message
System.out.println("Welcome to the Future Value Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// get input from user
System.out.print("Enter monthly investment: ");
double monthlyInvestment = Double.parseDouble(sc.nextLine());
System.out.print("Enter yearly interest rate: ");
double yearlyInterestRate = Double.parseDouble(sc.nextLine());
System.out.print("Enter number of years: ");
int years = Integer.parseInt(sc.nextLine());
// convert yearly values to monthly values
double monthlyInterestRate = (yearlyInterestRate / 12) / 100;
int months = years * 12;
// calculate the future value
double futureValue = 0;
int i = 1;
final int yearlyFee = 50;
while (i <= months) {
/* futureValue = futureValue + monthlyInvestment;
double monthlyInterestAmount =
futureValue * monthlyInterestRate;
futureValue = futureValue + monthlyInterestAmount; new updated code */
futureValue=(futureValue+monthlyInvestment)+(futureValue+monthlyInvestment)*monthlyInterestRate;
if(i%12==0) {
futureValue = futureValue -yearlyFee;
}
// i = i + 1; using increment operator ++
i++;
}
// format and display the result
System.out.println("Future value: " +
NumberFormat.getCurrencyInstance().format(futureValue));
System.out.println();
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
System.out.println("Bye!");
}
}
output after and before modification are same as follow
Welcome to the Future Value Calculator
Enter monthly investment: 5000
Enter yearly interest rate: 10
Enter number of years: 5
Future value: $390,103.77
Continue? (y/n): n
Bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.