Computer Scientists are Pretty Pessimistic

Sunday, 28 August 2016

Exception Handling in JAVA

# An exception is an abnormal condition that arises in a code sequence at run time.

Java exception handling is managed via five keywords : -
                       Try, Catch, Throw, Throws and Finally

Try : -
       Program statements that you want to monitor for exceptions are contained within a try block.

Catch : -
      Catched an exception.

Throw : -
     To manually throw an exception use the keyword throw.

Throws : -
     Any exception that is thrown out of a method must be specified as such by a throws clause.

Finally : -
     Any code that absolutely must be executed after a try block completes is put in a finally block.

# Once an exception is thrown program control transfers out of the try block into the catch block.

                                        
                    java.lang.Arithmetic Exception: divide by zero
                    at exc2.main(exc2.java.18)

Thursday, 11 August 2016

Conversion of an Infix expression into Postfix expression


Algorithm : :
S <- empty stack
S <- ‘#’
j <- 0
i <- 0
Append '$' sign at last of the expression
while top(s) ≠ ‘$’ do
        Case:
           E[i] is an operand or number :
                      j <- j+1
                      P[j] <- E[i]
            E[i] = ‘(‘ :
                      S <= E[i]
            E[i] = ‘)’ :
                      x <= S
                      while x ≠ ‘(‘ do
                           j <- j+1
                           P[j] <- x
                           x <= S
             E[i] is an operator :
                       while prec(E[i]) ≤ prec(top(S)) do
                                 j <- j+1
                                 P[j] <=S
                                  S <= E[i]
        i <- i+1


Precedency Table
Character
Precedency
#
0
$
1
(
2
+ , -
3
*,  /
4
^
5

CODE :: 

#include <iostream>
#include <map>
#include <stack>
#include <cstdio>
#include <cstring>
using namespace std;
int prec(char c)
{
    map<char,int>mp;
    mp['#']=0;
    mp['$']=1;
    mp['(']=2;
    mp['+']=3;  mp['-']=3;
    mp['*']=4;  mp['/']=4;
    mp['^']=5;
    return mp[c];
}
int main()
{
    char E[10000],P[10000];
    stack<char>st;
    st.push('#');
    int j=0,i=0;
    gets(E);
    strcat(E,"$");
    while(st.top()!='$')
    {
        if(isalpha(E[i]))
        {
            P[j++] = E[i];
        }
        else if(E[i]=='(')
        {
            st.push(E[i]);
        }
        else if(E[i]==')')
        {
            char x = st.top(); st.pop();
            while(x!='(')
            {
                P[j++] = x;
                x = st.top(); st.pop();
            }
        }
        else
        {
            while(prec(E[i])<=prec(st.top()))
            {

                P[j++] = st.top(); st.pop();
            }
            st.push(E[i]);
        }
        i++;
    }
    for(int i=0;i<j;i++)
        cout<<P[i];
    cout<<endl;
}

Wednesday, 22 June 2016

What is Automatic Type Conversion and Automatic Type Promotion ?

Automatic Type Conversion:

Automatic conversion between compatible type is called type conversion. When one type of data is assigned to another type of variable an automatic type conversion will take place if the following two conditions are met,

1. The two types are compatible.

2. The destination type is larger than the source type.


Automatic Type Promotion:

While evaluating expressions Java automatically promotes all byte, short and char values to int and in case of the presence of a float or double they are promoted to float or double respectively. This is called automatic type promotion. 

Access Specifiers In Java

There are four types of access specifiers in java : -

1. Anything declared public can be accessed from anywhere.

2. Anything declared private can not be seen outside of its class.

3. Anything declared as default access (package private) can be accessed from anywhere in the same package.

4. Anything declared as protected can be accessed from anywhere in the same package plus from only its sub classes outside the package. 

Is Java compiled language or interpreted language or both ? Give justification to your answer.


Generally a programming language is compiled or interpreted. But Java is both compiled and interpreted. Because firstly a Java source code converted into byte code. This byte code is machine independent. After that this byte code converted into machine code by Java interpreter. So, Java is called both compiled and interpreted language. And also java is known two stage programming language.



public static void main ( String args[] )

public : main () method must be declared as public since it must be called by code outside of its class when the program is started.

static : The keyword static allows main () method to be called without having to instantiate a particular instance of the class. This is necessary since main () method is called by the JVM before any objects are made.

main : All java application begin execution by calling main () method. So, we need a main method must in our code.

String args[] : In main () method there is only one parameter which is an array of instances of the class String. Objects of type String store character Strings. In this case args receive any command line arguments present when the program is executed. 

Friday, 17 June 2016

Queue Implementation with link list

Algoritm ::

Q <= x  : // enqueue operation
                l ← getcell
                if l = nil then overflow
                else
                      info ( l ) ← x
                      link ( l ) ← nil
                      link (r ) ← l
                       r ← l
x <= Q : // dequeue operation
                t ← link ( f )
                if t = nil then underflow
                else
                      x ← info ( t )

                      link ( f ) ← link ( t )

CODE ::
#include <iostream>
#include <cstdlib>
using namespace std;
struct node{
    int data;
    struct node *link;
}*t,*r,*f;
void enqueue(int x)
{
    node *l = (node*) malloc(sizeof(node));
    l->data = x;
    l->link = NULL;
    r->link = l;
    r=l;
}
int dequeue()
{
    t=f->link;
    if(t==NULL)
    {
        cout<<"Underflow"<<endl;
        exit(0);
    }
    int x = t->data;
    f->link = t->link;
    return x;
}
int main()
{
    node *head = (node*) malloc(sizeof(node));
    r=head;
    f=head;
    for(int i=0;i<5;i++)
    {
        int x= rand()%100;
        enqueue(x);
    }
    for(int i=0;i<5;i++)
    {
        cout<<dequeue()<<" ";
    }
}