use java jgrasp A bank offers 65 interest on a savings accou
Solution
To execute the program please save the below progam with name Calculation.java and then run it to produce the out put for the desired input.
Program:
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Class to take input from user (amount and yearlyDeposit) and prints yearly amount in tabular form
*
* @author Ritesh Satija
* @Date 11th Oct, 2016
*
*/
public class Calculation {
public static void main(String args[]) {
//Scanner to read input
//written in try with resources to handle resource leak and handle exception
try(Scanner sc=new Scanner(System.in);){
System.out.println(\"How much would you like to start with:\");
float amount = sc.nextFloat();
System.out.println(\"How much do you deposit each year:\");
float yearlyDeposit = sc.nextFloat();
System.out.println(\"The Bank uses 6.5% interest.\");
System.out.println(\"***************************************************************\");
float rate = 6.5f;
for(int i=0; i<25; i++) {
//calling method to calculate interest
float interest = calculateInterest(amount, rate);
//print in tabular form
// \\t refers to a tab character
System.out.println(\"Year \"+(i+1)+\"\\t Starts \"+amount+\" \\t Interest \"+interest+\" \\t Total \"+(amount+interest));
//updating amount for the next pass
amount = amount + interest + yearlyDeposit;
}
} catch(InputMismatchException e) {
System.out.println(\"Bad Input Dying.\");
}
}
/**
* Method to calculate interest for 1 year
*
* @param amount
* @param rate
* @return interest
*/
private static float calculateInterest(float amount, float rate) {
float interest;
int timeInyears = 1;
//formula for interest = amount * rate * time /100
interest = (amount * rate * timeInyears)/100;
return interest;
}
}

