Question

In: Computer Science

*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,   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!
RUBRICS:
Design   the   Exception class   (20   points):
Constructing   the   Try…   Catch…   Throw within   the   Driver   class   (40   points)
Constructing   Error   Message   for   the   Exception   class   (10   points)
Constructing   other   error   messages   (20   points)
Loop   and   other   constructs   to   complete   the   program   (10   points)

Solutions

Expert Solution

Solution - Required code has been provided below along with the output. Comments have been placed in the code to depict the functionality of the various statements/functions.

There is an exception class defined, in which the constructor has been defined to print the required error message when the string length is too long. Code runs until user enters "DONE" (all in caps)

Code


import java.util.*;

//Except class 
class ManyCharactersException extends Exception 
{

        //Definded Constructor to print error when exception is thrown
 ManyCharactersException(String s)
 {
        System.out.println(s);
 }

}


public class ExceptionTest {

        public static void main (String args[])
        {
                //Scanner to read user input
                Scanner reader = new Scanner(System.in);

                //input from user with a loop 
                System.out.println("Enter strings. When finished enter DONE");
                String s = reader.nextLine();

                while (s.equals("DONE") == false)
                {
                        try 
                        {
                                if (s.length() > 20)
                                {
                                        //throw the exception with the message
                                        throw new ManyCharactersException("You entered too many characters");                           
                                }
                                else 
                                {
                                        System.out.println("You entered: "+s);
                                        s= reader.nextLine();
                                }
                        }
                        catch(ManyCharactersException exp)
                        {
                                s= reader.nextLine();
                        }
                }
                System.out.println("You entered: "+s);
                System.out.println("Good bye!");

        }
}

Output: user input has been shown in purple colour.

java ExceptionTest
Enter strings. When finished enter DONE
Short string
You entered: Short string
medium string
You entered: medium string
very very very long string
You entered too many characters
DONE
You entered: DONE
Good bye!


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....
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* 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...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...
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...
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT