Assume the method giveBonus() has been added to the BankAccount class.
public class Raise {
private int annualSalary;
public Raise(){
annualSalary = 0;
} //end constructor
public Raise(int currentSalary){
annualSalary = currentSalary;
} //end constructor
public void giveRaise(){
annualSalary = annualSalary + 500;
} //end giveRaise
public void getSalary(){
return annualSalary;
} //end giveRaise
}
What will be the output from the following statements that use this BankAccount class? (assume there is a getBalance() method that returns the balance)
Raise greatEmployee = new Raise ();
greatEmployee.giveRaise ();
greatEmployee.giveRaise ();
greatEmployee.giveRaise ();
System.out.printIn (greatEmployee.getSalary());
Correct answer is 1500
Explanation:
In testing code, we initialised the variable "greatEmployee" of type class "Raise", so initial value of salary is 0. And we have called the giveRaise() method 3 times. For each call, we increment the salary by 500. So after 3 calls, the value of salary will be 1500. So greatEmployee.getSalary() will returns 1500 and prints it.
Note: Update the return type of getSalary() method to int type. So that we will get the above output.
Mention in comments if any mistakes or errors are found. Thank you.
Get Answers For Free
Most questions answered within 1 hours.