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

Assume the method giveBonus()has been added to the BankAccount class. public cla

ID: 672792 • Letter: A

Question

Assume the method giveBonus()has been added to the BankAccount class.

public class BankAccount(){

    private int balance;

    public BankAccount(){

          balance = 0;

}

  public BankAccount(int initialBalance){

balance = initialBalance;

    }

public void giveBonus()

{

balance = balance + 10.0;

}

}

What will be output from the following statements that use this BankAccount class?

BankAccount premiumAccount = new BankAccount (10);

BankAccount basicAccount = new BankAccount (5);

premiumAccount.giveBonus ();

premiumAccount.giveBonus ();

basicAccount.giveBonus ();

System.out.println (premiumAccount.getBalance());

System.out.println (basicAccount.getBalance());

Explanation / Answer

//First of all one method getBalance is missing from the class.I added myself

class BankAccount {
   private int balance;

   public BankAccount() {
       balance = 0;
   }

   public BankAccount(int initialBalance) {
       balance = initialBalance;
   }

   public void giveBonus() {
       balance = balance + 10;
   }

   public int getBalance() {
       return balance;
   }
  
}

public class Test {
   public static void main(String arsgs[]) {
       BankAccount premiumAccount = new BankAccount (10);   //current premiumAccount:balance = 10
       BankAccount basicAccount = new BankAccount (5);   //current basicAccount:balance = 5
       premiumAccount.giveBonus ();   //increase the balance by 10. premiumAccount:balance = 10+10 = 20
       premiumAccount.giveBonus ();   //ncrease the balance by 10. premiumAccount:balance = 20+10 = 30
       basicAccount.giveBonus ();       //ncrease the balance by 10. basicAccount:balance = 5+10 = 15
       System.out.println (premiumAccount.getBalance()); //print 30
       System.out.println (basicAccount.getBalance());       //print 15
   }
}

//Hence output will be
30
15

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote