Question

In: Computer Science

CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create...

CREATE A NEW Java PROGRAM that demonstrates :

1.1 numeric and 1 string exception

2. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.

Solutions

Expert Solution

Java Program:

//Exception Class
class UserException extends Exception{
   String str1;
   // Constructor
   UserException(String val) {
       //Storing message
       str1=val;
   }
   //Method that prints the message
   public String toString(){
       return ("UserException Occurred: " + str1) ;
   }
}

class ExceptionDemonstrater {
   //Main method
   public static void main(String[] args) {
      
       //Number Exception
       try
       {
           //Trying to convert String to integer - Generates Exception
           int n = Integer.parseInt("One");
       }
       catch(NumberFormatException ex)
       {
           //Handling and printing exception message
           System.out.println(ex.toString());
       }
      
       //String Exception
       try
       {
           //Sample String
           String s = "Hello World";
           //Trying to fetch a character from string with invalid index value - Generates Exception
           char ch = s.charAt(100);
       }
       catch(StringIndexOutOfBoundsException ex)
       {
           //Handling and printing exception message
           System.out.println(ex.toString());
       }
      
       //User Defined Exceptions
       try
       {
           //Throwing Exception
           throw new UserException("User Defined Exception Generated.");
       }
       catch(UserException ex)
       {
           //Handling and printing exception message
           System.out.println(ex.toString());
       }
   }
}

_________________________________________________________________________________________

Sample Run:


Related Solutions

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
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string and number exception handling, as well as a user defined exception CREATE A NEW PROGRAM that demonstrates Any 2 of the following 3 choices: 1 numeric and 1 string exception make the program require 1 input argument and throw an exception if there are no command line arguments passed in. In the exception, inform the user that an input argument is required. (You can...
*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...
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available...
Create a program that accepts in a string of 2 or more words. The program then...
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you run the program first time for example it will print 3 then when you run the program for the second time it will give a random number for example 9 but number 7 will not be printed because we need you to do exception for it.
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string....
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string. Ex, "$6.25", "$0.21", "$4.01", "$2.00". MAKE SURE TO CONSIDER ALL EXAMPLES! b) *String moneyToText(int[] money); // Returns the Money as words. Ex,{123,51} => "one hundred and twenty three dollars and fifty one cents." YOU MAY ASSUME money <$1000. context: this is what I have so far package com.company; import java.util.*; public class Main { public static void main(String[]args) { int money[] = createMoney(12, 34);...
*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,  ...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT