Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise, you’ll use some of the skills that you learned in this chapter

ID: 3882102 • Letter: I

Question

In this exercise, you’ll 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 that’s 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 you’re done, the console should display user input and calculation results like this:

Code looks like this:

Code:

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!");
}
}

Help is appreciated, I'm stuck. Thanks so much.

Enter monthly investment: 100 Enter yearly interest rate: 3 Enter number of years: Yearly fee: Future value: $50.00 $3,616.85

Explanation / Answer

FutureValueCalculator.java

import java.text.NumberFormat;

import java.util.Scanner;

public class FutureValueCalculator {

public static void main(String[] args) {

// Declaring variables

int months, monthlyInvestment, noOfYears, i = 0;

final double YEARLY_FEE = 50;

double futureVal = 0.0, interest;

// Creating a Numberformat instance

NumberFormat fmt = NumberFormat.getCurrencyInstance();

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

while(true)

{

// Getting the input entered by the user

System.out.print(" Enter monthly investment :");

monthlyInvestment = sc.nextInt();

System.out.print("Enter yearly investment rate :");

interest = sc.nextDouble();

System.out.print("Enter no of years :");

noOfYears = sc.nextInt();

months = noOfYears * 12;

// This loop continues to run for desired no of months

while (i != months) {

futureVal += monthlyInvestment;

i++;

futureVal += futureVal * (interest / 1200);

if (i % 12 == 0) {

futureVal -= YEARLY_FEE;

}

}

// Displaying the future value

System.out.println("Yearly fee :" + fmt.format(YEARLY_FEE));

System.out.println("Future Value :" + fmt.format(futureVal));

//Getting the character from the user 'Y' or 'y' or 'N' or 'n'

System.out.print("Do you want to continue(Y/N) ::");

char ch = sc.next(".").charAt(0);

if(ch=='Y'||ch=='y')

continue;

else

{

System.out.println(":: Bye! ::");

break;

}

}

}

}

__________________

Output:


Enter monthly investment :100
Enter yearly investment rate :3
Enter no of years :3
Yearly fee :$50.00
Future Value :$3,616.85
Do you want to continue(Y/N) ::n
:: Bye! ::

_______________Could you plz rate me well.Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote