1) Add a Scanner and prompt the user for the three values (monthly amount to inv
ID: 3671371 • 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.
//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
import java.util.Scanner; public class Ex_2_21 { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); //Prompt the user to enter investment amount, annual interest rate, and number of years System.out.println("Enter investment amount:"); System.out.println("Enter annual interest rate:"); System.out.println("Enter number of years:"); float investmentamount = input.nextFloat(); float interestrate = input.nextFloat(); float numberofyears = input.nextFloat(); float years = numberofyears * 12; //Formula to calculate the accumulated value float futureInvestmentValue = (float) (investmentamount * Math.pow((years), 1 + interestrate)); //Print result System.out.println("The accumulated value is " + futureInvestmentValue); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.