Question

In: Computer Science

EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...

EXCEPTION HANDLING

Concept Summary:

1. Exception handling

2. Class design

Description

Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it.

Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception.

Design the program such that it catches and handles the exception if it is thrown. Handle the exception by printing an appropriate message, and then continue processing more names by getting more inputs from the user. Note: Your program should exit only when the user enters "DONE".

SAMPLE OUTPUT:

Enter strings. When finished enter DONE

Short string

You entered: Short string

Medium size string

You entered: Medium size string

Really really long string, someone stop me!

You entered too many characters

DONE

You entered: DONE

Good bye!

Solutions

Expert Solution

//Create a class for exception handle name with ManyCharactersException.java

//Create a driver class with name ExceptionDriver.java to check the functionality of ManyCharactersException

//run the driver class

-------------------------------------------------------------------------------------------------------------------------------------------------

ManyCharactersException.java

public class ManyCharactersException extends Exception {
int condition = 0;
  
public ManyCharactersException(int conditionViolated)
{
super("Invalid Password: ");
condition = conditionViolated;
}
public String printMessage()
{
// Call constructor of parent Exception
// according to the condition violated
switch (condition) {

// designed to be thrown when a string is discovered that has too many characters in it.
case 1:
return ("You entered: Short String");
  
// characters near about 20
case 2:
return ("You entered: Medium String");
  
// characters has more than 20 characters
case 3:
return ("You entered too many characters");

}
  
return ("You entered: DONE");
}
}

-------------------------------------------------------------------------------------------------------------------------------------------------

ExceptionDriver.java code


import java.util.Scanner;

public class ExceptionDriver {

public static void checkString(String name) throws ManyCharactersException {
//here are the conditions about length of String
//you can change the legth of string for comparision
if (name.equals("DONE")) {
throw new ManyCharactersException(4);
} else if (name.length() <= 12) {
throw new ManyCharactersException(1);//to short
} else if (name.length() > 12 && name.length() < 20) {
throw new ManyCharactersException(2);
} else if (name.length() > 20) {
throw new ManyCharactersException(3);
}
}

public static void main(String[] args) {
System.out.println("Enter strings. When finished enter DONE");
Scanner sc = new Scanner(System.in);
String input = "";
while (!input.equals("DONE")) {
try {
input = sc.nextLine();
checkString(input);

} catch (ManyCharactersException ee) {
System.out.println(ee.printMessage());
}
}
System.out.println("Good bye!");
}
}

--------------------------------------------------------------------OUTPUT--------------------------------------------------------------


Related Solutions

EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to be thrown when a   string is discovered that has too many characters in it. Consider a   program that   takes   in the last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,   throw   the   exception.  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry by a user. a. Write a program that prompts a user to enter a length in feet and inches. The length values must be positive integers. b. Calculate and output the equivalent measurement in centimeters 1 inch = 2.54 centimeters c. Write the code to handle the following exceptions: If the user enters a negative number, throw and catch an error that gives...
Apply JavaFX and exception handling to design a CircleApp class using proper JavaFX GUI components, such...
Apply JavaFX and exception handling to design a CircleApp class using proper JavaFX GUI components, such as labels, text fields and buttons(Submit, Clear, and Exit) to compute the area of circle and display the radius user entered in the text field and computing result in a proper location in the application windows. It will also verify invalid data entries for radius(No letters and must be a postive real number) using expection handling. Your code will also have a custom-designed exception...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
Write a program that creates a Singleton class to connect to two databases. The program must...
Write a program that creates a Singleton class to connect to two databases. The program must provide two instances of this class. One instance for connecting to MySQL database and the other for connecting to Oracle database.
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT