Question: I have an assignment due in two days. The assignment is to create in java a BankAccount class, a SavingsAccount class that extends from the

I have an assignment due in two days. The assignment is to create in java a BankAccount class, a SavingsAccount class that extends from the BankAccount class, and a driver class called FinalExam. The BankAccount and SavingsAccount classes are supposed to handle deposits and withdrawals as well as a NegativeAmountException and a InsufficientFundsException. Here is what I have:

1. BankAccount class

class NegativeAmountException extends Exception // creates NegativeAmountException { public NegativeAmountException() { } public NegativeAmountException(String message) { super(message); } }

class InsufficientFundsException extends Exception //creates InsufficientFundsException { public InsufficientFundsException() { } public InsufficientFundsException(String message) { super(message); } }

public class BankAccnt { String name; double balance; public BankAccnt(String name) throws NegativeAmountException { this.name = name; balance = 0; } public BankAccnt(String name, double balance) throws NegativeAmountException { this.name = name; if(balance < 0) { throw new NegativeAmountException("The amount entered cannot be negative"); } this.balance = balance; } public void deposit(double amount) throws NegativeAmountException { if(amount < 0) { throw new NegativeAmountException ("The amount entered cannot be negative"); } balance+=amount; }

public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException { if(balance < amount) { throw new InsufficientFundsException("You do not have sufficient funds in your account to perform this operation"); }

if(amount < 0) { throw new NegativeAmountException("The amount entered cannot be negative"); } balance-=amount; }

public double getBalance() { return balance; }

//print bank statement including customer name

//and current account balance

public void printStatement() { System.out.println(name+""+balance); } }

2. SavingsAccount class

import java.text.DecimalFormat;

public class SavingsAccnt extends BankAccnt { double currentRate; public SavingsAccnt(String name,double balance,double rate) throws NegativeAmountException { super(name,balance); if(balance < 0) { throw new NegativeAmountException("Cannot be negative"); } this.currentRate = rate; } public void postInterest() throws NegativeAmountException //post monthly interest by multiplying current balance { //by current interest rate divided by 12 and then adding double interestEarned = balance * currentRate / 12; //result to balance by making deposit deposit(interestEarned); }

public void printStatement() { DecimalFormat formatter = new DecimalFormat ("#0.00");// Create an object for dollars and cents //print bank statement including customer name // and current account balance( use print Statement from the BankAccount superclass) // following this also print current interest rate System.out.println("Amount for "+name+" "+"$ "+formatter.format(balance)+" balance"+" "+(currentRate*100)+"% Current Interest Rate"); } }

3. FinalExam class

import java.util.Scanner; import java.text.DecimalFormat;

public class FinalExam { public static void main(String args[]) throws InsufficientFundsException, NegativeAmountException { SavingsAccnt account = new SavingsAccnt("Penny Saved",1000.00,.05); Scanner info = new Scanner(System.in); DecimalFormat formatter = new DecimalFormat ("#0.00");// Create an object for dollars and cents System.out.print("Enter amount to Deposit : "); double credit = info.nextDouble(); account.deposit(credit); account.printStatement(); System.out.println(); System.out.print("Enter amount to Withdraw : "); double debit = info.nextDouble(); account.withdraw(debit); account.printStatement(); account.postInterest(); System.out.println(); System.out.println("Posting Monthly Interest"); account.printStatement(); } }

All three classes compile great and most of the output is what I am looking for.

Here's my question: Instead of throwing the exceptions, I need to catch them using a try/catch and possibly finally block. Can anyone give me an idea on how to do this?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!