wrapper classes in Java

Describe the wrapper classes in Java.Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.

Following table lists the primitive types and the corresponding wrapper classes:
Primitive - Wrapper
boolean  - java.lang.Boolean

byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void

Java Class as Applet as well as Application

Can you write a Java class that could be used both as an applet as well as an application?
A. Yes. Add a main() method to the applet.

Bring JFrame JDialog Window to front java swing

To bring JFrame or JDialog ... or Window (JFrame and JDialog inherits Window)   to front in JAVA, fun the code below :

java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    //myFrame is object of Window or JFrame
    public void run() {
        myFrame.toFront();
        myFrame.repaint();
    }
});

Redirect Standard System Output and Error Message to PrintStream in Java

In a Java program, how can you divert standard system output or error messages, say to a file?
->We can achieve this by using
public static void setErr(PrintStream err)
and public static void setOut(PrintStream out) methods.
By default, they both point at the system console. The following example redirects Out and Err messages to 'error.txt'  file

Stream stream = new Stream(new FileOutputStream("error.txt"));
System.setErr(stream);
System.setOut(stream);

You can redirect the Err and Out stream to any PrintStream object.

concatenate arrays of any type - java

Combining two arrays of any type

 public static  T[] concat(T[] first, T[] second) {
  T[] result = Arrays.copyOf(first, first.length + second.length);
  System.arraycopy(second, 0, result, first.length, second.length);
  return result;
 }

For combining arbitrary number of arrays

difference between an interface and an abstract class

What's the difference between an interface and an abstract class? Also discuss the similarities. (Very Important)
Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods
Abstract class is a Class prefix with a abstract keyword followed by Class definition. Interface is a Interface which starts with interface keyword.
Abstract class contains one or more abstract methods. where as Interface contains all abstract methods and final declarations
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.

Differences are as follows:
* Interfaces provide a form of multiple inheritance. A class can extend only one other class.
* Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
* A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
* Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast. 

Similarities:

* Neither Abstract classes or Interface can be instantiated.


How to define an Abstract class?
A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class:

