Create an application to check Exception in Roman Numbers also convert it and check its range

 

Create an application to check Exception in Roman Numbers  also convert it and check its range.

Problem Statement :

Write a class to represent Roman numerals. The class should have two constructors. One constructs a Roman numeral from a string such as "XVII" or "MCMXCV". It should throw a NumberFormatException if the string is not a legal Roman numeral. The other constructor constructs a Roman numeral from an int. It should throw a NumberFormatException if the int is outside the range 1 to 3999.In addition, the class should have two instance methods. The method toString() returns the string that represents the Roman numeral. The method toInt() returns the value of the Roman numeral as an int.

Introduction :

Java supports five Exception handling statements

·       Try

·       Catch

·       Throw

·       Throws

·       Finally

A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code. The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by a catch block or finally block. A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.

If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature. You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the difference between throws and throw keywords, throws is used to postpone the handling of a checked exception and throw is used to invoke an exception explicitly.

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception. Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code

 

Program

import java.io.*;

import java.util.*;

import java.lang.NumberFormatException;

class exp7

{

String z="XVII",q="MCMXCV",a;

boolean w;

int x,y,c,o;

Scanner s=new Scanner(System.in);

exp7(String st)

{

a=st;

if(z.equals(a)||q.equals(a))

{

w=true;

}

else

{

w=false;

}

try

{

if(w==true)

                  {

      System.out.println("Roman numeral is equal");

}

else

      {

                                         

     

throw new NumberFormatException("Not equal");

}

}

catch(IllegalArgumentException e)

{

System.out.println("wrong expression");

}*/

finally

{

System.out.println("finally");

      }

}

exp7(int d)

{

o=d;        

try

{

if(o<=3999)

{

System.out.println("number is in range");        

}

      else

{

throw new NumberFormatException("number out of range");

}

}

finally

{

System.out.println("finally");

}

}

public static void main(String args[])

{

Scanner s=new Scanner(System.in);

     

String a1;

     

System.out.println("Enter the Roman number:");

a1=s.next();

exp7 e1=new exp7(a1);

int f;

System.out.println("Enter number");

f=s.nextInt();

exp7 g=new exp7(f);

}

}

Post a Comment

Previous Post Next Post