Question#4: Override the clone() method in the BankAccount class to perform deep copying. Verify your code in the main() method by creating a deep copy of a BankAccount object and displaying that the copied object is equivalent to the original object. public class BankAccount implements Cloneable{ private int accountNumber; private int balance; private Date createDate; // constructor for creating a bank account public BankAccount(int accountNumber, int balance) { this.accountNumber = accountNumber; this.balance = balance; this.createDate = new java.util.Date(); } //deposit money public int deposit(int amount) { balance = balance + amount; return balance; } //withdraw money public int withdraw(int amount) throws Exception{ if (amount<0) throw new Exception("Amount cannot be negative"); balance = balance - amount; return balance; } @Override public Object clone() { //Write your code here } }
Question#4: Override the clone() method in the BankAccount class to perform deep copying. Verify your code in the main() method by creating a deep copy of a BankAccount object and displaying that the copied object is equivalent to the original object.
public class BankAccount implements Cloneable{
private int accountNumber;
private int balance;
private Date createDate;
// constructor for creating a bank account
public BankAccount(int accountNumber, int balance) {
this.accountNumber = accountNumber;
this.balance = balance;
this.createDate = new java.util.Date();
}
//deposit money
public int deposit(int amount) {
balance = balance + amount;
return balance;
}
//withdraw money
public int withdraw(int amount) throws Exception{
if (amount<0)
throw new Exception("Amount cannot be negative");
balance = balance - amount;
return balance;
}
@Override
public Object clone() {
//Write your code here
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images