Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 1: Define an exception class called MessageTooLongException. The class shou

ID: 3764048 • Letter: T

Question

Task 1: Define an exception class called MessageTooLongException. The class should have a default constructor. If an exception is thrown using this zero-argument constructor, getMessage should return “Message too Long!”. This 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.

Task 2: Write an application called UserInput 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 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.

Explanation / Answer

Task 2 :

The below is the program for the one you required for the Task 2.

The comments are also provided above the lines of code where ever necessary for easy understanding.

import java.util.Scanner;
public class InputString
{
public static void main(String[]args) throws Exception
{

String str;
int num;

//Here the user will enter the sentence

System.out.println("Enter a string of characters" + "When finsihed type DONE");
Scanner scan = new Scanner(System.in);

str = scan.nextLine();

// Here the entered string will check for the string “DONE”. If it exists then It will go to next line of the code.
while (!str.equals("DONE"))
{
try
{
num = Integer.parseInt(str);
if(num >20)

{

// Here we will raise our custom exception if the length of the string is greater than 20.

throw MessageTooLongException (“Exception Throws: Entered line is greaterthan 20 characters”);

}
}
catch(StringTooLongException problem)
{
System.out.println("Input String is too long.");
}

}
System.out.println("End of main method.");
}

}
// By the following method we can define our custom exception
public class MessageTooLongException extends Exception
{

MessageTooLongException (String message)
{
super (message);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote