Write a program that takes two numbers from the Java console representing, respe
ID: 3787531 • Letter: W
Question
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate.) Do not prompt anything from the user before reading in the investment and interest rate. Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, 15 and 20 years using the following formula: future value = investment * (1 + interest rate)^year We will assume the interest rate is an annual rate and is compounded annually. Use NumberFormat to output the $: https://docs.oracle.com/javase/7/docs/api/javax/swing/text/NumberFormatter.html Only display the output like this:
Year 5: $31,907.04 Year 10: $40,722.37 Year 15: $51,973.20 Year 20: $66,332.44
substituting the correct amount for the value of the investment after the given period. The program must ignore any input other than decimal numbers.
Scanner s = new Scanner(System.in); while(!s.hasNextDouble()) s.next();
Lastly, don't forget to take out the package when you submit to mimir. Pay attention to the spacing in the output.
Skeleton Code : /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
package interestrate; import java.util.Scanner; import javax.swing.text.NumberFormatter; import java.text.NumberFormat; import java.util.Locale; /** * * @author random */ public class InterestRate { /** * @param args the command line arguments */
public static void main(String[] args)
{ NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); Scanner s = new Scanner(System.in);
//fill in the logic here
//Display like this exactly
System.out.println("Year " + + ": " + nf.format()); } }
Explanation / Answer
package interestrate;
import java.util.Scanner;
import javax.swing.text.NumberFormatter;
import java.text.NumberFormat;
import java.util.Locale;
/** * * @author random */
public class InterestRate
{
public static double getFutureValueByYear(double investment, double interestRate, int year)
{
double futureValue = investment*Math.pow((1+interestRate), year);
return futureValue;
}
/** * @param args the command line arguments */
public static void main(String[] args)
{
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
Scanner s = new Scanner(System.in);
// loop until s doesn't have double
while(!s.hasNextDouble())
s.next();
double investment = s.nextDouble();
while(!s.hasNextDouble())
s.next();
double interestRate = s.nextDouble();
double futureValue;
for(int year = 5; year <= 25; year = year+5)
{
futureValue = getFutureValueByYear(investment, interestRate,year);
System.out.println("Year " + year + ": " + nf.format(futureValue));
}
}
}
/*
Sample output
20000
.065
Year 5: $27,401.73
Year 10: $37,542.75
Year 15: $51,436.82
Year 20: $70,472.90
Year 25: $96,553.98
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.