getApr() should prompt for and input a percentage as, for example, 3.5 for 3.5%.
ID: 3592794 • Letter: G
Question
getApr() should prompt for and input a percentage as, for example, 3.5 for 3.5%. It should return the percentage as a double, as, for example, .035 for input of 3.5. The method should print an error message and exit the program if the percentage is negative or 0.
getInvestment() should prompt for and input an investment amount as a double and return it. If the amount is negative or 0, the method should print an error message and exit the program.
printAsMoney(double value) should format the argument as currency using a DecimalFormat and print it, staying on the same line.
compound(double value, double apr) should return a double, (1+apr) * value
The program should use those methods to input the APR and the investment, then output the value of the investment after each of 20 years, in the following format:
After nn years, the investment will be worth $nn.nn.
Explanation / Answer
CompundInvestment.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class CompundInvestment {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//Declaring variables
double apr, investment, compoundAmt;
//calliong the methods
apr = getApr();
investment = getInvestment();
compoundAmt = compound(investment, apr);
printAsMoney(compoundAmt);
}
//This method will display the amount in decimal format where precision limiting to two decimal places
private static void printAsMoney(double compoundAmt) {
// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat(".00");
System.out.print("After 20 years, the investment will be worth $" + df.format(compoundAmt));
}
//Calculating the compound interest
private static double compound(double investment, double apr) {
return (investment * Math.pow((1 + apr / 12), 20 * 12));
}
//Getting the investment entered by the user
private static double getInvestment() {
double investment;
System.out.print("Enter Invesetment Amount :$");
investment = sc.nextDouble();
if (investment < 0) {
System.out.println("** Invalid.Must be Positive **");
System.exit(1);
}
return investment;
}
//Getting the rate entered by the user
private static double getApr() {
double apr;
System.out.print("Enter Apr :");
apr = sc.nextDouble();
if (apr < 0) {
System.out.println("** Invalid.Must be Positive **");
System.exit(1);
}
return apr / 100;
}
}
________________________
Output#1:
Enter Apr :-3.5
** Invalid.Must be Positive **
_________________
Output#2:
Enter Apr :3.5
Enter Invesetment Amount :$-10000
** Invalid.Must be Positive **
_________________
Output#3:
Enter Apr :3.5
Enter Invesetment Amount :$10000
After 20 years, the investment will be worth $20117.02
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.