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. java

Respuesta :

Answer:

Check the explanation

Explanation:

EndOfSentenceException.java

//Create a class EndOfSentenceException that

//extends the Exception class.

class EndOfSentenceException extends Exception

{

    //Construtor.

    EndOfSentenceException(String str)

    {

         System.out.println(str);

    }

}

CommaException.java

//Create a class CommaException that extends the class

//EndOfSentenceException.

class CommaException extends EndOfSentenceException

{

    //Define the constructor of CommaException.

    public CommaException(String str)

    {

         super(str);

    }

}

PunctuationException.java

//Create a class PunctuationException that extends the class

//EndOfSentenceException.

class PunctuationException extends EndOfSentenceException

{

    //Constructor.

    public PunctuationException(String str)

    {

         super(str);

    }

}

Driver.java

//Include the header file.

import java.util.Scanner;

//Define the class Driver to check the sentence.

public class Driver {

    //Define the function to check the sentence exceptions.

    public String checkSentence(String str)

              throws EndOfSentenceException

    {

         //Check the sentence ends with full stop,

//exclamation mark

         //and question mark.

         if(!(str.endsWith(".")) && !(str.endsWith("!"))

                    && !(str.endsWith("?")))

         {

              //Check the sentence is ending with comma.

              if(str.endsWith(","))

              {

                    //Throw the CommaException.

                    throw new CommaException("You can't "

                              + "end a sentence in a comma.");

              }

              //Otherwise.

              else

              {

//Throw PunctuationException.

              throw new PunctuationException("The sentence "

                              + "does not end correctly.");

              }

         }

         //If the above conditions fails then

         //return this message to the main function.

         return "The sentence ends correctly.";

         }

         //Define the main function.

         public static void main(String[] args)

         {

              //Create an object of Scanner

              Scanner object = new Scanner(System.in);

              //Prompt the user to enter the sentence.

              System.out.println("Enter the sentence:");

              String sentence=object.nextLine();

              //Begin the try block.

              try {

                    //Call the Driver's check function.

                    System.out.println(new

Driver().checkSentence(sentence));

              }

              //The catch block to catch the exception.

              catch (EndOfSentenceException e)

              {}

         }

}