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

Consider the following Java code. public class BankAccount { private int number;

ID: 3629104 • Letter: C

Question

Consider the following Java code.

public class BankAccount
{
private int number;
private int balance;
public BankAccount(int number, int balance)
{
this.number = number;
this.balance = balance;
}
public void deposit(int amount)
{
balance += amount;
}
public void withdraw(int amount)
{
balance -= amount;
}
public int balance()
{
return balance;
}
public String toString()
{
return "BankAcount(" + number + ", " + balance + ")";
}
}

(a) (5 points) Define a pointcut named balanceChanged to designate an execution of the mutation methods of class BankAccount. A mutation method is a method that may change the state of an object.


(b) (10 points) Write an advice to log the balance changes of all BankAccount objects. Your log message should contain the account number and the new balance of the account.



(c) (15 points) Define an aspect to prevent from withdrawing more than the current balance. That is, the maximum amount that can be withdrawn is the current balance. If the requested amount is larger than the current balance, only the amount of current balance should be returned by the withdraw method; i.e., the balance should never become negative. You may assume that the requested amount be non-negative.

Explanation / Answer

import java.io.*;

class BankAccount

{

private int number;

private int balance;

public BankAccount(int number, int balance)

{

this.number = number;

this.balance = balance;

}

public void deposit(int amount)

{

balance += amount;

}

public void withdraw(int amount)

{

if(amount>balance)

{

System.out.print("Withdraw limit"+balance);

balance=0;

}

else

balance -= amount;

}

public int balance()

{

return balance;

}

public String toString()

{

return "BankAcount(" + number + ", " + balance + ")";

}

public void balanceChanged(BankAccount obj)

{

this.number=obj.number;

this.balance=obj.balance;

}

}

public class BankTest

{

public static void main(String[] args)

{

// TODO Auto-generated method stub

BankAccount acc1=new BankAccount(1452,5000);

acc1.deposit(200);

acc1.withdraw(200);

System.out.println(acc1);

acc1.withdraw(5200);

}

BankAcount(1452, 5000)

Withdraw limit5000

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