Use the exception class MessageTooLongException in a program that asks the user
ID: 3940321 • Letter: U
Question
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.Explanation / Answer
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
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.