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!

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


Class stringArray
{
    public string Name { get; set; }
}


class ManyCharactersException: Exception
{
    public ManyCharactersException()
    {

    }

    public ManyCharactersException(string name)
        : base(String.Format("You entered too many characters"))
    {

    }
  
}

class Program
{
    static void Main(string[] args)
    {
        StringArray newstringArray = null;

char str[1000];
While(1)
{
   printf( "Enter a value :");
   gets( str );

          
        try
        {               
            newstringArray = new stringArray();
            newstringArray.Name = str;
            
            ValidateArray(newstringArray);
        }
        catch(ManyCharactersException ex)
        {
            Console.WriteLine(ex.Message );
        }
          

        Console.ReadKey();
}
    }

    private static void ValidateArray(stringArray str)
    {
       if (strlen(str.Name > 20)
             throw new ManyCharactersException(str.Name);
       else if
            (str.Name = “Done”)
            Console.WriteLine("Goodbye");
            Break;
       else
            Console.WriteLine(“you entered” );
            Console.WriteLine(str.Name);
    }
}

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