Explanation of Solution
Creating “Main.java”:
- Create a class named “Main”.
- Define the “main ()” method.
- Declare required variables.
- Do till the user enters “n” using “while” condition.
- Get the string from the user.
- Check if the length of the string is greater than or equal to 20.
- Create an object “e” for the class. Here the exception is thrown using default constructor.
- Get and print the message using the method “e.getMessage()”.
- Else,
- Print the number of characters.
- Ask whether the user wants to continue or not and store the response in a variable “response”.
- Check if the response is equal to “n”.
- Break the loop.
- Define the “main ()” method.
Creating “MessageTooLongException.java”:
- Create a class named “MessageTooLongException” that extends “Exception”.
- Define a default constructor that calls the parent class’s method using “super ()” by passing a message.
- Define a parameterized constructor that calls the parent class’s method using “super ()” by passing a message that is given as the argument.
Program:
MessageTooLongException.java:
//Define a class
public class MessageTooLongException extends Exception
{
//Default constructor
public MessageTooLongException()
{
//Call the parent class by passing the message
super("Message Too Long!");
}
//Parameterized constructor
public MessageTooLongException(String message)
{
//Call the parent class by passing the message
super(message);
}
}
Main.java:
//import the package
import java.util.Scanner;
//Main class
class Main
{
//Define main method
public static void main(String[] args)
{
//Create an object for the scanner class
Scanner sc = new Scanner(System.in);
//Declare required variables
String str, response = "y";
//Do till the user enters 'n'
while(response...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Java Exception method withdraw throws an exception if amount is greater than balance. For example: Test Result Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(5500.0); System.out.println("Balance: "+account.getBalance()); Insufficient: Insufficient funds. Balance: 5000.0 Account account = new Account("Acct-001","Juan dela Cruz", 5000.0); account.withdraw(500.0); System.out.println("Balance: "+account.getBalance()); Balance: 4500.0arrow_forwardSubject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception (SEE ATTACHED PHOTO FOR THE PROBLEM)arrow_forwardWrite a Month class that holds information about the month. Write exception classes for the following error conditions:• A number less than 1 or greater than 12 is given for the month number.• An invalid string is given for the name of the month.Modify the Month class so that it throws the appropriate exception when either of these errors occurs. Demonstrate the classes in a program.arrow_forward
- JAVA PROGRAM Probloem: Define a new exception, called ExceptionLineTooLong, that prints out the error message "The strings is too long". Write a program that reads phrase and throws an exception of type ExceptionLineTooLong in the case where a string is longer than 80 characters. First Example:Input:The quick brown fox jumped over the lazy dogs.Output:The quick brown fox jumped over the lazy dogs. Second Example:Input: The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown…arrow_forwardSubject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception Define a new exception, called ExceptionLineTooLong, that prints out the error message "The strings is too long". Write a program that reads phrase and throws an exception of type ExceptionLineTooLong in the case where a string is longer than 80 characters. EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. Output:The quick brown fox jumped over the laze dogs. ANOTHER EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick…arrow_forwardA job ad for Gaggle Inc. says:It is necessary to major in either math, computer science, orphilosophy, and be an A student. Exceptions will be made for logicminors, for whom there are no grade requirements.You have also heard through the grapevine that it is sufficient to win a Fields Medal or Turing Award (regardless of what the ad may say).Express the above as one PC wff. You should of course state what your atoms represent. If you want partial credit, you should also provide wffs for individual parts, clearly writing out the corresponding phrases (I strongly recommend you do this to avoid getting a 0). Please do not do any form of reasoning to simplify the wff – that’s not the point here, and may also introduce bugs.arrow_forward
- Write codesarrow_forwardWrite a C++ program that converts 12-hour time (including AM/PM) to 24 hour time. You must define an exception class called InvalidTime wherein if the user enters an out of range time or gibberish characters, your program will throw and catch InvalidTime. Make your class support the following code: TwelveHourTime sevenAM( "7:00 AM" );cout << sevenAM.twentyFourTime( ) << endl; // prints "07:00"TwelveHourTime eightPM( "8:30 PM" );cout << eightPM.twentyFourTime( ) << endl; // prints "20:30"try{ TwelveHourTime bad( "asef32422" ); // the previous line should throw an InvalidTime exception} catch( InvalidTime ) { cout << "caught error as expected" << endl;}arrow_forwardWrite a program that prompts the user to enter a person’s date of birthin numeric form such as 8-27-1980. The program then outputs thedate of birth in the form: August 27, 1980. Your program must containat least two exception classes: invalidDay and invalidMonth.If the user enters an invalid value for day, then the program shouldthrow and catch an invalidDay object. Follow similar conventions forthe invalid values of month and year. (Note that your program musthandle a leap year.)arrow_forward
- Java displays an exception message when the program code could have prevented an error. Using scanner. create an exception handling method that declares three integers, prompts the user for values for two of them, and calculate the value of third integer by diving the first two values. (hint: Elephant's and Aliens)arrow_forwardpython program please helparrow_forwardExceptions in Java Write a program that accepts two integers X and Y. After, divide X by Y. If an exception occurs, print the following: If divided by zero, print "Division by zero is illegal." Print the result of the division if there is no error. Inputs 1. X 2. Y Sample Output Enter X: 1 Enter Y: 0 Division by zero is illegal.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr