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

1) Add a Scanner and prompt the user for the three values (monthly amount to inv

ID: 3671793 • Letter: 1

Question

1) Add a Scanner and prompt the user for the three values (monthly amount to invest in $, number of years to invest, annual rate of return in %) and run the calculations using the user's supplied values. (Hint: remember to convert your rate from % to decimal...)

2) Add a getPercentInstance() number formatter to correctly display the user's interest rate as a %.

Following code is what I have so far but I can't figure out the second part where it adds a getPercentInstance() number formatter. Please read the question first before start coding.

//RetirementCalc.java
//
import java.util.Scanner;


public class RetirementCalc
{
public static void main(String[] args)
{
//===========================================================

Scanner scan = new Scanner(System.in);
double monthly, rate;
int years;

//===========================================================

System.out.println("How much would you like to invest each month?");
monthly = scan.nextDouble;
  
double total = monthly * ((Math.pow(1 + rate/12, years*12) - 1) / (rate/12) );
//===========================================================
//
//===========================================================


System.out.println("Saving $" +monthly + " per month at " + rate* 100 +"% for " + years + " years will total $" + total);
getPercentInstance
}
}

Explanation / Answer

/**The java program that prompts user to enter principal amount, rate and how many years and display the total amount */

//RetirementCalc.java
import java.text.NumberFormat;
import java.util.Scanner;
public class RetirementCalc
{
   public static void main(String[] args)
   {
       //Create an instance of Scanner class
       Scanner scan = new Scanner(System.in);      
       double principal, rate;
       int years;
      
       //prompt for principal
       System.out.println("How much would you "
               + "like to invest?");
       principal = Double.parseDouble(scan.nextLine());
      
       //prompt for rate
       System.out.println("How much is interest rate?");
       rate = Double.parseDouble(scan.nextLine());
      
       rate=rate/100.0;
      
       //prompt for years
       System.out.println("How many years?");
       years = Integer.parseInt(scan.nextLine());
      
       //Calculate total amount
       double total = principal *Math.pow((1+rate/12),years*12);
      
       //Call the method getPercentInstance
       NumberFormat percent=getPercentInstance();
      
       //Call format method on rate       
       System.out.println("Saving $" +principal + " per month at "
       + percent.format(rate) + " for "
       + years + " years will total $" + total);      
   }

   /**The method getPercentInstance that returns the object of the NumberFormat*/
   private static NumberFormat getPercentInstance()
   {
       //Create an instance of NumberFormat using getPercentInstance method
       //return percent
       NumberFormat percent=NumberFormat.getPercentInstance();      
       return percent;
   }
}//end of the class

-----------------------------------   ----------------------------------- -----------------------------------

Sample Output:

How much would you like to invest?
5000
How much is interest rate?
5
How many years?
10
Saving $5000.0 per month at 5% for 10 years will total $8235.0474884514