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;
}