Tuesday, 28 January 2014

JAVA: Count the number of instances of substring within a string without using subString function

package PACKAGE1;

import java.io.*;

public class subStringCheck {
 static String strng;
 static char st[];

 public static void main(String args[]) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the String");
  strng = br.readLine();
  st = strng.toCharArray();
  subStringCheck obj = new subStringCheck();
  System.out.println("Enter the sub string");
  obj.substring(br.readLine());
 }

 public void substring(String str) {
  char[] s1 = str.toCharArray();
  int length = str.length();
  int i = -1;
  int count = 0;
  int noOfTimes = 0;
  boolean repeated = false;

  while (i < (strng.length() - 1)) {
   for (int s = 0; s < str.length(); s++) {
    i++;
    if (st[i] == s1[s]) {
     count++;

     if (count == length) {
      noOfTimes++;
      repeated = true;
      break;
     }
    } else {

     count = 0;
     break;

    }

   }

  }

  if (repeated == true) {
   System.out.println("The substring has repeated " + noOfTimes
     + " times");
  } else {

   System.out.println("Theere is no substring");
  }
 }

}



Monday, 27 January 2014

JAVA: Displaying the position of the compass.

Get the direction and display the position of the compass.

public class directionProgram {

 static char dir[] = new char[4];
 static int R;

 public static void main(String args[]) {

  dir[0] = 'N';
  dir[1] = 'E';
  dir[2] = 'S';
  dir[3] = 'W';

  directionProgram obj = new directionProgram();
  obj.getDirection("L", 3);
  obj.getDirection("L", 1);
  obj.getDirection("L", 2);
  obj.getDirection("R", 1);
  System.out.println("the position is " + dir[R]);
 }

 public void getDirection(String d, int rontations) {

  if (rontations >= 4) {
   rontations = rontations % 4;
  }

  if (d == "R") {
   R = R + rontations;
  } else {
   R = R + 4 - rontations;

  }
  if (R >= 4) {
   R = R % 4;
  }
 }
}


JAVA: Divide an int array in to two arrays so that the sum of elements in each subarray is equal , if division is not possible display a error message to the user.

package PACKAGE1;

import java.io.*;
import java.util.Arrays;

public class programRareMile {

 public static void main(String args[]) throws NumberFormatException,
   IOException {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("enter the no. of elements to enter");
  int n = Integer.parseInt(br.readLine());
  int x[] = new int[n];
  System.out.println("Enter " + n + " elements");
  int half;
  int sum = 0;

  for (int i = 0; i < n; i++) {

   x[i] = Integer.parseInt(br.readLine());
  }

  
  for (int i = 0; i < n; i++) {
   sum = sum + x[i];
  }

  if (sum % 2 != 0) {
   System.out.println("the sum is odd and cannot be divided");
   System.out.println("The sum is " + sum);
  } else {
   boolean div = false;
   half = sum / 2;
   int sum1 = 0;
   for (int i = 0; i < n; i++) {

    sum1 = sum1 + x[i];
    if (sum1 == half) {
     System.out.println("array can be divided");
     div = true;
     break;
    }

   }

   if (div == true) {
    int t = 0;
    int[] array1 = new int[n];
    int count = 0;
    for (int i = 0; i < n; i++) {
     t = t + x[i];
     if (t <= half) {
      array1[i] = x[i];
      count++;
     }
    }
    array1 = Arrays.copyOf(array1, count);
    int array2[] = new int[n - count];
    int k = 0;
    for (int i = count; i < n; i++) {
     array2[k] = x[i];
     k++;
    }
    System.out.println("The first array is ");
    for (int m : array1) {

     System.out.println(m);
    }
    System.out.println("The second array is ");
    for (int m : array2) {

     System.out.println(m);
    }
   } else {
    System.out.println("array cannot be divided");
   }
  }
 }

}


Monday, 20 January 2014

JAVA : selection sort algorithm.

Selection Sort.

public class BubbleSort{

 static int no_of_iteration;

 public static void main(String args[]) throws NumberFormatException,
   IOException {
  BufferedReader buff = new BufferedReader(new InputStreamReader(
    System.in));
  System.out.println("How many no.s to sort");
  int n = Integer.parseInt(buff.readLine());

  int arr[] = new int[n];
  System.out.println("entered " + n + " elements");
  for (int i = 0; i < n; i++) {

   arr[i] = Integer.parseInt(buff.readLine());

  }
  System.out.println("entered elements are");
  for (int i : arr) {

   System.out.println(i);
  }
  for (int i = 0; i < n - 1; i++) {
   no_of_iteration++;
   for (int j = i + 1; j < (n); j++) {
    no_of_iteration++;
    if (arr[i] < arr[j]) {

     int temp = arr[j];
     arr[j] = arr[i];
     arr[i] = temp;

    }
   }
  }
  System.out.println("sorted elements are");
  for (int i : arr) {

   System.out.println(i);
  }
  System.out.println("total no of iteration" + no_of_iteration);
 }

}

Thursday, 16 January 2014

JAVA: Implementing caching in JAVA using weakHashMap() in ORM framework.

If there are static queries  and once you hit the database , next time you dont need to hit the DB again , whereas you store it (caching) and get the values back
I have implemented this using three classes - class1,class2 and class3

class3 invoking the class1 object 

public class class3 {

  public static void main(String args[]) throws InterruptedException{
     class1 obj1= new class1();
     Thread t1 = new Thread(obj1,"Thread1");
     t1.start();
     t1.join();
     Thread t2 = new Thread(obj1,"Thread2");
     t2.start();
  }

}


class 2 has many variables which are mapped to database

public class class2 {

 int x;

/*

many variables mapped to database

*/

}

class1 implements cache. class 1 stores the object of the class2 and next time it will not hit the database ,it will get the object from the map and calls the variables

import java.util.*;

import org.apache.naming.java.javaURLContextFactory;

public class class1 implements Runnable {

 int i;
 Map map = new WeakHashMap();
 class2 obj = new class2();

 public  void run(){
  if(i==0){
  // database connection code;
  obj.x=10;// assign the field values to the class2 obj
  map.put(obj.getClass(), obj); // save the object in the hashMap
  System.out.println(Thread.currentThread().getName()+" "+obj.x);
  i++;  //increment the integer.
  }
  else{
  class2 x =(class2)map.get(obj.getClass());
  System.out.println("i am in else part");
  System.out.println(Thread.currentThread().getName()+" "+x.x);

 }
}
}



Wednesday, 1 January 2014

JAVA: Finding the duplicate elements in the String.




public class duplicateCharInString {

 public void findDuplicateChar(String x) {
  char[] character = x.toCharArray();
  for (int i = 0; i < character.length; i++) {
   boolean y = false;
   int k = 0;
   for (int j = 0; j < character.length; j++) {
    if (character[i] == character[j]) {
     if (j >= i) {
      k++;
     } else {
      y = true;
      break;
     }
    }
   }
   if (y == false && k > 1) {
    System.out.println(character[i] + " repeated " + k + " times");
   }
  }
 }

 public static void main(String args[]) {
  duplicateCharInString obj = new duplicateCharInString();
  obj.findDuplicateChar("ibmcompanyhello");
 }
}