Exam 2 Programming Section Write an application to calculate the balance of diff
ID: 3761878 • Letter: E
Question
Exam 2 Programming Section Write an application to calculate the balance of different saving accounts. Design and implement a class called SavingsAccount that contains instance data that represents the account Id, name in the account, balance, annual interest rate, and a Boolean variable that represent whether the account has requested a monitoring service. Define two SavingsAccount constructors, one constructor with empty instance data and no service (false) and another one that initialized the account with Id, name, balance, and service. Include accessor and mutator methods for all instance data. Include a method to calculate the dividends earned for a month by multiplying the balance by the annual interest rate divided by 12. Also include a method to calculate the fee for the monitoring service. For the accounts that requested this service a fee of 5 dollars is monthly deducted from its balance. Finally, include a tost r ing method that returns a one-line description with the account Id, name and balance. Create a driver class called SavingsAccountDemo, whose main method instantiates two SavingsAccount objects (one with empty data, and one with initials values). The user will enter all the data for those two accounts and you need to validate that the initial balance is greater than 0 (zero) using a while loop. When testing, the user should request the monitoring service for one of the account (so you can check if the fee is applied correctly). Set the annual interest rate to 4%, then calculate the monthly dividends, the charges for services, and print the new balances using the toString method. Then, set the interest rate to 5% and calculate next month's dividends and charges and print the new balance for both accounts using again the toString method.Explanation / Answer
Program code:
public class SavingsAccount {
int accountID,balance;
double interestRate;
String name;
boolean monService;
double caclulateInterest(int bal,double inRate)
{
balance=bal;
interestRate=inRate;
double dividend=(balance*interestRate)/12;
return dividend;
}
double caclulateServiceCharge(boolean monService1)
{
boolean monService=monService1;
if(monService)
{
balance=balance-5;
}
return balance;
}
public String toString()
{
return accountID+" "+name+" "+balance;
}
public SavingsAccount()
{
monService=false;
}
public SavingsAccount(int accountID1,String name1,int balance1,boolean monService1)
{
this.accountID=accountID1;
this.name=name1;
this.balance=balance1;
this.monService=monService1;
}
public int getaccountID()
{
return accountID;
}
public String getName()
{ return name;
}
public int getbalance() { return balance; }
public boolean getmonService() { return monService; }
@SuppressWarnings("unused")
private void setAccountID(int accountID1) {
this.accountID=accountID1;
}
@SuppressWarnings("unused")
private void setName(String name1) {
this.name=name1;
}
@SuppressWarnings("unused")
private void setBalance(int balance1) {
//this.accountID=accountID1;
this.balance=balance1;
}
@SuppressWarnings("unused")
private void setmonService(boolean monService1) {
//this.accountID=accountID1;
this.monService=monService1;
}
public static void main(String args[])
{
SavingsAccount s1=new SavingsAccount();
SavingsAccount s2=new SavingsAccount(1234,"Tom",500,true);
//s2.setBalance(1000);
System.out.println("Balnce in account initially="+s2.getbalance());
System.out.println("dividend1="+s2.caclulateInterest(500, 0.04));//interest rate 4%;
System.out.println("dividend2="+s2.caclulateInterest(500, 0.05));//interest rate 5%;
System.out.println("Service charge="+s2.caclulateServiceCharge(true));
System.out.println("AccNumber Name Balance");
System.out.println(s2.toString());
}
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.