1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Alex777 [14]
3 years ago
6

2. Use inheritance to create a hierarchy of Exception classes -- EndOfSentenceException, PunctuationException, and CommaExceptio

n. 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.
Computers and Technology
1 answer:
Lelechka [254]3 years ago
5 0

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)

{}

}

}

You might be interested in
A service technician removed the inspection/fill plug from the differential of a rear-wheel- drive vehicle and gear lube started
adell [148]

Answer:

The correct answer is - Technician B is correct.

Explanation:

Because when the service technicians remove inspection plug from a differential of the rear wheel drive vehicles and then the gear lubes is started to flows out and after that, it catches the fluids and allows the fluids to continue to the drain.

So, that's why the technician B is correct.

4 0
4 years ago
Your welcome 95 points gg
STatiana [176]

Answer:

tysm :))

Explanation:

have a great day

4 0
3 years ago
Read 2 more answers
Which protocol is used to encrypt data as it travels a network?
pickupchik [31]
The protocol that's used to encrypt data as it travels a network would be the IPSec.
3 0
3 years ago
The fractional_part function divides the numerator by the denominator, and returns just the fractional part (a number between 0
7nadin3 [17]

Answer:

part a

Explanation:

7 0
4 years ago
I am a receptionist for a large corporation what career cluster is this in?
RSB [31]
Business and administration
4 0
3 years ago
Read 2 more answers
Other questions:
  • How did AI help in creating the Cognitive dress for Marchesa?
    5·1 answer
  • What email address should you use to send questions to a UC faculty or staff member?
    14·2 answers
  • Five aplications of ict​
    10·1 answer
  • How do u know when a website doesnt like u?
    11·1 answer
  • If you type too much text on a PowerPoint slide, the additional text is added to a second slide.
    9·1 answer
  • Bit is an abbreviation of_____​
    9·1 answer
  • A _is a short descriptive label that you assign to webpages, photos,
    10·1 answer
  • Define a function OutputValues() that takes two integer parameters and outputs all integers between the first and the second par
    15·1 answer
  • What is the difference between a computer’s RAM and its hard disk?
    13·2 answers
  • #include
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!