Saturday, 1 February 2020

Array Program 1 : Find Evenly Repeated number from an Array

Array Program 1: Find the Evenly Repeated number from an Array


Input: int[] a = {1,1,5,4,8,1,5,8,3,9,15,15,16,2,8,7,9,1};

Output : [1, 5, 9, 15]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package JavaPractice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArraysEvenOccurance {

 public static void main(String arg[]){
     int[] input = {1, 1, 5, 4, 8, 1, 5, 8, 3, 9, 15, 15, 16, 2, 8, 7, 9, 1};
     int[] output = new int[input.length];
        // sort input array
        Arrays.sort(input);
        
        int counter = 1, k = 0, j=0;
        for (int i = 1; i < input.length; i++) {
            if (input[k] == input[i]) {
                // check for same number
                counter++;
            } else {
                // check even number of counter
                if (counter % 2 == 0) {
                    output[j] = (input[k]);
                    j++;
                }
                counter = 1;
                k = i;
            }
        }
        // Removing Extra appended element (sub array)
        output = Arrays.copyOfRange(output, 0, j);
        // Print Result array
        System.out.println(Arrays.toString(output));
    }
}

Sunday, 26 January 2020

What is Synchronization in Java

What is Synchronization in Java

Synchronization in java is the capability to control the access of multiple threads to any shared resource.


Example :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package JavaPractice;

import java.util.Date;

public class TestSynchronization {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Table obj = new Table();
  MyThread1 th1 = new MyThread1(obj, true); // true for Synchronization
  MyThread2 th2 = new MyThread2(obj, true);
  th1.start();
  th2.start();
 }

}

class Table {
 // method not Synchronization
 void printTable(int n) {
  for (int i = 1; i <= 5; i++) {
   System.out.println(n * i);
   try {
    Thread.sleep(400);
   } catch (Exception e) {
    System.out.println(e);
   }
  }

 }

 // method is Synchronization
 synchronized void printTableSynchronization(int n) {
  for (int i = 1; i <= 5; i++) {
   System.out.println(n * i);
   try {
    Thread.sleep(400);
   } catch (Exception e) {
    System.out.println(e);
   }
  }

 }
}

class MyThread1 extends Thread {
 Table t;
 boolean isCallSynchronization;

 MyThread1(Table t, boolean isCallSynchronization) {
  this.t = t;
  this.isCallSynchronization = isCallSynchronization;
 }

 public void run() {
  if (this.isCallSynchronization) {
   System.out.println(new Date());
   t.printTableSynchronization(5);
   System.out.println(new Date());
  } else {
   System.out.println(new Date());
   t.printTable(5);
   System.out.println(new Date());
  }
 }
}

class MyThread2 extends Thread {
 Table t;
 boolean isCallSynchronization;

 MyThread2(Table t, boolean isCallSynchronization) {
  this.t = t;
  this.isCallSynchronization = isCallSynchronization;
 }

 public void run() {
  if (this.isCallSynchronization) {
   System.out.println(new Date());
   t.printTableSynchronization(100);
   System.out.println(new Date());
  } else {
   System.out.println(new Date());
   t.printTable(100);
   System.out.println(new Date());
  }
 }
}

How to find Second Highest element in Array in Java

How to find Second Highest element in Array in Java
  • Three ways to find Second largest element in Array.
    1. using single for loop
    2. using Arrays.
    3. using Collections.
  • Example :
  •  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    package JavaPractice;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class SecondLargestNumber {
     /**
      * Using Single For loop
      */
     public static void secondLargestNumberUsingSingleForLoop(int arr[],
       int arr_size) {
      int first, second;
      if (arr_size < 2) {
       System.out.print(" Invalid Input ");
       return;
      }
      first = second = Integer.MIN_VALUE;
      for (int i = 0; i < arr_size; i++) {
       if (arr[i] > first) {
        second = first;
        first = arr[i];
       } else if (arr[i] > second && arr[i] != first) {
        second = arr[i];
       }
      }
    
      if (second == Integer.MIN_VALUE)
       System.out.print("There is no second largest" + " element\n");
      else
       System.out.print("The second largest element" + " is " + second + "\n");
     }
    
     /**
      * Using Arrays
      */
     public static void secondLargestNumberUsingArrays(int arr[], int arr_size) {
      Arrays.sort(arr);
      System.out.print("The second largest element" + " is "
        + arr[arr_size - 2] + "\n");
     }
    
     /**
      * Using Collections and Stream
      */
     public static void secondLargestNumberUsingCollections(int arr[],
       int arr_size) {
      List<Integer> list = Arrays.stream(arr).boxed()
        .collect(Collectors.toList());
      Collections.sort(list);
      System.out.print("The second largest element" + " is "
        + list.get(arr_size - 2)+ "\n");
     }
     /**
      * Main Function
      */
     public static void main(String[] args) {
      int arr[] = { 12, 35, 1, 10, 34, 1 };
      int n = arr.length;
      secondLargestNumberUsingSingleForLoop(arr, n);
      secondLargestNumberUsingArrays(arr, n);
      secondLargestNumberUsingCollections(arr, n);
    
     }
    
    }
    

Monday, 2 October 2017

Short Intro of Java Web Application


  1. Web Application
    • Collection of web pages and resources.
    • Web Pages can be static (HTML) or dynamic (processed at server)


       2. HTTP Protocol
    • Connection-less, state less
    • request response model

            2.1 HTTP Request

    • From Browser to Web Server using URL.
    • http://servername:port/application/webpage

            2.2 HTTP Response : 

    • From Web Server to Browser

            2.3 HTTP Request Methods
                GET :
                        - data sent via URL (query-string)
                        - not secure
                        - send limited data (browser URL address is limited up to 2K character)
                        - faster
                POST :
                        - data is sent via request body
                        - secure
                        - no data limit
                        - slower
                HEAD :
                         - response only contains header not body
                PUT :
                         - for upload/put a file on web server
                 DELETE :
                         - for delete a file on web server
                 TRACE :
                         - for tracing http req/res data
                  OPTION :
                         - to know which methods supported on server

Tuesday, 19 January 2016

How to write Simple HELLO Program in Java?

This is Simple Java Program


PROGRAM:

        1. class Simple{  
        2.     public static void main(String args[]){  
        3.      System.out.println("Hello Java");  
        4.     }  
        5. }  


OUTPUT:

Hello Java

Process:

Use NetbansIDE or EclipseIDE and run Program.

Another is

1. Open Notepad Write code
2. Save file as hello.java
3. open CMD(Command Prompt) 
4. type cd\ and click enter
5. type javac hello.java and click enter
6. type java hello and click enter
7. you will see the output on CMD

 

Thank you




 





Sunday, 17 January 2016

How to Install Java?

Java is a general-purpose computer programming language

  • JDK 1.0 (January 21, 1996)
  • JDK 1.1 (February 19, 1997)
  • J2SE 1.2 (December 8, 1998)
  • J2SE 1.3 (May 8, 2000)
  • J2SE 1.4 (February 6, 2002)
  • J2SE 5.0 (September 30, 2004)
  • Java SE 6 (December 11, 2006)
  • Java SE 7 (July 28, 2011)
  • Java SE 8 (March 18, 2014)

this version you can install from click here!



you can also download essential software

1) Eclipse IDE

2) MySql (Back-end)