Question

In: Computer Science

Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string...

  1. Create a program with the exception example code from Hour14Exceptions.java
  1. the Hour14Exceptions shows examples of string and number exception handling, as well as a user defined exception
  2. CREATE A NEW PROGRAM that demonstrates Any 2 of the following 3 choices:
    1. 1 numeric and 1 string exception
    2. make the program require 1 input argument and throw an exception if there are no command line arguments passed in.
      1. In the exception, inform the user that an input argument is required. (You can go further, and require a numeric in put, and throw an exception if a non numeric string is passed in)
    3. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.
    4. COMMENTS ARE REQUIRED IN THE CODE TO EXPLAIN WHAT IS HAPPENING OR IT WILL BE MARKED INCOMPLETE

__________________________________________________________________________

The Hour14Exceptions.java Code

package hour14exceptions;

//user defined exception accepts string as input
class MyException extends Exception
{
public MyException(String s)
{
// Call constructor of parent Exception
super(s);
}
}

class MyException_Demo
{
public static void main(String args[])
{
   //divide by 0; arithmetic exception
try {
int n1 = 30, n2 = 0;
int n3 = n1/n2;
System.out.println ("Answer=" + n3);
}
catch(ArithmeticException e) {
System.out.println ("ArithmeticException");
}

   //null string pointer (would work for any bad pointer)
try {
String mys = null; //no data in string
System.out.println(mys.charAt(0));
}
catch(NullPointerException e) {
System.out.println("NullPointerException");
}

//string character index out of bounds exception
try {
String mysoob = "Test for out of bounds character access";
char myc = mysoob.charAt(99);
System.out.println(myc);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
}

//array index out of bounds exception
try{
int myarray[] = new int[20];
myarray[20] = 9; // last element of array is [19]
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("ArrayIndexOutOfBoundsException");
}
try {
// convert non numeric string to number = format exception
int mynum = Integer.parseInt ("qwerty") ;
System.out.println(mynum);
}
catch(NumberFormatException e) {
System.out.println("NumericFormatException");
}

//custom exception
try
{
for (int idx = 0; idx < 6; ++idx)
{
switch (idx)
{
//all 'even' values are acceptable
case 0:
case 2:
case 4:
System.out.println("EvenValue=" + idx);
break;
  
case 1:
case 3:
case 5:
// Throw an object of user defined exception on 3 only
if (idx == 3)
throw new MyException("String to print with user information = " + idx);
else
System.out.println("OddValue=" + idx);
break;
}
}
}
catch (MyException e)
{
System.out.println("User exception caught!!!");
// Print the message from MyException object
System.out.println(e.getMessage());
}
}
}

Solutions

Expert Solution

Code:

package assignment;
import java.util.Scanner;
class SI extends Exception{
   public SI(String s)
   {
   // Call constructor of parent Exception
   super(s);
   }}
public class Hour14Exceptions{
   static void validate(int age)throws SI{
   if(age<18)
   throw new SI("not valid");
   else
   System.out.println("welcome to vote");
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner sc=new Scanner(System.in);
       System.out.println("choose the Exception");
       System.out.println("1. String Exception");
       System.out.println("2. numeric Exception");
       System.out.println("3. Exit");
       int n=sc.nextInt();
       switch(n) {
       case 1:
           try
       {
       // Throw an object of user defined exception
       throw new SI("Mouni"); }
       catch (SI ex){          
       System.out.println(ex.getMessage()); //// Print the message from SI object
       }
           break;
       case 2:
           try{
           validate(13);}//throws the user defined Exception
                       catch(Exception m){System.out.println("Exception occured: "+m);}
           break;
       case 3:break;
       }}}

Note:***I hope your happy with my answer****If you like my answer please give me upvote***Thank you.....


Related Solutions

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.
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
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.
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
In python Using the example code from the HangMan program in our textbook, create a Word...
In python Using the example code from the HangMan program in our textbook, create a Word Guessing Game of your choice. Design a guessing game of your own creation. Choose a theme and build your words around that theme. Keep it simple. Note: I would highly recommend closely reviewing the HangMan code and program from Chapter 10 before starting work on this project. You can run the program via my REPL (Links to an external site.). Using python Similar to...
string string_long_division (string dividend, string divisor) { string ans; //code in here return ans; } Create...
string string_long_division (string dividend, string divisor) { string ans; //code in here return ans; } Create a function that takes in 2 strings called dividend (is a string because this could be greater than the size of an int) and divisor (is a string because this could be greater than the size of an int) and performs long division on them and returns the result as a string. It would be preferred if the code was written in c++. THIS...
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...
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...
Program #1: You will be writing code for several different examples of conditional execution. Each example...
Program #1: You will be writing code for several different examples of conditional execution. Each example should be written as a unique method. Each method should include a method header. So the basic outline of your code is: public class Branching { /** * This is an example method header. Notice how it's multi-line and written * using college-level English and not fragments - this is for the Human to * read and not the compiler to parse, so be...
(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using...
(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using semaphores. (Full program)Write code showing the elevator algorithm. c++ Language
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT