Use the exception class MessageTooLongException in a program

Use the exception class MessageTooLongException in a program that asks the user to enter a line of text having no more than 20 characters. If the user enters an acceptable number of characters, the program should display the message. \"You entered x characters, which is an acceptable length\" (with the letter x replaced by the actual number of characters). Otherwise, a MessageTooLongException should be thrown and caught In either case, the program should repeatedly ask whether the user wants to enter another line or quit the program. but instead name the class MessageTooLongException. Also, if an exception is thrown using the default constructor. getMessage should return \"Message Too Long!\" Define an exception class called CoreBreachException The class should have a default constructor. If an exception is thrown using this zero-argument constructor. getMessage should return \"Core Breach! Evacuate Ship!\" The class should also define a constructor having a single parameter of type String If an exception is thrown using this constructor. getMessage should return the value that was used as an argument to the constructor.

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

###########   MessageTooLongException.java ###############

public class MessageTooLongException extends Exception{

  

   // default message constant declaration

       private final static String message = \"Message Too Long!\";

      

       // default constructor

       public MessageTooLongException(){

           super(message); // passing to parent class construction so this message can be

                           // retrieved from getMessage

       }

}

##################   MessageTooLongExceptionTest.java ################

import java.util.Scanner;

public class MessageTooLongExceptionTest {

  

   public static void main(String[] args) {

      

       // creating Scanner Object

       Scanner sc = new Scanner(System.in);

       String op = \"y\";

       while(op.charAt(0) == \'Y\' || op.charAt(0) == \'y\'){

          

           // getting user input

           System.out.println(\"Enter a line of text having no more than 20 characters: \");

           String line = sc.nextLine();

          

           // getting length of entered text

           int length = line.length();

          

           try{

              

               if(length > 20)

                   throw new MessageTooLongException();

               else

                   System.out.println(\"You entered \"+length+\" characters, which\"+

                                       \"is an acceptavle length\");

              

           }catch(MessageTooLongException e){

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

           }

          

           System.out.println(\"Do you want to another line (Y/N) ? \");

           op = sc.next();

          

       }

   }

}

/*

Sample run:

Enter a line of text having no more than 20 characters:

THis is a line that is having more than 20 characters

Message Too Long!

Do you want to another line (Y/N) ?

n

*/

############ CoreBreachException.java #############

public class CoreBreachException extends Exception {

   // default message constant declaration

   private final static String message = \"Core Breach! Evacuate Ship!\";

  

   // default constructor

   public CoreBreachException(){

       super(message); // passing to parent class construction so this message can be

                       // retrieved from getMessage

   }

}

 Use the exception class MessageTooLongException in a program that asks the user to enter a line of text having no more than 20 characters. If the user enters a
 Use the exception class MessageTooLongException in a program that asks the user to enter a line of text having no more than 20 characters. If the user enters a
 Use the exception class MessageTooLongException in a program that asks the user to enter a line of text having no more than 20 characters. If the user enters a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site