Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 9 Going back to your completed Assignment 7, analyze your code for ar

ID: 3700570 • Letter: A

Question

Assignment 9 Going back to your completed Assignment 7, analyze your code for areas that could be improved with Exception handling. Modify the code to represent the changes listed above. Once you have modified the program, be sure to test the program for proper operation. You should have at least three areas you have modified to utilize Exception techniques.

Assignment 7:

ACCOUNT CLASS:

public class Account

{

   public String accountNo;

   public double accountBal;

   public String accountName;

  

   public Account () {}

  

   public Account(String acct, String accountNo)

   {

       this.accountNo = accountNo;

       this.accountName = null;

       this.accountBal = 0;

   }

  

   public Account(String acct, String name, double balance)

   {

       super();

       this.accountNo = acct;

       this.accountName = name;

       this.accountBal = balance;

   }

  

   public void setBalance(double balance)

   {

       this.accountBal = balance;

   }

  

   public void setName(String name)

   {

       this.accountName = name;

      

   }

  

   public String getacct()

   {

       return accountName;

   }

  

   public String getname()

   {

       return accountName;

   }

  

   public double balance()

   {

       return accountBal;

   }

   // equals

   public boolean equals(Account obj)

   {

       Account other = (Account) obj;

  

  

   if (Double.doubleToLongBits(accountBal) != Double.doubleToLongBits(other.accountBal))

       return false;

  

   if (accountName == null)

   {

       if (other.accountName != null)

           return false;

       }

   else if (!accountName.equals(other.accountName))

       return false;

   if (accountName == null)

   {

       if (other.accountName != null)

           return false;

       }

   else if (!accountName.equals(other.accountName))

       return false;

   return true;

}

}

SAVINGS CLASS:

public class Savings extends Account

{

double interestRate;

Savings() {}

public Savings(double interestRate)

{

this.interestRate=interestRate;

}

public Savings(String accountNo, String accountName,

       double accountBal,double interestRate)

{

super(accountNo, accountName, accountBal);

this.interestRate=interestRate;

}

public double getInterestRate()

{

return interestRate;

}

public void setInterestRate(double interestRate)

{

this.interestRate = interestRate;

}

public String print()

{

return " Savings Account Information: Account Number: "+super.accountNo+

" Balanace : "+super.accountBal+" Account Holder: "+super.accountName+

" Interset Rate: " +this.calcInterest()%.2f;

}

public double calcInterest()

{

return ((this.interestRate * (super.accountBal))/12);

}

}

CHECKINGS CLASS:

public class Checking extends Account

{

   int checkNumber;

   Checking() {}

   public Checking(int checkNumber)

   {

       this.checkNumber=checkNumber;

   }

   public Checking(String accountNo, String accountName, double accountBal, int checkNumber)

   {

       super(accountNo, accountName, accountBal);

       this.checkNumber = checkNumber;

   }

  

   public int getCheckNumber()

   {

       return checkNumber;

   }

  

   public void setCheckNumber(int checkNumber)

   {

       this.checkNumber = checkNumber;

   }

  

   public String print()

   {

       return " Checking Account Information: "+super.accountNo+

       " Current Check Number: "+this.checkNumber+" Account Holder: "+super.accountNo+

       " Balance: " +super.accountBal;

   }

}

Explanation / Answer

//Account.java
public class Account
{
   public String accountNo;
   public double accountBal;
   public String accountName;

   public Account () {}

   /*Throw exception if balance is below zero*/
   public Account(String acct, String accountNo)
           throws InvalidBalanceException

   {
       this.accountNo = accountNo;
       this.accountName = null;
       setBalance(0);
   }

   /*Throw exception if balance is below zero*/
   public Account(String acct, String name, double balance)
           throws InvalidBalanceException
   {

       super();
       this.accountNo = acct;
       this.accountName = name;
      setBalance(balance);
   }

   /*Throw exception if balance is below zero*/
   public void setBalance(double balance)
           throws InvalidBalanceException
   {
       if(balance<0)
           throw new InvalidBalanceException("Invalid Balance");

       else
           this.accountBal = balance;
   }

   public void setName(String name)
   {
       this.accountName = name;

   }

   public String getacct()
   {
       return accountName;
   }

   public String getname()
   {
       return accountName;
   }

   public double balance()
   {
       return accountBal;
   }
   // equals
   public boolean equals(Account obj)
   {
       Account other = (Account) obj;
       if (Double.doubleToLongBits(accountBal) != Double.doubleToLongBits(other.accountBal))
           return false;

       if (accountName == null)
       {
           if (other.accountName != null)
               return false;
       }
       else if (!accountName.equals(other.accountName))
           return false;
       if (accountName == null)
       {
           if (other.accountName != null)
               return false;
       }
       else if (!accountName.equals(other.accountName))
           return false;
       return true;
   }
}

----------------------------------------------------

//Savings.java
public class Savings extends Account
{
   double interestRate;

   Savings() {}
   public Savings(double interestRate)
   {
       this.interestRate=interestRate;
   }
   /*Throw exception if balance is below zero*/
   public Savings(String accountNo, String accountName,
           double accountBal,double interestRate)
                  throws InvalidBalanceException
   {
       super(accountNo, accountName, accountBal);
       this.interestRate=interestRate;
   }

   public double getInterestRate()
   {
       return interestRate;
   }
   public void setInterestRate(double interestRate)
   {
       this.interestRate = interestRate;
   }
   public String print()
   {
       return " Savings Account Information: Account Number: "+super.accountNo+
               " Balanace : "+super.accountBal+" Account Holder: "+super.accountName+
               " Interset Rate: " +this.calcInterest()%.2f;
   }

   public double calcInterest()
   {
       return ((this.interestRate * (super.accountBal))/12);
   }
}

----------------------------------------------------

//Checking.java
public class Checking extends Account
{

   int checkNumber;

   Checking() {}
   public Checking(int checkNumber)
   {
       this.checkNumber=checkNumber;
   }
   /*Throw exception if balance is below zero*/
   public Checking(String accountNo, String accountName, double accountBal, int checkNumber)
           throws InvalidBalanceException
   {

       super(accountNo, accountName, accountBal);
       this.checkNumber = checkNumber;
   }

   public int getCheckNumber()
   {
       return checkNumber;
   }

   public void setCheckNumber(int checkNumber)
   {
       this.checkNumber = checkNumber;
   }

   public String print()
   {
       return " Checking Account Information: "+super.accountNo+
               " Current Check Number: "+this.checkNumber+" Account Holder: "+super.accountNo+
               " Balance: " +super.accountBal;
   }

}

----------------------------------------------------

//InvalidBalanceException.java
public class InvalidBalanceException extends Exception
{
   public InvalidBalanceException(String msg) {
       super(msg);
   }
}

----------------------------------------------------

Sample Output:

Enter account number :
12345
Enter account name :
johnson
Enter account balance :
-100
Exception in thread "main" InvalidBalanceException: Invalid Balance