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

Answers

Answer 1
Answer:

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)

{}

}

}


Related Questions

Which of the following describes a 2×4 decoder? A)-Two AND gates, four Inverters, and one OR gate. B)-Two Inverters, four AND gates, and one OR gate. C)-Two OR gates, four AND gates, and one Inverter. D)-Two Inverters, four OR gates, and one AND gate. E)-Two Inverters, four AND gates, and no OR gates.
You need to design online to form in which users have to type their name and password to log into an application. The password can be acombination of numbers and letters. Which data type is most suitable for a password field?Thedata type is most suitable to define a password field.
In which situation does a linear search always perform better than a binary search?
The program that you create for this exercise will begin by reading the cost of a meal ordered at a restaurant from the user. Then your program will compute the tax and tip for the meal. Use your local tax rate when computing the amount of tax owing. Compute the tip as 18 percent of the meal amount (without the tax). The output from your program should include the tax amount, the tip amount, and the grand total for the meal including both the tax and the tip. Format the output so that all of the values are displayed using two decimal places.
What do you think is the single greatest physical threat to information systems? Fire? Hurricanes? Sabotage? Terrorism? Something else? Discuss this question and provide support for your answer.

Create an application (that uses the SortedABList) that allows a user to enter a list of countries that he or she has visited and then displays the list in alphabetical order, plus a count of how many countries are on the list. If the user mistakenly enters the same country more than once, the program should inform the user of their error and refrain from inserting the country into the list a second time.

Answers

Answer:

import java.util.*;

public class Country

{

  public static void main(String args[])

  {

      char ch,temp;

      int flag = 0;

      String country;

      ArrayList<String> countries = new ArrayList<String>();

      Scanner sc = new Scanner(System.in);

      do

      {

          System.out.println("enter the country you have visited:\t");

          country = sc.next();

          for(int i=0;i<countries.size();i++)

          {

              if(countries.get(i).equals(country))

              {

                  System.out.println("you have already entered the country");

                  flag = 1;

                  break;      

              }

          }

          if(flag == 0)

          {

              countries.add(country);

              flag = 0;

          }

          System.out.println("want to add another country(y/n):\t");

          ch = sc.next().charAt(0);

      }while(ch!='n');

      Collections.sort(countries);

      System.out.println("Countries you have visited:\t"+countries);

      System.out.println("Total number of contries visited:"+countries.size());

     

  }

}

Explanation:

Illustrator : how do you edit a swatch ?​

Answers

Answer:

To edit an existing pattern, double-click the pattern in the pattern swatch, or select an object containing the pattern and choose Object > Pattern > Edit Pattern

_ is the adherence to a personal code of principles.Ethics
Morality
Integrity
Honesty
Section B

Answers

Answer: Ethics

Explanation:

 Ethics is the basic principle for the personal code. The code of the ethics is basically designed for outline the values in the organization with honesty and integrity.

The ethics is basically depend upon the principle of core value of the organization. The code of the ethics basically guide the core value in the organization and breaking the rule of ethics can also cause termination from the organization.

Morality, integrity and honesty are all the sub part of the ethics vale in the organization. Therefore, ethics is the correct option.  

Write a programe to add two numbers using function with return type"void".

Answers

Answer:

#include<iostream>

using namespace std;

//create the function which add two number

void addTwoNumber(int num_1,int num_2)

{

   int result = num_1 + num_2;  //adding

   

   cout<<"The output is:"<<result<<endl;  //display on the screen

}

//main function

int main(){

   //calling the function

   addTwoNumber(3,6);

   return 0;

}

Explanation:

First, include the library iostream for using the input/output instructions.

then, create the function which adds two numbers. Its return type is void, it means the function return nothing and the function takes two integer parameters.

then, use the addition operation '+' in the programming to add the numbers and store the result in the variable and display the result.

create the main function for testing the function.

call the function with two arguments 3 and 6.

then, the program copies the argument value into the define function parameters and then the program start executing the function.

I need help please?!!!

Answers

Answer:

I can't give an answer if you don't tell me what you need help with

Explanation:

I can't give an answer if you don't tell me what you need help with

1. (1 point) Which flag is set when the result of an unsigned arithmetic operation is too large to fit into the destination? 2. (1 point) Which flag is set when the result of a signed arithmetic operation is either too large or too small to fit into the destination? 3. (1 point) Which flag is set when an arithmetic or logical operation generates a negative result?

Answers

Answer:

(1) Carry flag (2) Overflow flag (3) Sign or negative Flag

Explanation:

Solution

(1) The carry flag : It refers to the adding or subtracting. a two registers has a borrow or carry bit

(2) The over flow flag: This flag specify that a sign bit has been modified during subtracting or adding of operations

(3) The sign flag: This is also called a negative sign flag is a single bit status in a register that specify whether the last mathematical operations  generated a value to know if the most significant bit was set.