abstract class testAbstractClass { 
    protected String myString; 
    public String getMyString() { 
    return myString; 

public abstract string anyAbstractFunction();
}

How to define an Interface?Answer: In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Example of Interface:

public interface sampleInterface {
    public void functionOne();
    public long CONSTANT_ONE = 1000;
}

Adding image to JPanel Java

Adding Image to JPanel

BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
add( picLabel );

:D

Read time from internet time server - working java code

Read time from internet time server - working java code
import java.io.*;
import java.net.*;
import java.util.*;

/**
 * This program makes a socket connection to the atomic clock in Boulder,
 * Colorado, and prints the time that the server sends.
 */
public class Time_Server_Socket_Test_Java {
    public static void main(String[] args) {
        try {
            Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
            try {
                InputStream inStream = s.getInputStream();
                Scanner in = new Scanner(inStream);

                while (in.hasNextLine()) {
                    String line = in.nextLine();
                    System.out.println(line);
                }
            } finally {
                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



iterate through map hashmap linkedhashmap in java

How to iterate through Map.. MashMap, LinkedHashMap ...  in Java
Method 1 : If you're only interested in the keys, you can iterate through the keySet() of the map:
Map map = ...;
for (String key : map.keySet()) {
    // ...
}

Method 2 :

java escape text in regular expression

Java's a built-in way to escape arbitrary text so that it can be included in a regular expression-
For example, users enter "$5", we can match that exactly rather than a "5" after the end of input.


Just do this : to escape text in regex
Pattern.quote("$5");

use of @override annotation in java - why ?

The @override annotation  is most useful as a compile-time reminder that the intention of the method is to override a parent method. See this example:

import java.util.HashSet;
import java.util.Set;
/**
 * Override_Annotation_Usage_Example, from  Effective Java
 */

java reflection - what is - 101 tutorial


The name reflection is used to describe code which is able to inspect other code in the same system (or itself).
A simple code example of this in Java (imagine the object in question is foo) :
Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);
One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.
There are some good reflection examples to get started : at http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html

Bubble sort working source code - C/C++

Bubble sort working source code - C/C++
#include <stdio.h>
#include <iostream.h>

void bubbleSort(int *array,int length)//Bubble sort function 
{
    int i,j;
    for(i=0;i<length;i++)
    {
        for(j=0;j<i;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swap 
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
}

void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<length;i++)
        cout<<array[i]<<endl;
}


void main()
{

    int a[]={9,6,5,23,2,6,2,7,1,8};   // array to sort 
    bubbleSort(a,10);                 //call to bubble sort  
    printElements(a,10);               // print elements 
}


C C++ CODE : Shooting method for solving boundary value problem

Working C C++  Source code program for Shooting method for solving boundary value problem
#include<iostream.h>
#include<conio.h>
float f1(float ,float,float);
float f2(float, float ,float);
int main()
{
    int i;

C C++ CODE : Simpsons 1/3 rule for integration

Working C C++  Source code program for Simpsons 1/3 rule for integration
/************** SIPMPSONS 1/3 RULE ***********************/

#include<iostream.h>
#include<conio.h>
#include<math.h>
float funct(float a);
int main()
{
    char choice='y';
    float f,x,h,a,b,sum;
    clrscr();
    cout<<"a & b ? ";cin>>a>>b;
    do
    {
        sum=0;
        x=a;
        cout<<"Enter value of h ? ";cin>>h;
        while(x<b)
        {
            sum+=(funct(x)+4*funct(x+h)+funct(x+2*h));
            x=x+2*h;
        }
        cout<<endl<<"The integration is: "<<sum*h/3<<endl;
        cout<<endl<<"wanna continue (y/n) ? ";cin>>choice;
    }while(choice=='y');
    getch();
    return 0;
}

float funct(float x)
{
    return x*exp(x)-2;   // sin takes arguments in radian........
}

C C++ CODE : Trapezoidal rule for integration

Working C C++  Source code program for Trapezoidal rule for integration
/************* TRAPEZOID FULE FOR INTEGRATION *****************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
float funct(float a);
int main()
{
    char choice='y';
    float f,x,h,a,b,sum;
    clrscr();
    cout<<"a & b ? ";cin>>a>>b;
    do{
        sum=0;
        x=a;
        cout<<"Enter value of h ? ";cin>>h;
        while(x<b)
        {
            sum+=(funct(x)+funct(x+h));
            x=x+h;
        }
        cout<<endl<<"The integration is: "<<sum*h/2<<endl;
        cout<<endl<<"wanna continue (y/n) ? ";cin>>choice;
    } while(choice=='y');
    getch();
    return 0;
}

float funct(float x)
{
    return x*exp(x)-2;   // sin takes arguments in radian........
}

C C++ CODE : Numerical integration for tabular data

Working C C++  Source code program for numerical integration - trapeziode and simpsons 1/3 method
/************************ INTEGRATION FOR TABULAR DATA *****************/
#include<iostream.h>
#include<conio.h>
int main()
{
    int n,i;
    float x[10],f[10],h,sum=0,a;
    clrscr();
    cout<<"No of datas ? ";cin>>n;
    cout<<"Enter datas : ";
    for(i=0;i<n;i++)
        cin>>x[i]>>f[i];
        
    for(i=0;i<n;i++)
    {
        if(i==0||i==n-1)
            sum+=f[i];
        else
            sum+=2*f[i];
    }
    h=x[1]-x[0];
    cout<<"The value using trapezoide: "<<h*sum/2;
    a=x[0];
    sum=0;
    while((a-h)<x[n])
    {
        sum+=(f[a]+4*f[a+h]+f[a+2*h]);
        a+=2*h;
    }
    cout<<"\nThe value using simpsons 1/3 is : "<<h*sum/3;
    getch();
    return 0;
}


C C++ code : power method - numerical method to find eigen value and vector

Working C C++  Source code program for finding eigen value and eigen vector by power method
/************* Eigen value and eigen vector by Power method ***********/

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    float a[10][10],x[10],c[10],d=0,temp;
    int n,i,j;

C C++ CODE: Gauss Jordon elimination method to solving linear equations

Working C C++  Source code program for Gauss Jordon elimination method to solving linear equations
/*************** Gauss Jordan method ********************/
#include<iostream.h>
#include<conio.h>
int main()
{
    int i,j,k,n;
    float a[10][10],d;
    clrscr();

    cout<<"No of equations ? ";cin>>n;
    cout<<"Read all coefficients of matrix with b matrix too "<<endl;
    for(i=1;i<=n;i++) // read nxn matrix - cofficients
        for(j=1;j<=n+1;j++)
            cin>>a[i][j];

    /************** partial pivoting **************/
    for(i=n;i>1;i--)
    {
        if(a[i-1][1]<a[i][1])
        for(j=1;j<=n+1;j++)
        {
            d=a[i][j];
            a[i][j]=a[i-1][j];
            a[i-1][j]=d;
        }
    }
    cout<<"pivoted output: "<<endl;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
            cout<<a[i][j]<<"    ";
        cout<<endl;
    }
    /********** reducing to diagonal  matrix ***********/

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        if(j!=i)
        {
            d=a[j][i]/a[i][i];
            for(k=1;k<=n+1;k++)
                a[j][k]-=a[i][k]*d;
        }
    }
    /************** reducing to unit matrix *************/
    for(i=1;i<=n;i++)
    {
    d=a[i][i];
        for(j=1;j<=n+1;j++)
            a[i][j]=a[i][j]/d;
    }


    cout<<"your solutions: "<<endl;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
            cout<<a[i][j]<<"    ";
        cout<<endl;
    }

    getch();
    return 0;
}

C C++ CODE : Gauss jordan method for finding inverse matrix

Working C C++  Source code program for Gauss jordan method for finding inverse matrix
/*************** Gauss Jordan method for inverse matrix ********************/
#include<iostream.h>
#include<conio.h>
int main()
{
    int i,j,k,n;
    float a[10][10]={0},d;
    clrscr();

C C++ CODE : LU Decomposition for solving linear equations

Working C C++  Source code program for LU Decomposition for solving linear equations
/************** LU Decomposition for solving linear equations ***********/
#include<iostream.h>
#include<conio.h>
int main()
{
    int n,i,k,j,p;
    float a[10][10],l[10][10]={0},u[10][10]={0},sum,b[10],z[10]={0},x[10]={0};
    clrscr();
    cout<<"Enter the order of matrix ! ";
    cin>>n;

C C++ CODE : Gauss elimination for solving linear equations

Working C C++  Source code program for Gauss elimination for solving linear equations
/************* Gauss elimination for solving linear equations *************/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int n,i,j,k,temp;
    float a[10][10],c,d[10]={0};
    clrscr();
    cout<<"No of equation ? ";
    cin>>n;

C C++ CODE : least square fitting regression

Working C C++  Source code program for least square fitting regression
/*************** least square fitting ******************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
    int n,i,j;
    float a,a0,a1,x[10],f[10],sumx=0,sumy=0,sumxy=0,sumx2=0;
    clrscr();
    cout<<"Enter no of sample points ? ";cin>>n;

C C++ CODE: Lagrange's interpolation

Working C C++  Source code program for Lagrange's interpolation
/********** Lagrange's interpolation ***************/

#include<iostream.h>
#include<conio.h>
int main()
{
    int n,i,j;
    float mult,sum=0,x[10],f[10],a;
    clrscr();

C C++ CODE for Newton's interpolation

Working C C++  Source code program for Newton's interpolation
/***************** Newtons interpolation **************/
#include<iostream.h>
#include<conio.h>
int main()
{
    int n,i,j;
    float x[10],f[10],a,sum=0,mult;
    clrscr();

C C++ CODE: Cubic Spline Interpolation

Working C C++  Source code program for Cubic Spline Interpolation
/********************* Cubic Spline Interpolation **********************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
    char choice='y';
    int n,i,j,k;

C C++ code : horner's synthetic division

Working C C++  Source code program for horner's synthetic division method for finding solution of polynomial equation.
//horner's polynomial soln, synthetic division
#include <iostream.h>
#include <conio.h>
#include <complex.h>
void main()
{
    int it,n;
    complex a[10],b[10],c[10],x;
    clrscr();
    cout<<"Enter the degree of the polynomial:";
    cin>>n;

C C++ code : Newton - Horner's method for solution of polynomial equation

Newton - Horner's method for finding solution of polynomial equation
/***************** Newton horner's method ******************/
#include<iostream.h>
#include<conio.h>
#include<complex.h>
#include<math.h>
int main()
{
    complex a[20],b[20],c[20];
    complex x;
    int n,i;
    clrscr();

C C++ code- numerical differentiation of given equation

Working C C++  Source code program  for two point - three point numerical differentiation of given equation
/*****************  NUMERICAL DIFFERENTION *********************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
float funct(float a);
int main()
{
    char choice='y';

C C++ code : Bisection method for solving non-linear equation

Working C C++  Source code program : Bisection method for solving non-linear equation
#include<iostream.h>
#include<conio.h>
#include<math.h>
float funct(float);

int main()
{

C C++ Code : Newton rapshon's method for solving non-linear equation

Working C C++  Source code program for newton rapshon's method for solving non-linear equations.
/*Newton Rapsons method : for solving non linear equations*/
#include<iostream.h>
#include<conio.h>
#include<math.h>

double funct(double);
double derv(double);