Assume you’re required to implement an Abstract Data Type (ADT) for a checking a
ID: 3574436 • Letter: A
Question
Assume you’re required to implement an Abstract Data Type (ADT) for a checking account. The requirement is that the ADT should provide operations such as deposit, withdraw and check balance. Since this is a checking account, you are required to enforce an invariant that the balance is never negative. With this invariant in mind, state a contract (in terms of precondition, postcondition and side effects) on the “withdraw” operation: public void withdraw (double amount).
Problem 3 15pt] Assume you're required to implement an Abstract Data Type (ADT) for a checking ac- count. The requirement is that the ADT should provide operations such as deposit, withdraw and check balance. Since this is a checking account, you are required to enforce an invariant that the balance is never negative. With this invariant in mind, state a contract (in terms of precondition, postcondition and side effects) on the "withdraw" operation: public void withdraw (double amount)Explanation / Answer
SOURCE CODE:
public class Account {
private int acc_no;
private String Name;
private String address;
private double balance;
public Account(int acc_no, String Name, String address) {
this.acc_no = acc_no;
this.Name = Name;
this.address = address;
this.balance = 0.0;
}
public Account(int acc_no, String Name, String address, double balance) {
this.acc_no = acc_no;
this.Name = Name;
this.address = address;
if(balance<0.0)
this.balance = 0.0;
else
this.balance = balance;
}
public void checkBalance()
{
System.out.println("Current Balance of "+this.Name+" = "+this.balance);
}
public void withdraw(double amount)
{
if((this.balance-amount)>0.0)
{
System.out.println(" Withdrawl Amount: "+amount);
this.balance=this.balance-amount;
}
else
System.out.println("Your withdrawl amount has excedded the current balance");
this.checkBalance();
}
public void deposit(double amount)
{
this.balance=this.balance+amount;
System.out.println(" Deposit Amount: "+amount);
this.checkBalance();
}
public double getBalance() {
return balance;
}
public String getAddress() {
return address;
}
public String getName() {
return Name;
}
public int getAcc_no() {
return acc_no;
}
}
--------------------------------------------------------------------------------------------------------
public class AccountDriver {
public static void main(String args[])
{
Account a1 = new Account(16370005,"Alik Pramanick","Krishnanagar,WB");
Account a2 = new Account(16370001,"Soubhik Biswas","Madanpur,WB",8900.00);
Account a3 = new Account(16370002,"Koushambi Bhowmick","Sodpur,WB",7000.00);
a1.checkBalance();
a1.withdraw(9000.00);
a2.withdraw(6000);
a3.withdraw(700.00);
a1.deposit(91000.00);
a1.withdraw(50000.00);
}
}
SAMPLE OUTPUT:
Current Balance of Alik Pramanick = 0.0
Your withdrawl amount has excedded the current balance
Current Balance of Alik Pramanick = 0.0
Withdrawl Amount: 6000.0
Current Balance of Soubhik Biswas = 2900.0
Withdrawl Amount: 700.0
Current Balance of Koushambi Bhowmick = 6300.0
Deposit Amount: 91000.0
Current Balance of Alik Pramanick = 91000.0
Withdrawl Amount: 50000.0
Current Balance of Alik Pramanick = 41000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.