public class InsufficientFundsException extends Exception{
InsufficientFundsException(double balance)
{
System.out.println("insufficient funds .... available balance : "+balance);
}
}
// Account.java
public class Account throws InsufficientFundsException{
private int acctNo;
private String owner;
private double balance;
public Account(){
acctNo = 00;
owner = " ";
balance= 0.0;
}
public Account(int ac, String own, double bal){
acctNo = ac;
owner = own;
balance = bal;
}
public void Deposit(double amount){
balance = balance + amount;
}
public void Withdraw (double amount){
try {
if(amount>balance)
{
throw new InsufficientFundsException(balance);
}
else
balance =balance - amount;
}
catch(InsufficientFundsException e)
{
System. out.println(e.printStackTrace());
}
}
public int getAcctNo() {
return acctNo;
}
public void setAcctNo(int ac) {
acctNo = ac;
}
public String getOwner() {
return owner;
}
public void setOwner(String own) {
owner = own;
}
public double getBalance() {
return balance;
}
public void setBalance(double bal) {
balance = bal;
}
public void display(){
System.out.println("Account Number = " + getAcctNo());
System.out.println("Account Owner = " + getOwner());
System.out.println("Account Balance = $" + getBalance());
System.out.println();
}
}
public class TestExceptions{
public static void main(String args []){
Account a1;
a1 = new Account(2222, "Jimmy Zulu", 1000);
a1.Deposit(100);
a1.display();
Account a2;
a2 = new Account();
a2.setAcctNo(3333);
a2.setOwner("John Mwemba");
a2.setBalance(500);
a2.Deposit(100);
a2.Withdraw(900);
a2.display();
}
}
Solution
public class InsufficientFundsException extends Exception{
InsufficientFundsException(double balance)
{
System.out.println("insufficient funds .... available balance : "+balance);
}
}
// Account.java
public class Account throws InsufficientFundsException{
private int acctNo;
private String owner;
private double balance;
public Account(){
acctNo = 00;
owner = " ";
balance= 0.0;
}
public Account(int ac, String own, double bal){
acctNo = ac;
owner = own;
balance = bal;
}
public void Deposit(double amount){
balance = balance + amount;
}
public void Withdraw (double amount){
try {
if(amount>balance)
{
throw new InsufficientFundsException(balance);
}
else
balance =balance - amount;
}
catch(InsufficientFundsException e)
{
System. out.println(e.printStackTrace());
}
}
public int getAcctNo() {
return acctNo;
}
public void setAcctNo(int ac) {
acctNo = ac;
}
public String getOwner() {
return owner;
}
public void setOwner(String own) {
owner = own;
}
public double getBalance() {
return balance;
}
public void setBalance(double bal) {
balance = bal;
}
public void display(){
System.out.println("Account Number = " + getAcctNo());
System.out.println("Account Owner = " + getOwner());
System.out.println("Account Balance = $" + getBalance());
System.out.println();
}
}
public class TestExceptions{
public static void main(String args []){
Account a1;
a1 = new Account(2222, "Jimmy Zulu", 1000);
a1.Deposit(100);
a1.display();
Account a2;
a2 = new Account();
a2.setAcctNo(3333);
a2.setOwner("John Mwemba");
a2.setBalance(500);
a2.Deposit(100);
a2.Withdraw(900);
a2.display();
}
}

public class InsufficientFundsException extends Exception{Insuffic.pdf

  • 1.
    public class InsufficientFundsExceptionextends Exception{ InsufficientFundsException(double balance) { System.out.println("insufficient funds .... available balance : "+balance); } } // Account.java public class Account throws InsufficientFundsException{ private int acctNo; private String owner; private double balance; public Account(){ acctNo = 00; owner = " "; balance= 0.0; } public Account(int ac, String own, double bal){ acctNo = ac; owner = own; balance = bal; } public void Deposit(double amount){ balance = balance + amount; } public void Withdraw (double amount){ try { if(amount>balance) { throw new InsufficientFundsException(balance); } else balance =balance - amount; } catch(InsufficientFundsException e) {
  • 2.
    System. out.println(e.printStackTrace()); } } public intgetAcctNo() { return acctNo; } public void setAcctNo(int ac) { acctNo = ac; } public String getOwner() { return owner; } public void setOwner(String own) { owner = own; } public double getBalance() { return balance; } public void setBalance(double bal) { balance = bal; } public void display(){ System.out.println("Account Number = " + getAcctNo()); System.out.println("Account Owner = " + getOwner()); System.out.println("Account Balance = $" + getBalance()); System.out.println(); } } public class TestExceptions{ public static void main(String args []){ Account a1; a1 = new Account(2222, "Jimmy Zulu", 1000); a1.Deposit(100); a1.display(); Account a2; a2 = new Account();
  • 3.
    a2.setAcctNo(3333); a2.setOwner("John Mwemba"); a2.setBalance(500); a2.Deposit(100); a2.Withdraw(900); a2.display(); } } Solution public classInsufficientFundsException extends Exception{ InsufficientFundsException(double balance) { System.out.println("insufficient funds .... available balance : "+balance); } } // Account.java public class Account throws InsufficientFundsException{ private int acctNo; private String owner; private double balance; public Account(){ acctNo = 00; owner = " "; balance= 0.0; } public Account(int ac, String own, double bal){ acctNo = ac; owner = own; balance = bal; } public void Deposit(double amount){ balance = balance + amount; } public void Withdraw (double amount){
  • 4.
    try { if(amount>balance) { throw newInsufficientFundsException(balance); } else balance =balance - amount; } catch(InsufficientFundsException e) { System. out.println(e.printStackTrace()); } } public int getAcctNo() { return acctNo; } public void setAcctNo(int ac) { acctNo = ac; } public String getOwner() { return owner; } public void setOwner(String own) { owner = own; } public double getBalance() { return balance; } public void setBalance(double bal) { balance = bal; } public void display(){ System.out.println("Account Number = " + getAcctNo()); System.out.println("Account Owner = " + getOwner()); System.out.println("Account Balance = $" + getBalance()); System.out.println();
  • 5.
    } } public class TestExceptions{ publicstatic void main(String args []){ Account a1; a1 = new Account(2222, "Jimmy Zulu", 1000); a1.Deposit(100); a1.display(); Account a2; a2 = new Account(); a2.setAcctNo(3333); a2.setOwner("John Mwemba"); a2.setBalance(500); a2.Deposit(100); a2.Withdraw(900); a2.display(); } }