Tuesday, 31 December 2013

JAVA: Implementing ArrayList in JAVA - Simple program



package Programs;

import java.util.Arrays;

public class implementingArrayList {

 int get[] = new int[10];
 int size;

 public void add(int x) {
  if (size != 0) {
   if (size % 10 == 0) {

    get = Arrays.copyOf(get, (get.length + 10));

   }
  }
  get[size] = x;
  ++size;
 }

 public void remove(int x) {

  for (int i = 0; i < get.length; i++) {
   if (get[i] == x) {
    for (int j = (i + 1); j < (get.length - 1); j++) {
     get[i] = get[j];
     i++;
    }

    size--;
    get = Arrays.copyOf(get, get.length - 1);
    break;
   }
  }

 }

 public static void main(String args[]) {
  implementingArrayList list = new implementingArrayList();
  list.add(1);
  list.add(2);
  list.add(3);
  list.add(4);
  list.add(5);
  list.add(11);
  list.add(12);
  list.add(13);
  list.add(14);
  list.add(15);
  list.add(21);
  list.add(22);
  list.add(23);
  list.add(24);
  list.add(25);
  list.add(100);
  list.remove(14);
  System.out.println("Size of the list is" + list.size);
  for (int x = 0; x < list.size; x++) {
   System.out.println(list.get[x]);
  }
 }

}

Wednesday, 25 December 2013

JAVA: Reading integer from the keyboard without using Integer.ParseInt() method



import java.io.*;

public class readIntegerWithoutUsingParseint {

    public static void main(String args[]) throws IOException{
         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
         String integer = input.readLine();
         char ar[]= integer.toCharArray();
         int x = (int)'0';
         int sum = 1;
         int number=0;
         
         for(int i=(ar.length-1); i>=0; i--){
         number = number + (sum)*((int)ar[i]-x);
         sum = sum*10;
         }

         System.out.println(number);

    }
}

JAVA: Check the number if its perfect square or not.



import java.util.*;
import java.io.*;
public class perfectSquare {

    public static void main(String args[]) throws NumberFormatException, IOException{
       
        BufferedReader as = new BufferedReader(new InputStreamReader(System.in));
        int x = Integer.parseInt(as.readLine());
        boolean y = true;
    //124System.out.println(x);
        for(int i=0; i<=x/2; i++){
            if(i*i <= x){
            if(x == (i*i)){
                System.out.println(x+ " is a perfect square");
                y = false;
                break;
               
            }
            }
        }
       
        if(y == true){
            System.out.println(x+ " is not a perfect square");   
           
        }
       
    }
   
}

Tuesday, 10 December 2013

JAVA: Removing White Spaces from the String without using inbuilt String functions Like ReplaceAll(), Trim(), Split().



Below is the code.
package Strings;

public class removingwhitespaceWithoutFunctions {
    public static void main(String args[]){
        String x = "  aslam anweranwer      hello hi we hello a  ";
        int y = x.length();
        char c[]= x.toCharArray();
        char h[] ;
        int j=0,t=0;
        for(int i=0;i<y;i++){
            int m = c[i];
        if(m ==32){       // we can compare space also, here i am comparing the asccii code.
            ++t;                // count how many spaces are there.
}
        }
        h = new char[y - t]; // set the length of the array which is going to hold the new string.
        System.out.println(h.length);
        for(int i=0;i<y;i++){
            int m = c[i];
        if(m !=32){
   
        h[j]=c[i];   
        j=j+1;   
        }
        }
       
    System.out.println(h);
        }}

Monday, 9 December 2013

JAVA: MultiThreading Producer and Consumer.



Producer and Consumer in the same program.(Single person doing multitask ,with one hand produce and with another hand  consume)

The below program has two methods one is Producer() and other one is Consumer().
Producer thread is main thread and Consumer thread is child thread.

//Begin Program
package Programs;

public class producerConsumer implements Runnable {
    static   int x;
    static boolean y,produced = false;
    public void Producer() throws InterruptedException{
        synchronized(this){
            Thread t2 = new Thread(this);
            t2.start();
        for(int i=0;i<10 data-blogger-escaped-0="" data-blogger-escaped-1="" data-blogger-escaped-2="" data-blogger-escaped-3="" data-blogger-escaped-4="" data-blogger-escaped-5="" data-blogger-escaped-6="" data-blogger-escaped-7="" data-blogger-escaped-8="" data-blogger-escaped-9="" data-blogger-escaped-args="" data-blogger-escaped-as.producer="" data-blogger-escaped-as="new" data-blogger-escaped-catch="" data-blogger-escaped-child="" data-blogger-escaped-consumed="" data-blogger-escaped-consumer="" data-blogger-escaped-e.printstacktrace="" data-blogger-escaped-e="" data-blogger-escaped-false="" data-blogger-escaped-for="" data-blogger-escaped-hread="" data-blogger-escaped-i="" data-blogger-escaped-int="" data-blogger-escaped-interruptedexception="" data-blogger-escaped-main="" data-blogger-escaped-nd="" data-blogger-escaped-notifyall="" data-blogger-escaped-nterruptedexception="" data-blogger-escaped-onsumed="" data-blogger-escaped-out="" data-blogger-escaped-pre="" data-blogger-escaped-produced="" data-blogger-escaped-producerconsumer="" data-blogger-escaped-program="" data-blogger-escaped-public="" data-blogger-escaped-put:="" data-blogger-escaped-roduced="" data-blogger-escaped-run="" data-blogger-escaped-static="" data-blogger-escaped-synchronized="" data-blogger-escaped-system.out.println="" data-blogger-escaped-this.consumer="" data-blogger-escaped-this.notifyall="" data-blogger-escaped-this.wait="" data-blogger-escaped-this="" data-blogger-escaped-thread="" data-blogger-escaped-throws="" data-blogger-escaped-tring="" data-blogger-escaped-try="" data-blogger-escaped-void="" data-blogger-escaped-while="" data-blogger-escaped-x="" data-blogger-escaped-y="true;">