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

getApr() should prompt for and input a percentage as, for example, 3.5 for 3.5%.

ID: 3592466 • 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

What are some examples of how these methods would be used in java

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 {

value = compound(value, apr);

printAsMoney(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(double value) {

DecimalFormat d = new DecimalFormat("'$'0.00");

System.out.println(d.format(value));

}

}

Output:

Enter the percentage:
3.5
Enter the investment amount:
1000
$1035.00