Question
Exercise 7-3 Modify the Future Value application In this exercise, youll use some of the skills that you learned in this chapter to modify the
Exercise 7-3 Modify the Future Value application
In this exercise, youll use some of the skills that you learned in this chapter to modify the Future Value application.
Work with the increment operator and order or precedence
1. Open the project named ch07_ex3_FutureValue thats in the extra_ex_starts folder. Then, review the code for this project and run it until you understand how it works.
2. Open the Main class. Within the while loop, use the increment operator (++) to increment the counter variable named i.
3. 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.)
4. Run the application to make sure it still works correctly.
Add a yearly fee
5. Before the while loop, declare a constant that stores a yearly fee of $50.
6.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.
7.After the while loop, add a statement that applies currency formatting to the yearly fee and prints it to the console. When youre done, the console should display user input and calculation results like this:
Enter monthly investment: 100
Enter yearly interest rate: 3
Enter number of years: 3
Yearly fee: $50.00
Future value: $3,616.85
package murach.fv;
import java.text.NumberFormat; import java.util.*;
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!"); } }
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