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]
2 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]2 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
Is Using programming libraries is one way of incorporating existing code into new programs true
postnew [5]

Yes, Using programming libraries is one way of incorporating existing code into new programs  is a true statement.

<h3>What function do libraries provide in programming?</h3>

Programming libraries are helpful resources that can speed up the work of a web developer. They offer prewritten, reuseable portions of code so that programmers can easily and quickly create apps. Consider building a program that enables users to enroll in and pay for courses.

Therefore, Using a code library often saves developers from having to create everything from scratch. It can take less time to develop projects and have more reliable software if the catalog of programming resources is kept up well.

Learn more about libraries from

brainly.com/question/17960151
#SPJ1

6 0
1 year ago
All websites with medical information should be considered credible.
larisa [96]
No, this is not true. WebMD is a great example asking you of simple symptoms that you may be facing and your results could present an apparent fatal disease. This constantly scares the population of netizens whenever they would want a quick diagnosis online through these medical information sites. The best solution to your symptoms is to visit your family doctor or a licensed physician who will give you proper diagnosis checking your vital signs and other related information. Do not always trust the internet and the information it gives you as a user you must take the information with discretion before reacting inappropriately. 
3 0
2 years ago
Read 2 more answers
Write an algorithm to sum to values
Elis [28]

Answer:

There is no need to make an algorithm for this simple problem. Just add the two numbers by storing in two different variables as follows:

Let a,b be two numbers.

c=a+b;

print(c);

But, if you want to find the sum of more numbers, you can use any loop like for, while or do-while as follows:

Let a be the variable where the input numbers are stored.

while(f==1)

{

printf(“Enter number”);

scanf(“Take number into the variable a”);

sum=sum+a;

printf(“Do you want to enter more numbers? 1 for yes, 0 for no”);

scanf(“Take the input into the variable f”);

}

print(Sum)

Explanation:

hi there answer is given mar me as brainliest

5 0
3 years ago
True or false webcasts are delivered only as prerecorded audio and video content
alexandr1967 [171]
I believe the answer is false
3 0
3 years ago
What is the function of a CPU? simple words:D
koban [17]

Answer:

The brains of a computer

4 0
2 years ago
Read 2 more answers
Other questions:
  • Write a function `has_more_zs` to determine which of two strings contains # more instances of the letter "z". It should take as
    9·1 answer
  • A computer application such as Microsoft Access that is used to store data and convert it into information is a ________________
    6·1 answer
  • You want to discard your old computer and want to securely erase the data from your hard drive. What can you use to do this and
    6·1 answer
  • Write a MARIE program to allow the user to input 8 integers (positive, negative, or zero) and then find the smallest and the lar
    15·1 answer
  • Write a program that reads two fractions such as 1/2 and 1/4 and computes and stores the sum, the difference, the product and th
    15·1 answer
  • What is the best way to locate where my C program gets into an infinite loop
    5·1 answer
  • NoSQL is a programming language used to create mobile apps.<br> a) True<br> b) False
    13·2 answers
  • What time is spellrd the same forwards and backwards​
    11·1 answer
  • Help plz. due yesterday
    13·1 answer
  • What is meant by the term text?
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!