I NEED A CODE TO MODIFY THIS PROGRAM SUCH THAT IT CATCHES AND HANDLES THE EXCEPT
ID: 3535997 • Letter: I
Question
I NEED A CODE TO MODIFY THIS PROGRAM SUCH THAT IT CATCHES AND HANDLES THE EXCEPTION OF IT IS THROWN. HANDLE THE EXCEPTION BY PRINTING AN APPROPRIATE MESSAGE, AND THEN CONTINUE PROCESSING. PLEASE COMPLETE ALL CLASSES AND DRIVER, FOR THE PROGRAM TO RUN PROPERLY. PLEASE DO NOT COPY THE EXACT SAME CODE WHEN MODIFYING. THANK YOU. HERE IS THE CODE TO THE PROGRAM THAT I NEED MODIFIED BELOW:
InvalidDocumentCodeException class
//*******************************************************************
//InvalidDocumentCodeException.java Java Foundations
//
//Samuel Edebe
//
//Designed to be thrown when an improper designation
//for a document is encountered during processing.
//
//********************************************************************
public class InvalidDocumentCodeException extends Exception{
public InvalidDocumentCodeException(){
}
public String getMessage(){
return "Document code must be U (Unclassified), C (Confidential), or P (Proptietary)." +
" Document will default to Unclassified.";
}
}
Document class
//*******************************************************************
//Document.java Java Foundations
//
//Samuel Edebe
//
//Documents are given a two-character designation.
//
//********************************************************************
public class Document {
private String dType = "U";
public Document(String docType){
try{
if( docType.compareToIgnoreCase("U") != 0
&& docType.compareToIgnoreCase("C") != 0
&& docType.compareToIgnoreCase("P") != 0 )
throw newInvalidDocumentCodeException();
dType = docType;
}
catch(InvalidDocumentCodeException e){
System.out.println("Exception caught and handled! Continuing...");
System.out.println(e.getMessage());
}
}
public voidsetDocType(String docType){
dType = docType;
}
public String toString(){
return "Document type: "+ dType.toUpperCase();
}
}
Starter class
//*******************************************************************
//Starter.java Java Foundations
//
//Samuel Edebe
//
//Tests the exception, allowing it to terminate the program.
//
//********************************************************************
import java.util.Scanner;
public class Starter {
public static voidmain(String[] args) {
String done = "DONE";
System.out.println("DocType? U = Unclassified, C = Confidential, P = Proprietary.");
System.out.println("Type DONE, in all caps to end.");
Scanner scan = newScanner(System.in);
String docType = scan.next();
while(docType.compareTo(done) != 0){
Document doc = newDocument(docType);
System.out.println(doc);
docType = scan.next();
}
System.out.println("Program Fin~");
}
}
Explanation / Answer
InvalidDocumentCodeException class
public class InvalidDocumentCodeException extends Exception{
public InvalidDocumentCodeException(){
}
public String getMessage(){
return "Document code must be U (Unclassified), C (Confidential), or P (Proptietary)." +
" Document will default to Unclassified.";
}
}
Document class
public class Document {
private String dType = "U";
public Document(String docType){
try{
if( docType.compareToIgnoreCase("U") != 0
&& docType.compareToIgnoreCase("C") != 0
&& docType.compareToIgnoreCase("P") != 0 )
throw newInvalidDocumentCodeException();
dType = docType;
}
// I am using (Exception e), this will catch all types of exception and then print it.
catch(Exception e){
System.out.println(e);
System.out.println("Exception caught and handled! Continuing...");
System.out.println(e.getMessage());
}
}
public voidsetDocType(String docType){
dType = docType;
}
public String toString(){
return "Document type: "+ dType.toUpperCase();
}
}
Starter class
import java.util.Scanner;
public class Starter {
public static voidmain(String[] args) {
String done = "DONE";
System.out.println("DocType? U = Unclassified, C = Confidential, P = Proprietary.");
System.out.println("Type DONE, in all caps to end.");
Scanner scan = newScanner(System.in);
String docType = scan.next();
while(docType.compareTo(done) != 0){
Document doc = newDocument(docType);
System.out.println(doc);
docType = scan.next();
}
System.out.println("Program Fin~");
}
}
// put the code that you want to execute even it there is an exception in the finally {} .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.