Answer:
See explaination
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)
{}
}
}