JAVA Programming Design and implement a program that creates

JAVA Programming

Design and implement a program that creates an exception class called InvalidDocumentCodeException, designed to be thrown when an improper designation for document is encountered during processing. Suppose in a particular business all documents are given two-character designation starting with either U, C, or P, standing for unclassified, confidential, or proprietary. If a document designation is encountered that does not fit that description, the exception is thrown. Create a driver program to test the exception, allowing it to terminate the program.

Solution

DocumentCodeTest.java


import java.util.Scanner;

public class DocumentCodeTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the designation: \");
       String designation = scan.next();
       try{
       if (designation != null
               && (designation.charAt(0) == \'U\'
                       || designation.charAt(0) == \'C\' || designation
                       .charAt(0) == \'P\') && designation.length() == 2) {
           System.out.println(\"Valid designation\");
       }
       else{
           throw new InvalidDocumentCodeException(\"Invalid designation. Designation must be two characters and start with U, C or P.\");
       }
       }
       catch(InvalidDocumentCodeException e){
           System.out.println(e);
       }

   }

}

InvalidDocumentCodeException.java

public class InvalidDocumentCodeException extends Exception{
   String errorMsg ;
   public InvalidDocumentCodeException(String s){
       this.errorMsg = s;
   }
public String toString(){
return (errorMsg ) ;
}  
}

Output:

Enter the designation:
UC
Valid designation

Enter the designation:
AE
Invalid designation. Designation must be two characters and start with U, C or P.

JAVA Programming Design and implement a program that creates an exception class called InvalidDocumentCodeException, designed to be thrown when an improper desi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site