Java help Module 4Exception Handling and Text IO Assignment

Java help

Module 4:Exception Handling and Text I/O

Assignment 4: Test Defined Exception

Purpose: User Defined Exception class in Java

Java has very good support of handling Error and Exception, It has a well-defined Exception hierarchy and language level support to throw and catch Exception and deal with them. Java Programmers often deals with built-in exceptions from java.lang package and several others which are already defined in JDK API e.g. NullPointerException. Please remember the advice our textbook gives regarding Exception. You should try to reuse the Exception classes provided in the JDK, e.g.,IndexOutOfBoundException, ArithmeticException, IOException, and java.lang.ArrayIndexOutOfBoundsException, instead of creating new ones for similar purpose. But there are cases when a user defined, custom exception provides better opportunity to deal with special cases. So remember, you can always create your own Exception classes by extending from the class Exception or one of its sub classes. Also note that RuntimeException and its sub classes are not checked by the compiler and need not be declared in the method\'s signature. Therefore, use them with care, as you will not be informed and may not be aware of the exceptions that may occur by using that method (and therefore do not have the proper exception handling codes) – a bad software engineering practice.

User Defined Exception in Java

Here is an example of how to create a user defined custom Exception in Java. By using this you can handle different error condition differently. It\'s one of the simplest example to understand need of customized exception in Java. Here we have an Account class, which is representing a bank account where you can deposit and withdraw money, but what will happen if you want to withdraw money which exceed your bank balance? You will not be allowed, and this is where user defined exception comes into picture. We have created a custom exception called NotSufficientFundException to handle this scenario. This will help you to show more meaningful message to user and programmer.

Account.java

Description: The Account class should be declared as a public class and should meet all the requirements listed below. Its purpose is to represent a Bank account which holds your initial balance of $1000 at creation, and provide two means of modifying your balance: one by withdrawing money and another by depositing money; and a simple method called balance which returns the actual balance.

Instance Variables:

private int balance =1000;//stores a $1000 at the time of creation of a bank account.

Constructor: Account() 1- Parameters: none;

Methods:

1-public int balance() {//returns the actual account balance return balance;}

2-public void withdraw(int amount) throws NotSufficientFundException{

//withdraws amount from balance if there are sufficient funds.

if (amount > balance) {

throw new NotSufficientFundException(String.format(\"Current balance %d is less than requested amount %d\", balance, amount));}

balance = balance – amount;}}

3-public void deposit(int amount) (){//deposits amount in balance if amount is not <0.

if (amount <= 0) {throw new IllegalArgumentException(String.format

(\"Invalid deposit amount %s\", amount));

}

balance=balance + amount;}}

NotSufficientFundException.java

Description: The NotSufficientFundException class should be declared as a public class and should meet all the requirements listed below. Its purpose is to alert the user that he/she is withdrawing an amount of money that exceeds the amount that is available.

Instance Variables:

private String message;//a String message to share with the user.

Constructor: NotSufficientFundException()

1- Parameters:

String message;

Methods:

1-public NotSufficientFundException (Throwable cause, String message) {

super(cause);

this.message = message;}

2- public String getMessage() {

return message;

}

TestDefinedException.java

Description: The TestDefinedException class should be declared as a public class and should meet all the requirements listed below. Its purpose is to implement a main method which creates an object -- an Account object -- and test withdrawing money from the account twice to test the ideas proposed above.

You should already be familiar with how to instantiate objects and print values to the screen using System.out.println(...). Therefore, the actual implementation code for this assignment will not be provided. You may organize the output of data according to their own specifications. However, the main method must perform the following tasks:

Create an instance of Account, called acct.

Print the current balance of acct.

Next we want to withdraw “400” from acct.

Print the current balance of acct.

Next we want to deposit “300” from acct.

Print the current balance of acct.

Next we want to withdraw “$1000” from acct.

Print the current balance of acct.

Solution

Hi, Please find my code.

Please let me know in case of any issue:

########### Account.java ################

public class Account {

  

private int balance;//stores a $1000 at the time of creation of a bank account.

   public Account(){

       balance =1000;

   }

  

public int balance() {//returns the actual account balance return balance;

       return balance;

   }

      

   public void withdraw(int amount) throws NotSufficientFundException{

       //withdraws amount from balance if there are sufficient funds.

       if (amount > balance) {

           throw new NotSufficientFundException(

                   String.format(\"Current balance %d is less than requested amount %d\", balance, amount));

       }

       balance = balance - amount;

   }

public void deposit(int amount){//deposits amount in balance if amount is not <0.

       if (amount <= 0) {

           throw new IllegalArgumentException(String.format(\"Invalid deposit amount %s\", amount));

       }

       balance=balance + amount;

      

   }

}

###########   NotSufficientFundException.java #################

public class NotSufficientFundException extends Exception {

  

private String message;//a String message to share with the user.

  

   public NotSufficientFundException(String message){

       this.message = message;

   }

  

   public NotSufficientFundException (Throwable cause, String message) {

       super(cause);

       this.message = message;

   }

  

   public String getMessage() {

       return message;

   }

}

############### TestDefinedException.java ####################

public class TestDefinedException {

  

   public static void main(String[] args) {

  

       // creating Account Object

       Account acct = new Account();

      

       // printing current balance

       System.out.println(\"Current Balance: \"+acct.balance());

      

       // withdrawing 400  

       try {

           acct.withdraw(400);

       } catch (NotSufficientFundException e) {

           System.out.println(e.getMessage());

       }

      

       // printing current balance

       System.out.println(\"Current Balance: \"+acct.balance());

      

       // deposite 300

       acct.deposit(300);

      

       // printing current balance

       System.out.println(\"Current Balance: \"+acct.balance());

      

       // withdrawing 1000  

       try {

           acct.withdraw(400);

       } catch (NotSufficientFundException e) {

           System.out.println(e.getMessage());

       }

   }

}

/*

Sample Output:

Current Balance: 1000

Current Balance: 600

Current Balance: 900

*/

Java help Module 4:Exception Handling and Text I/O Assignment 4: Test Defined Exception Purpose: User Defined Exception class in Java Java has very good support
Java help Module 4:Exception Handling and Text I/O Assignment 4: Test Defined Exception Purpose: User Defined Exception class in Java Java has very good support
Java help Module 4:Exception Handling and Text I/O Assignment 4: Test Defined Exception Purpose: User Defined Exception class in Java Java has very good support
Java help Module 4:Exception Handling and Text I/O Assignment 4: Test Defined Exception Purpose: User Defined Exception class in Java Java has very good support
Java help Module 4:Exception Handling and Text I/O Assignment 4: Test Defined Exception Purpose: User Defined Exception class in Java Java has very good support

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site