Question: Java: Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException , PunctuationException , and CommaException . EndOfSentenceException is the parent class to PunctuationException

Java:

Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException, PunctuationException, and CommaException. EndOfSentenceException is the parent class to PunctuationException, which is the parent class to CommaException.

Test your classes in a Driver class with a main method that asks the user to input a sentence. If the sentence ends in anything except a period (.), exclamation point (!), or question mark (?), the program should throw a PunctuationException. If the sentence specifically ends in a comma, the program should throw a CommaException.

Use a catch block to catch all EndOfSentenceExceptions, causing the program to print a message and terminate. If a general PunctuationException is caught, the program should print "The sentence does not end correctly." If a CommaException is caught, the program should print "You can't end a sentence in a comma." If there are no exceptions, the program should print "The sentence ends correctly." and terminate.

I understand that this will need to be 5 different classes (Exception, EndOfSentenceException extends Exception, PunctuationException extends EndOfSentenceException, CommaException extends PunctuationException, and ExceptionDriver), but I am confused by how to begin this. The previous answers to this question are either incorrect or incomplete. Any help would be appreciated!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!