Part B: For Loop Program
Write a program to compute the interest on a bank account. The program will have the following characteristics:
- The user will be prompted to input a single line of text containing the deposit amount ($), interest rate (%), and term (in months or years). All items must be separated by whitespace and the input should not be case sensitive. Interest must be compounded monthly. So, some sample (valid) inputs might be:
- 10000.00 5 36 months
- 5000.00 4.5 2 years
- 45000.00 5.0 24 Months
- If the user has made an input error, you need to inform them of the error and prompt them to re-enter the information. Primary error conditions are:
- Term specified that is not “months” or “years”.
- If values for interest rate or term time ≤ 0.
- For this program, you must use the FOR loop construct to compute the interest.
- You must make sure that interest is computed properly for monthly compounding. If you don’t know what “compound interest” is, Google it. One such site is here.
- Once you have verified that the data is properly entered, compute and print out the interest payment on a month by month basis.
- At the end of the program, print out the beginning balance, final balance, and the cumulative interest earned. Make sure to clearly label each output.
The code written so far is:
import java.util.Scanner;
public class CompoundInterest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double principal = 0;
double rate = 0;
double time = 0;
double compoundInterest = 0;
System.out.print("Enter the Principal amount : ");
principal = input.nextDouble();
System.out.print("Enter the Rate : ");
rate = input.nextDouble();
System.out.print("Enter the Time : ");
time = input.nextDouble();
compoundInterest = principal * Math.pow((1 + rate/100), time);
System.out.println("");
System.out.println("The Compound Interest is : "
+ compoundInterest);
}
}
But I don't know how to get input from the user.