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

Consider the class BankAccount, for use in a bank\'s account record system: publ

ID: 665609 • Letter: C

Question

Consider the class BankAccount, for use in a bank's account record system: public class BankAccount The accName is the username of the account holder such as BillGates or JackSmith. private String accName private int accNumber private int balance; public String getName freturn accName public int getBalance tre turn balance constructor and other details omitted The records are sorted in alphabetical order by the account names. Write a method public int account Balance BankAccount DJ accList, String accName) that uses an efficient search technique to search the sorted account list acclist for the bank account with the account name accName, and returns its balance. If accName is not in accList the method should throw a suitable exception.

Explanation / Answer

public int accountBalance(BankAccount[] accList, String accName) throws NameNotFoundException{
int start = 0;
int end = accList.length - 1;
while(start <= end) {
   int mid = (start + end) / 2;
   if (accName.equals(accList[mid].getName())) {
       return accList[mid].getBalance();
   }
   if(accName.compareTo(accList[mid].getName()) < 0){
       end = mid - 1;
   }
   else {
       start = mid + 1;
   }
}
throw new NameNotFoundException();
}