getApr() should prompt for and input a percentage as, for example, 3.5 for 3.5%.
ID: 3599457 • 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.
How do i get 20 lines each with an investment value of 20 years later this is what i have so far
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double apr = getApr();
double value = getInvestment();
double compoundAmount = compound(value,apr);
printAsMoney(compoundAmount);
if (apr<=0){
System.out.println("The Percent should not be 0 or Negative:");
System.exit(0);
}
else {
if (value<=0){
System.out.println("The Investment should not be 0 or negative");
System.exit(0);
}
}
}
private static void printAsMoney(double compoundAmount) {
DecimalFormat df = new DecimalFormat("'$'0.00");
System.out.print("After 20 years, the investment will be worth " + df.format(compoundAmount));
}
private static double compound(double value, double apr) {
return (value*Math.pow(1+apr/12,20*12));
}
private static double getInvestment() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Investment amount");
double value = sc.nextDouble();
return value;
}
private static double getApr() {
Scanner sc = new Scanner (System.in);
System.out.println("Enter the Percentage");
double apr = sc.nextDouble();
return apr/100;
}
}
Explanation / Answer
MoneyInvestment.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class MoneyInvestment {
public static void main(String[] args) {
double apr = getApr();
if (apr <= 0) {
System.out.println("percentage should not be negative");
} else {
double value = getInvestment();
if (value <= 0) {
System.out.println("Investment amount should not be negative");
} else {
double total = 0;
for(int i=1;i<=20;i++) {
value = compound(value, apr);
total = total + value;
printAsMoney(i, value);
}
}
}
}
public static double getApr() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the percentage: ");
double apr = scan.nextDouble();
return apr;
}
public static double getInvestment() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the investment amount: ");
double value = scan.nextDouble();
return value;
}
public static double compound(double value, double apr) {
return (1 + apr/100) * value;
}
public static void printAsMoney(int year, double value) {
DecimalFormat d = new DecimalFormat("'$'0.00");
System.out.println("After "+year+" years, the investment will be worth"+d.format(value));
}
}
Output:
Enter the percentage:
3.5
Enter the investment amount:
1000
After 1 years, the investment will be worth$1035.00
After 2 years, the investment will be worth$1071.22
After 3 years, the investment will be worth$1108.72
After 4 years, the investment will be worth$1147.52
After 5 years, the investment will be worth$1187.69
After 6 years, the investment will be worth$1229.26
After 7 years, the investment will be worth$1272.28
After 8 years, the investment will be worth$1316.81
After 9 years, the investment will be worth$1362.90
After 10 years, the investment will be worth$1410.60
After 11 years, the investment will be worth$1459.97
After 12 years, the investment will be worth$1511.07
After 13 years, the investment will be worth$1563.96
After 14 years, the investment will be worth$1618.69
After 15 years, the investment will be worth$1675.35
After 16 years, the investment will be worth$1733.99
After 17 years, the investment will be worth$1794.68
After 18 years, the investment will be worth$1857.49
After 19 years, the investment will be worth$1922.50
After 20 years, the investment will be worth$1989.79
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.