use java, jgrasp A bank offers 6.5% interest on a savings account, compounded an
ID: 3922453 • Letter: U
Question
use java, jgrasp A bank offers 6.5% interest on a savings account, compounded annually. Ask the user for an initial amount to invest and a yearly investment amount. Then using these two values, print out a table that show how much money a person will have after each year for the first 25 years. Your table should indicate for each year the Initial amount, the new interest and the total at the end. For example: How much would you like to start with: 1000 How much do you deposit each year: 500 The bank uses 6.5% interest.Explanation / Answer
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
// refers to a tab character
System.out.println("Year "+(i+1)+" Starts "+amount+" Interest "+interest+" 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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.