Search

Java - Data Structures - One-Dimensional Array Example

package datastructures.linear;

import java.util.Arrays;

class Array{
public static void main(String arg[]) {
System.out.println("One-Dimensional Arrays Example");
ArraySample obj = new ArraySample();
obj.displayArray();
obj.sortArray();
obj.linearSearch(5);
}
}

class ArraySample{
/*
Declaration an array
_____________________
The declaration can be done using the following both ways
type var-name[];
OR
type[] var-name;
*/
int[] intArray1;
int intArray2[];
String stringArray[];
byte byteArray[];
short shortsArray[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
//Object datastructures.linear.Array
Object objectArray[];

//combining both statements (Declaration and Instantiating) in one
int[] intArray = new int[20];

//Defult constructor
public ArraySample(){
/*
Instantiating an datastructures.linear.Array
______________________
var-name = new type [size];
*/
// allocating memory for 5 integers.
intArray1 = new int[5];
stringArray = new String[]{"one","two","three"};

// initialize the first elements of the integer array
intArray1[0] = 6;

// initialize the secound elements of the integer array
intArray1[1] = 2;

// initialize the third elements of the integer array
intArray1[2] = 5;

intArray1[3] = 1;
intArray1[4] = 9;
}

public void sortArray(){
Arrays.sort(intArray1);
System.out.println("---------Sorted datastructures.linear.Array---------");
for (int iterable_element : intArray1) {
System.out.println(iterable_element);
}
}

public void linearSearch(int key){
for (int i = 0; i < intArray1.length; i++)
if(intArray1[i]==key)
System.out.println("Found...! index="+i);
else
System.out.println("Search key not found");

}

public void displayArray(){
//datastructures.linear.Array Length
System.out.println("datastructures.linear.Array size = "+stringArray.length);

// accessing the elements of the specified array
for (int i = 0; i < stringArray.length; i++)
System.out.println("Element at index " + i + " : " + stringArray[i]);


System.out.println("datastructures.linear.Array size = "+intArray1.length);

for (int iterable_element : intArray1) {
System.out.println(iterable_element);
}
}

}

PHP Database Connection-Insert Statement

$con=mysql_connect("localhost","root","");

mysql_select_db("esdb",$con);

$statement="inser into student values('1','Kamal','85')";

$my1=mysql_query($statement);

if($my1) {
    echo "Data Successfully Inserted";
} else {
    echo "Error In Insert";
}

PHP Database Connection-Select Statement

$con=mysql_connect("localhost","user Name","password");

mysql_select_db("esdb",$con);

$statement="select * from Students where id=1";

$my1=mysql_query($statement);

while($row=mysql_fetch_array($my1))
{
    echo $row[0];
}

java socket (sever side)

import java.net.*;
import java.io.*;
public class server{
 public static void main(String args[])throws IOException{
  ServerSocket s=new ServerSocket(1234);
  while(true){
   Socket s1=s.accept();
   DataInputStream is=new DataInputStream(s1.getInputStream());
   String sr=new String(is.readUTF());
   System.out.println(sr);
   DataOutputStream os=new DataOutputStream(s1.getOutputStream());
   os.writeUTF("bla bla bla");
   os.close();
   is.close();
   s1.close();
    
  }
 }
}

java socket (Client part)

import java.net.*;
import java.io.*;
public class client{
 public static void main(String a[]){
 try{
  Socket w=new Socket("localhost",1234);
  DataOutputStream o=new DataOutputStream(w.getOutputStream());
  o.writeUTF("where are you");
  DataInputStream i=new DataInputStream(w.getInputStream());
  String sr=new String(i.readUTF());
  System.out.println(sr);
  o.close();
  i.close();
  w.close();
 }
 catch(UnknownHostException u){
 System.out.println("message"+u);
 }
 catch(IOException i){
 System.out.println("message"+i);
 }
}

}

Bubble Sort in C language

#include
#include
#define N 5
int V[N];
void input(int V[N]);
void sort(int V[N]);
void display(int V[N]);
void main()
{
clrscr();
input(V);
sort(V);
display(V);
getch();
}

void input(int V[N])
{
printf("\n\t****** Enter data tot he array ******\n\n\n");
for(int i=0;i < N;i++)
{
printf("\nEnter value positon %d to the array : ",i+1);
scanf("%d",&V[i]);
}
}

void sort(int V[N])
{
int temp,flag=0;
for(int i=1;i < N;i++)
{
for(int j=0;j < (N-i);j++)
{
if(V[j]>V[j+1])
{
temp=V[j];
V[j]=V[j+1];
V[j+1]=temp;
flag=1;
}
}
if(flag==0)break;
}
}

void display(int V[N])
{
printf("\n\n\n\n\t****** Sorted Array ******\n");
for(int i=0;i < N;i++)
{
printf("\n%d",V[i]);
}
}

java Simple Swing programe

import java.awt.*; 
import javax.swing.*; 
public class myswing1 extends Jframe { 
    public static void main(String arg[]) { 
        Jframe jf1=new Jframe("NIBM"); 
        Jbutton jb1=new Jbutton("OK"); 
        jf1.add(jb1); 
        jf1.setsize(200,300); 
        jf1.setvisible(true); 
    } 
}

Java Interfaces vs. Abstract class

Summary of the similarities and differences between the Interfaces and Abstract class Abstract class Interface Has a constructor Yes No An i...