Question

How would I alter this code to have the output to show the exceptions for not...

How would I alter this code to have the output to show the exceptions for not just the negative starting balance and negative interest rate but a negative deposit as well? Here is the class code for BankAccount:

/**
* 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, double depAmt) throws NegativeStartingBalance,
                       NegativeInterestRate,
                                               NegativeDeposit
{
   if (startBalance < 0)
       throw new NegativeStartingBalance(startBalance);
   if (intRate < 0)
       throw new NegativeInterestRate (intRate);
   if (depAmt < 0)
       throw new NegativeDeposit (depAmt);
     
   deposit amount = depAmt;
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;
}
}

Here is the driver, AccountTest.java:

/**
* 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());
       }         
}
}   
Here is my NegativeInterestRate.java:

/**
* 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);
   }
}  

Here is my NegativeStartingBalance.java:

/**
* 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);
   }
}

So, I am asking for a new class to handle the exception for a negative deposit and for the output of TestAccount to show Error: Negative Starting Balance, Error: Negative interest rate, and Error: Negative Deposit.

Thank you

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
How would I alter this code to have the output to show the exceptions for not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank...

    please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank you so much! public abstract class BankAccount {    //declare the required class variables    private double balance;    private int num_deposits;    private int num_withdraws;    private double annualInterest;    private double serviceCharges;       //constructor that takes two arguments    // one is to initialize the balance and other    // to initialize the annual interest rate       public BankAccount(double balance,...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • need basic default constructor , and shoed an example too s0. write the class Battery and...

    need basic default constructor , and shoed an example too s0. write the class Battery and it has s1. a data for the battery capacity (that is similar to myBalance of the BankAccount class.) s2. a parameterized constructor: public Battery(double amount) s3. a drain mutator s4. a change mutator s5. a getRemainingCapacity accessor Need to run this tester - public class BatteryTester{ public static void main(String[] args) { Battery autoBattery = new Battery(2000); //call default constructor System.out.println("1. battery capacity is...

  • (JAVA NetBeans) Write programs in java Modify the textbook example textbook example: //BankAccount.java import java.tex...

    (JAVA NetBeans) Write programs in java Modify the textbook example textbook example: //BankAccount.java import java.text.DecimalFormat; public class BankAccount { public final DecimalFormat MONEY = new DecimalFormat( "$#,##0.00" ); private double balance; /** Default constructor * sets balance to 0.0 */ public BankAccount( ) { balance = 0.0; System.out.println( "In BankAccount default constructor" ); } /** Overloaded constructor * @param startBalance beginning balance */ public BankAccount( double startBalance ) { if ( balance >= 0.0 ) balance = startBalance; else balance...

  • I need to update the code listed below to meet the criteria listed on the bottom....

    I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT