Question: Using Java This assignment introduces you to an exception-related topic that isn't covered in the book but which you may find helpful down the road.

Using Java

This assignment introduces you to an exception-related topic that isn't covered in the book but which you may find helpful down the road. Recall that all exceptions are subclasses of the Exception class (which is a subclass of the Throwable class). Check out the hierarchy chart on page 339 to see this. This means that you have the ability to write your own exceptions by extending the Exception class or, for an unchecked exception the RuntimeException class.

For this homework I am giving you a custom exception called StringTooLongException which extends RuntimeException:

public class StringTooLongException extends RuntimeException {

private static final long serialVersionUID = 1L; //This is required to avoid a warning message

public static final int MAX_LENGTH = 30; //This could set to other values than 30

//This constructor uses the superclass constructor, setting a default message.

public StringTooLongException() {

super("There are too many characters in the string. Please try again.");

}

//This constructor uses the superclass constructor, and set the message based on the parameter passed in.

public StringTooLongException(String messageText) {

super(messageText);

}

}

With a custom exception handler you have to manually check for whether the exception happens and then throw it. In this case, the condition you're looking for is that the string is longer than the MAX_LENGTH instance variable. Because this was declared as a static variable you can access its value without instantiating an object as:

StringTooLongException.MAX_LENGTH

You will be using this custom exception in the following ways:

Write a tester class called STLTester1 that prompts the user for a string. If the length of the string is less than or equal to MAX_LENGTH the program should thank the user and repeat the string they entered. If the string is longer than than MAX_LENGTH you should throw a StringTooLongException exception.

Write a tester class called STLTester2 that prompts the user for a string. If the length of the string is less than or equal to MAX_LENGTH the program should thank the user and repeat the string they entered. If the string is longer than than MAX_LENGTH you should catch the exception and print out the error message.

Write a tester class called STLTester3 that prompts the user for a string. If the length of the string is less than or equal to MAX_LENGTH the program should thank the user and repeat the string they entered. If the string is longer than than MAX_LENGTH you should catch the exception and print out the error message. This program should loop until the user successfully enters a string whose length is valid.

In one of these you should use the default error message and in another you should use a custom message. For the third you can use either one.

The test file should be STLTester1.java, STLTester2.java and STLTester3.java.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!