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. In addition, I am supposed to handle exceptions, but the only exceptions that I have to handle are the NegativeAmountException and the InsufficientFundsException. If either of these exception conditions occurs, the assignment directs me to print an appropriate error message, terminate the application, and advises me that I " can simply re-throw any IOExceptions from the main." 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: I need to import and use the java.io.* built in I/O class in my program and I need to alter exception handling to "re-throw any IOExceptions from the main," which I don't think I am doing right now because I haven't imported java.io.* (but I could be wrong given my limited understanding of java). 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!