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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.