Write a java program that create a class savacnt, use a static variable anintrate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savbal indicating the amount the saver currently has ondep. Provide method calcmonthint to calculate the monthly interest by multiplying the savbal by anintrate divided by 12 this interest should be added to savbal. Provide a static method modiintrate that sets the anintrate to a new value.
Write a program to test class savacnt. Instantiate two savacnt objects, savel and save2, with balances 4000 and 6000, respectively. Set anintrate to 5%, then calculate the monthly interest and print the new balances for both savers. Then set the anintrate to 6%, calculate the next month's interest and print the new balances for both savers.
public class SaveAccount {
public static double anintrate;
private double savbal;
public SaveAccount(double savbal) {
this.savbal = savbal;
}
public double calcmonthint() {
return savbal + savbal * anintrate / 12;
}
public static void modiintrate(double anintrate) {
SaveAccount.anintrate = anintrate;
}
public static void main(String[] args) {
modiintrate(5);
SaveAccount save1 = new SaveAccount(4000);
SaveAccount save2 = new SaveAccount(6000);
System.out.println("Save 1 account after the first month: " + save1.calcmonthint());
System.out.println("Save 2 account after the first month: " + save2.calcmonthint());
modiintrate(6);
System.out.println("Save 1 account after the second month: " + save1.calcmonthint());
System.out.println("Save 2 account after the second month: " + save2.calcmonthint());
}
}
Comments
Leave a comment