Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I create an exception class to handle the following scenario: A negative number is passed to a deposit method, using a try catch

How do I create an exception class to handle the following scenario: A negative number is passed to a deposit method, using a try catch statement. The code i currently have is:

import java.util.Scanner; // Needed for the Scanner class

/** * This program demonstrates the BankAccount class. */

public class AccountTest { public static void main(String [] args) { BankAccount account;// To reference a BankAccount object double balance, // The account's starting balance interestRate, // The monthly interest rate pay, // The user's pay cashNeeded; // The amount of cash to withdraw

// Create a Scanner object for keyboard input. Scanner in = new Scanner(System.in); System.out.print("What is your account's starting balance?");

try { int balance = in.nextInt(); } catch(NegativeStartingBalance e) { System.out.println(e.getMessage()); } catch(NegativeInterestRate e) { System.out.println(e.getMessage()); } System.out.print("What was your monthly interest rate? ");

try { interestRate = keyboard.nextDouble(); } catch(NegativeStartingBalance e) { System.out.println(e.getMessage()); } catch(NegativeInterestRate e) { System.out.println(e.getMessage()); } // Create a BankAccount object. account = new BankAccount(balance, interestRate);

// Get the amount of pay for the month. System.out.print("How much were you paid this month? "); pay = keyboard.nextDouble();

// Deposit the user's pay into the account. System.out.println("We will deposit your pay " + "into your account."); account.deposit(pay); System.out.println("Your current balance is $" + account.getBalance());

// Withdraw some cash from the account. System.out.print("How much would you like " + "to withdraw? "); cashNeeded = keyboard.nextDouble(); account.withdraw(cashNeeded);

// Add the monthly interest to the account. account.addInterest();

// Display the interest earned and the balance. System.out.println("This month you have earned $" + account.getInterest() + " in interest."); System.out.println("Now your balance is $" + account.getBalance());

} }

_______________________________________________________

/** * BankAccount class * This class simulates a bank account. */

public class BankAccount { private double balance; // Account balance private double interestRate; // Interest rate private double interest; // Interest earned

/** * The constructor initializes the balance * and interestRate fields with the values * passed to startBalance and intRate. The * interest field is assigned 0.0. */ public BankAccount(double startBalance, double intRate) throws NegativeStartingBalance, NegativeInterestRate { if (balance < 0) throw new NegativeStartingBalance(balance); if (interestRate < 0) throw new NegativeInterestRate(interestRate); balance = startBalance; interestRate = intRate; interest = 0.0; }

/** * The deposit method adds the parameter * amount to the balance field. */

public void deposit(double amount) { balance += amount; }

/** * The withdraw method subtracts the * parameter amount from the balance * field. */

public void withdraw(double amount) { balance -= amount; }

/** * The addInterest method adds the * interest for the month to the balance field. */

public void addInterest() { interest = balance * interestRate; balance += interest; }

/** * The getBalance method returns the * value in the balance field. */

public double getBalance() { return balance; }

/** * The getInterest method returns the * value in the interest field. */

public double getInterest() { return interest; }

}

__________________________________________

/** * NegativeInterestRate exceptions are thrown by the * BankAccount class when a negative interest rate is * passed to the constructor. */

public class NegativeInterestRate extends Exception { /** * No-arg constructor */

public NegativeInterestRate() { super("Error: Negative interest rate"); }

/** * The following constructor accepts the amount that * was given as the interest rate. */

public NegativeInterestRate(double amount) { super("Error: Negative interest rate: " + amount); } }

_______________________________________________

/** * NegativeStartingBalance exceptions are thrown by * the BankAccount class when a negative starting * balance is passed to the constructor. */

public class NegativeStartingBalance extends Exception { /** * No-arg constructor */

public NegativeStartingBalance() { super("Error: Negative starting balance"); }

/** * The following constructor accepts the amount * that was given as the starting balance. */

public NegativeStartingBalance(double amount) { super("Error: Negative starting balance: " + amount); } }

When i compile AccountTest.java is:

AccountTest.java error: exception NegativeStartingBalance is never thrown in body of corresponding try statement

catch(NegativeStartingBalance e)

^

AccountTest.java error: exception NegativeInterestRate is never thrown in body of corresponding try statement

catch(NegativeInterestRate e)

^

I get that error twice more

Also, it prints the error:

variable account might not have been initialized

account.despsit(pay);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions