Create an application to check Exception while performing mathematical equatations like Sin,Cos,etc.

Create an application to check Exception while   performing      mathematical equatations like Sin,Cos,etc.

Problem Statement :

Develop a class Expr to create and evaluate given expression. Constructor accepts the expression as String. For example, Expr("x^2") or Expr("sin(x)+3*x"). If the parameter in the constructor call does not represent a legal expression, then the constructor throws an IllegalArgumentException. The message in the exception describes the error. Provide eval(double num) and eval(int num) method to evaluate given expression and return evaluated answer. For example, if Expr represents the expression 3*x+1, then function value(5) is 3*5+1, or 16.Finally, getDefinition() returns the definition of the expression. This is just the string that was used in the constructor that created the expression object

Introduction:

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

 An exception can occur for many different reasons. Following are some scenarios where an exception occurs.

·       A user has entered an invalid data.

·       A file that needs to be opened cannot be found.

·       A network connection has been lost in the middle of communications or the· JVM has run out of memory.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. Based on these, we have three categories of Exceptions. You need to understand them to know how exception handling works in Java. 

Checked exceptions: A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.

For example, if you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception

Unchecked exceptions: An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.

Program

import java.io.*;

import java.util.*;

import java.lang.IllegalArgumentException;

class exp

{

String z="x+y",a;

boolean w;

 

            int x,y,c;

Scanner s=new Scanner(System.in);

exp(String st)

{

a=st;

            if(z.equals(a))

                        {

                                    w=true;

                        }

                        else

                        {

                                    w=false;

                        }

                        try

                        {

                        if(w==true)

                        {

                        System.out.println("Expression is equal");

                        System.out.println("Enter the value of x,y");

                                    x=s.nextInt();

                                    y=s.nextInt();

                                    c=x+y;

                                    System.out.println("Addition is="+c);

                        }

                        else

                        {

                                               

                                    throw new IllegalArgumentException("Not equal");

                        }

            }

            /*catch(IllegalArgumentException e)

            {

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

            }*/

            finally

            {

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

            }

}

public static void main(String args[])

{

            Scanner s=new Scanner(System.in);

            String a1;

            System.out.println("Enter the expression:");

            a1=s.next();

            exp e1=new exp(a1);

}

}

Post a Comment

Previous Post Next Post