How do I alter this code to throw an exception when a Negative Deposit is entere
ID: 3687242 • Letter: H
Question
How do I alter this code to throw an exception when a Negative Deposit is entered. Here is my driver code:
/**
* This program demonstrates the BankAccount class
* throws exceptions.
*/
public class AccountTest
{
public static void main(String [] args)
{
// Force a NegativeStartingBalance exception.
try
{
BankAccount account = new BankAccount(-1,0.04);
}
catch(NegativeStartingBalance e)
{
System.out.println(e.getMessage());
}
catch (NegativeInterestRate e)
{
System.out.println(e.getMessage());
}
// Force a NegativeInterestRate exception.
try
{
BankAccount account = new BankAccount(100,-0.04);
}
catch(NegativeStartingBalance e)
{
System.out.println(e.getMessage());
}
catch(NegativeInterestRate e)
{
System.out.println(e.getMessage());
}
}
}
and here is my exception code:
/**
* NegativeDeposit exceptions are thrown by the
* BankAccount class when a deposit is
* passed to the constructor.
*/
public class NegativeDeposit extends Exception
{
/**
* No-arg constructor
*/
public NegativeDeposit()
{
super("Error negative deposit");
}
/**
* The following constructor accepts the amount that
* was given as the interest rate.
*/
public NegativeDeposit(double amount)
{
super("Error: Negative deposit: " + amount);
}
}
The problem is I am not sure how to insert it and make it function correctly.
Here is the BankAccount class code:
/**
* 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 to 0.0.
*/
public BankAccount (double startBalance,
double intRate) throws NegativeStartingBalance,
NegativeInterestRate
{
if (startBalance < 0)
throw new NegativeStartingBalance(startBalance);
if (intRate < 0)
throw new NegativeInterestRate (intRate);
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}
/**
* The deposit method adds a 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 balance.
* returns the value in the balance field.
*/
public double getBalance()
{
return balance;
}
/**
* The getInterest method returns the interest
* value in the interest field.
*/
public double getInterest()
{
return interest;
}
}
Thanks for the help!
Explanation / Answer
Hi, Please find the below classes implemented as per your requirement. It throws Exception now. Added two nee classes for handling the exception
1. NegativeStartingBalance.java
2. NegativeInterestRate.java
AccountTest.java
public class AccountTest
{
public static void main(String [] args)
{
// Force a NegativeStartingBalance exception.
try
{
BankAccount account = new BankAccount(-1,0.04);
}
catch(NegativeStartingBalance e)
{
System.out.println(e) ;
}
catch (NegativeInterestRate e)
{
System.out.println(e) ;
}
// Force a NegativeInterestRate exception.
try
{
BankAccount account = new BankAccount(100,-0.04);
}
catch(NegativeStartingBalance e)
{
System.out.println(e) ;
}
catch(NegativeInterestRate e)
{
System.out.println(e) ;
}
// Force a Negative Deposit exception.
try
{
BankAccount account = new BankAccount(100,0.04);
account.deposit(-100);
}
catch(NegativeInterestRate e)
{
System.out.println(e) ;
}
catch(NegativeStartingBalance e)
{
System.out.println(e) ;
}
catch(NegativeDeposit e)
{
System.out.println(e) ;
}
}
}
NegativeDeposit.java
public class NegativeDeposit extends Exception
{
String str;
public NegativeDeposit(String s)
{
this.str = s;
}
public String toString(){
return (str) ;
}
}
BankAccount.java
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 to 0.0.
* @throws NegativeStartingBalance
* @throws NegativeInterestRate
*/
public BankAccount (double startBalance,
double intRate) throws NegativeStartingBalance, NegativeInterestRate
{
if (startBalance < 0)
throw new NegativeStartingBalance("Error: Negative Balance entered");
if (intRate < 0)
throw new NegativeInterestRate ("Error: Negative Interest entered");
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}
/**
* The deposit method adds a parameter
*amount to the balance field.
*/
public void deposit(double amount)throws NegativeDeposit
{
if (amount < 0)
throw new NegativeDeposit("Error: Negative Deposit entered");
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 balance.
* returns the value in the balance field.
*/
public double getBalance()
{
return balance;
}
/**
* The getInterest method returns the interest
* value in the interest field.
*/
public double getInterest()
{
return interest;
}
}
NegativeStartingBalance.java
public class NegativeStartingBalance extends Exception{
String str;
public NegativeStartingBalance(String s)
{
this.str = s;
}
public String toString(){
return (str) ;
}
}
NegativeInterestRate.java
public class NegativeInterestRate extends Exception{
String str;
public NegativeInterestRate(String s)
{
this.str = s;
}
public String toString(){
return (str) ;
}
}
Output:
Error: Negative Balance entered
Error: Negative Interest entered
Error: Negative Deposit entered
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.