Question

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

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.

Please use Java, thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here are the 4 classes you will be needing for this problem.

Let me know for any help !

=================================================================

public class EndOfSentenceException extends Exception {



    public EndOfSentenceException( String message) {

        super(message);



    }

}

=================================================================

 public class PunctuationException extends EndOfSentenceException {



    public PunctuationException() {

        super("");

    }



    public PunctuationException(String message) {

        super(message);

    }

}

=================================================================

public class CommaException extends PunctuationException {



    public CommaException(String message) {

        super(message);

    }

}

=================================================================

public class Main {



    public static void main(String[] args) {



        String testSentences[] ={"Hi how are you$","I am sick@","I am perfect!","Where were you,"};



        for(String sentence:testSentences){



            try {

                System.out.println("Test Sentence: "+sentence);

                char lastLetter = sentence.charAt(sentence.length() - 1);

                if (lastLetter == ',') {

                    throw new CommaException("You can\'t end a sentence in a comma");

                } else if (!(lastLetter == '.' || lastLetter == '!' || lastLetter == '?')) {

                    throw new PunctuationException("You can\'t end a sentence without a \'.\' or \'!\' or \'?\'");

                }

                System.out.println("The sentence ends correctly.");

            }

                catch (EndOfSentenceException ese){

                System.out.println(ese.getMessage());

            }

        }

    }

}

=================================================================

Add a comment
Know the answer?
Add Answer to:
Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException, PunctuationException, and CommaException. EndOfSentenceException is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming....

    Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one of the biggest goals is "code reuse". Inheritance accomplishes this. In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct. Constructor implementations must use an initialization list. Please review the book and the online content on these issues so you know how it works...

  • (Catching Exceptions with Superclasses) Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses...

    (Catching Exceptions with Superclasses) Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC.

  • There is a white space in between but nothing is missing it's just the way screenshot...

    There is a white space in between but nothing is missing it's just the way screenshot is taken Can someone help me to solve this question Get Homework Help C PART 1: (7 Marks) Cre PDAssignment 2 Q BOStart Assignment New tab X file:///C:/Users/ksimr/AppData/Local/Packages/Microsoft.MicrosoftEdge_8wekyb3d8bbwe/TempState/Downloads/Assignment%202%20Question%20 PART I: (7 Marks) Create an exception hierarchy of ExceptionA, ExceptionB and ExceptionC such that ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. 6 Write a test program to show that the catch block for...

  • Question 20 Statements that might generate an exception are placed in a catch block. True False...

    Question 20 Statements that might generate an exception are placed in a catch block. True False 1 points Question 21 The class Exception and its subclasses are designed to catch exceptions that should be caught and processed during program execution. True False 1 points Question 22 Which of the following statements is NOT true about creating your own exceptions? Typically, constructors are the only methods that you include when you define your own exception class. The exception class that you...

  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly! THIS QUESTION WANTS THE A...

    THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly! THIS QUESTION WANTS THE A USER INPUT! SO FOR EXAMPLE THE CODE MUST MAKE IT SO THE USER HAS TO INPUT A TEMPERATURE. AND WHEN THE USER INPUTS A TEMP IT MUST OUTPUT IF THE TEMP ENTERED IS TO HIGH OR LOW. Again please read the question carefully and READ the notes I left also! Write a Thermostat class such that the user of the Thermostat...

  • JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use...

    JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...

  • UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships...

    UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships Labwork Please read all of the directions carefully. You will create a UML class diagram reflecting the class hierarchy for a fictional program that manages university personnel as constructed according to the graph displayed below. You will need to think about the attributes and behaviors that are unique to each class, and also those attributes and behaviors that are common amongst the subclasses and...

  • create a program that uses multiple inheritance by creating two base classes in C++ - an...

    create a program that uses multiple inheritance by creating two base classes in C++ - an Alien class and a Soldier class. -create three child classes from the Alien class and three child classes from the Soldier class. -use multiple inheritance to create five unique classes that each have a child Alien class and a child Soldier class as parents. -have the program print out each of the five class's characteristics when objects are created from the grandchildren classes.

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT