Monday, June 13, 2011

Handling more than one exception, Java SE 7 new feature.

In Java SE 7 and later, a single catch block can handle 
more than one type of exception. This feature can reduce code 
duplication and lessen the temptation to catch an overly broad 
exception. 
 
consider the following example... 
try{
.............

}
catch (IOException ex) {
     logger.log(ex);
     throw ex;
catch (SQLException ex) {
     logger.log(ex);
     throw ex;
}
 
 
The following example, which is valid in Java SE 7 and later, 
eliminates the duplicated code:
 
catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
} 

Tuesday, May 31, 2011

Installing openNMS on RedHat using rpm.

I am assuming postgres database is already installed.

download the following rpm.


 now installing process...
rpm -ivh opennms-core-1.8.5-1.noarch.rpm
rpm -ivh  jicmp-1.0.6-1.fc8.i386.rpm
rpm -ivh opennms-webapp-jetty-1.8.5-1.noarch.rpm


stop postgres database if it is running
/etc/init.d/postgresql stop

To allow OpenNMS to connect to the database, you will need to edit your database's  
pg_hba.conf file. On many default installations it can be found in the directory
/var/lib/pgsql/data/

local   all   all                      ident sameuser
host    all   all    127.0.0.1/32      ident sameuser
host    all   all    ::1/128           ident sameuser

You will need to change these entries to resemble the following: 


local   all   all                      trust
host    all   all    127.0.0.1/32      trust
host    all   all    ::1/128           trust
Next, you will need to edit postgresql.conf to accept TCP/IP 
connections. As with pg_hba.conf, check the defaults or your 
distribution's PostgreSQL documentation for the location of 
this file. As before, on many default installations it can 
be found in the directory /var/lib/pgsql/data/ Both 
postgresql.conf and pg_hba.conf by default should be 
in the same location.
 
....
#listen_addresses = 'localhost'
 ....

uncomment this line to resemble

....
listen_addresses = 'localhost'
 ....

start the postgres db..
/etc/init.d/postgresql start


go to /opt/opennms/bin/ (default installation dir for opennms)

run the install script...
#>install



after that start opennms

#>opennms start

open http://localhost:8980/opennms/index.jsp

and enjoy...:)

Wednesday, March 9, 2011

Simulating queue using stack...

Problem statement: we are given one stack and we have to implement the queue using this stack means enqueue(insert into stack) and dequeue ( pop from stack) should behave as queue. No additional stack should be used.

This can be done using Operating system stack by using recursion.
public class SimulatedQueue < E > {
    private java.util.Stack < E > stack = 
                     new java.util.Stack < E >();

    public void insert(E elem) {
        if (!stack.empty()) {
            E topElem = stack.pop();
            insert(elem);
            stack.push(topElem);
        }
        else
            stack.push(elem);
    }

    public E remove() {
        return stack.pop();
    }
}








Thursday, September 9, 2010

What is framework and how it differs from library?

A library is essentially a set of functions that we can use in our projects, libraries are bundled into jar (java) usually organized into classes. Each call does some work and returns control to the client.

A framework is same as libraries in the structure but differs in the design and functionality with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by sub-classing or by plugging in your own classes. The framework's code then calls your code at these points.

In short frame is the best possible use of basic libraries, as the frameworks are usually built on the top of libraries.

Inversion of Control is a key part of what makes a framework different to a library.

Wednesday, August 4, 2010

SQL queries

select employee_id,last_name, salary,department_id
from employees
where manager_id = &mid;
--=========================================================
select last_name
from employees
where last_name like '__a%';
--=========================================================
select last_name
from employees
where last_name like ('%a%e%') or last_name like ('%e%a%');
--=========================================================
--is not null
select last_name, salary, commission_pct
from employees
where commission_pct is not null;
--=========================================================
--between two specified dates
select last_name, hire_date
from employees
where hire_date between to_date('1994/01/01',
'yyyy/mm/dd')
AND to_date ('1994/12/31', 'yyyy/mm/dd');

select * from employees
where department_id not in (select department_id
from departments);
--=========================================================
select last_name, department_id,salary
from employees
where department_id IN (20,50) and salary between 5000
and 12000
order by last_name;
--=========================================================
select last_name, job_id

from employees
where last_name IN ('Taylor','Matos');
--=========================================================

Thursday, April 8, 2010

Redireciton of input and output on linux shell..

Syntax:
linux-command redirection-symbol input/output-file-name

There are three basic redirection operators.
  1. output (>)
    ex: $ ls > dirList.txt
    the output of the ls command will write in dirList.txt if the file is already present then it will be overwritten without any warning.

  2. append (>>)
    To output Linux-commands result to the end of file (append). Note that if file exist , it will be opened and new information will be written to end of file, without losing previous information, And if file is not exist, then new file is created. For e.g. To send output of pstree command to already exist file give command
    ex: $ pstree >> processList.txt

  3. input (<) To take input to Linux-command from file instead of key-board. If file does not exist then you have to give using key-board. ex: ./a.out < input.in
In Linux in C/CPP programming Language keyboard, screen etc are all treated as files. these files are:


































File Name

Discriptor

Use

Example

Stdin

0

Standard Input

Keyboard

Stdout

1

Standard Output

Screen

Stderr

2

Standard Error

Screen
In linux every program has three files associated with it, (when we start our program these three files are automatically opened by the shell). The use of first two files (stdin and stdout) , are already seen by us. The last file stderr is used by our program to print error on screen. Error message can't be redirected, for example the Command $ rm file1.txt > getError.txt will not log the error(rm: cannot remove `file1.txt': No such file or directory) into the file getError.txt in case file1.txt does not present, since output is send to error device. But if still want to log the error, use the discriptor for example $ rm file1.txt 2>getError.txt


Friday, April 2, 2010

The logic behind Ethiopian multiplication.

This method of multiplication is also called Egyptian Multiplication (as believed it was developed in Egypt).

How it Works.
  • write two no in the adjacent columns(smaller one in left side,

  • In the left column recursively halve the number, discarding remainders,

  • In the right column recursively double the number and write the result below, do this unlit left column shows 1,

  • Test for the left column if it is odd then add corresponding no in the right column.

Example

A = 34;
B = 12;






















BA
1234
668
3136
1272

_________________________________________
408
_________________________________________

12*34 = 136+272
= 34*4 + 34*8
= 34(4+8)
= 34(22+23)
= 34(12)

The main idea is that break the first no in the power of two and multiplication with 2 is easier. Then add to obtain result.

Useful links for me...

Tuesday, March 30, 2010

Bag Datastructure

An unordered collection of values that may have duplicates.

A bag has a single query function, numberIn(v), which tells how many copies of an element are in the bag, and two modifier functions, add(v) and remove(v).

numberIn(v) = returns no of element in Bag of type v;
add(v) = add a new element v in the bag;
remove(v) = removes all the element from the Bag of type v;
showBag() = prints all the element of the Bag;

Implementation of Bag in cpp(template).

#include < iostream >
#include < string.h >

using namespace std;

template < class Data >

class Bag
{
private:
struct node
{
Data data;
node *link;
}*ptr;
public:
Bag()
{
ptr = NULL;
}

void add(Data a)
{
if(ptr==NULL)
{
ptr = new node;
ptr- > data = a;
ptr- > link = NULL;
}
else
{
node *temp;
temp = new node;
temp- > data = a;
temp- > link = ptr;
ptr = temp;
}
}

void remove(Data data){
int present = 0;
if(ptr==NULL){
cout < < "Error !! Bag is empty" < < endl;
return;
}
else{
node *temp;
node *preTemp;
temp = ptr;
while(temp != NULL){
if(temp- > data == data){
if(temp == ptr){
ptr = ptr- > link;
delete temp;
temp = ptr;
}
else if(temp- > link!=NULL){
preTemp- > link = temp- > link;
delete temp;
temp = preTemp;
}
else if(temp- > link==NULL){
preTemp- > link = NULL;
delete temp;
}
}
else{
preTemp = temp;
temp = temp- > link;
}
}
}
}

int numberIn(Data data){
int count = 0;
if(ptr==NULL)
{
return count;
}
else
{
node *temp;
temp = ptr;
while(temp != NULL){
if(temp- > data == data){
count++;
}
temp = temp- > link;
}
}
return count;
}
void showBag(){
node *temp;
temp = ptr;
while(temp!=NULL){
cout < < " [" < < temp- > data < < "] " < < endl;
temp = temp- > link;
}
}
};



int main()
{
int input,remove;
Bag < int > intBag;

for(int i=0;i < 20;i++)
{
cin > > input;
intBag.add(input);
}
intBag.showBag();
cin > > remove;
cout < < "NumberIN = " < < intBag.numberIn(1) < < endl;
intBag.remove(remove);
cout < < "removed" < < endl;
intBag.showBag();
}

Search Ranjeet's Blog