2017-04-30

Shifting Elements in an Array n time


  • public class ArrayRotation {

  • public static void main(String[] args) {
  • int arr[]={1,2,3};
  • int length=arr.length;
  •                 // change the  rotation/shift value
  • int rotation=2;
  • int toShift=rotation % length;
  • int end=length;
  • int i=0;
  • int arr2[]=new int[length];
  • if(toShift!=0){
  • while(end!=0){
  • if(toShift>=length)
  • toShift=0;
  • arr2[toShift]=arr[i];
  • i++;
  • end--;
  • toShift++;
  • }
  • for(int temp:arr2)
  • System.out.print(temp);
  • }
  • else{
  • for(int temp:arr)
  • System.out.print(temp);
  • }
  • }
  • }



2017-04-29

java .gz file reading and writing

compression and decompression of .gz file


  • import java.io.FileInputStream;
  • import java.io.FileOutputStream;
  • import java.io.InputStream;
  • import java.io.OutputStream;
  • import java.util.zip.GZIPInputStream;
  • import java.util.zip.GZIPOutputStream;

  • public class CompressUncompress {

  • public static void main(String[] args) throws Exception {

  •     //compressing a file to .gz file
  • FileInputStream fis = new FileInputStream("original.txt");
  • FileOutputStream fos = new FileOutputStream("compressed.gz");
  • GZIPOutputStream gos = new GZIPOutputStream(fos);
  • Create(fis, gos);

  • //Uncompressing a file to .gz file
  • FileInputStream fis2 = new FileInputStream("compressed.gz");
  • GZIPInputStream gis = new GZIPInputStream(fis2);
  • FileOutputStream fos2 = new FileOutputStream("uncompressed.txt");
  • Create(gis, fos2);

  • }

  • public static void Create(InputStream is, OutputStream os) throws Exception {
  • int oneByte;
  • while ((oneByte = is.read()) != -1) {
  • os.write(oneByte);
  • }
  • os.close();
  • is.close();
  • }

  • }

2017-04-28

Java program interface variable recursive call problems

For this problems, Variable value is not initialize. its dependents on anther variable.


Problem

  • interface Test{
  • int a=Test3.c+1;
  • }
  • interface Test2{
  • int b=Test.a+1;
  • }
  • interface Test3{
  • int c=Test2.b+1;
  • }
  • public class InhTest{
  • public static void main(String[] args) {
  • System.out.println(Test.a);
  • System.out.println(Test2.b);
  • System.out.println(Test3.c);
  • }
  • }


Same problems with class

  • class Test{
  • static int a=new Test3().c+1;
  • }
  • class Test2{
  • static int b=new Test().a+1;
  • }
  • class Test3{
  • static int c=new Test2().b+1;
  • }
  • public class InhTest{
  • public static void main(String[] args) {
  • System.out.println(Test.a);
  • System.out.println(Test2.b);
  • System.out.println(Test3.c);
  • }
  • }

Output:

     3
     1
     2

2017-04-27

404 not found error redirect to a page

                                          tomcat custom http status 404 Error page configuration

If a requested page and it does not exist. tomcat default page look like this.

Here i am requesting for a page images in my application. But it is not present. Tomcat default error 404 page showing this page.

follow below step to override tomcat default 404 error page



You can put a custom page.

create a the page ' NotFound.html ' in your application home directory.

Add the following in web.xml file. (CATALINA_HOME/conf/web.xml)

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

2017-04-24

How to make pendrive bootable

If you CD drive is not working or your laptop doesn't have cd drive. then follow this step to install OS in system via pen drive. Create a bootable USB flash drive or Install OS for USB drive.


1. Insert your USB (4GB+ preferable)

2. Open Command Prompt As administrator. 

3. When the Command Prompt opens, enter the following command: 

  • DISKPART 

  • LIST DISK  

  • SELECT DISK 1 (Once you enter the LIST DISK command, it will show the disk number of your USB drive. In the below image my USB drive disk no is Disk 1. Replace DISK 1 with your disk number) 

  • CLEAN 

  • CREATE PARTITION PRIMARY 

  • SELECT PARTITION 1 

2017-04-14

angularjs convert variable value as key in json

Convert string value as key in json object


  1. $scope.attVal={"a":"fsdfsdfsd","b":"asdfas","c":"asdfas","d":"","e":"fas","f":"asd","g":"","h":"","i":"","j":"","k":""};

  2. $scope.selectedAttribute="a";
  3. $scope.attrValue="asdda";
  4. $scope.attVal[$scope.selectedAttribute]=$scope.attrValue;

In this example you can see, we are converting a string variable into a json key. and Updating old json by key.

2017-04-12

Write a program to print K-th repeating element from an array


Printing repeating element from an array
  1. import java.util.HashMap;
  2. import java.util.Map;


  3. public class RepeatingElement {
  4. public static void main(String[] args) {
  5. int arr[]={1,1,1,2,3,1,6,5,7,7,8,4,4,12,2,5,8,9,7,0,6,0,6,5,3,4,0,0,1};
  6. Map m=new HashMap();
  7. for(int i=0;i<arr.length;i++){
  8. int total=0;
  9. for(int j=i+1;j<arr.length;j++){
  10. if(!m.containsKey(arr[i]+"")){
  11. if(arr[i]==arr[j]){
  12. total=total+1;
  13. }
  14. }
  15. }
  16. if(total>0){
  17. m.put(arr[i]+"", total+"");
  18. }
  19. }
  20. System.out.println(m);
  21. }
  22. }


2017-04-06

find first repeating letter in a string

print first repeated element



  1. public class Array2 {

  2. public static void main(String[] args) {
  3. /*String s="ffaghglklklh";
  4. Outer:for(int i=0;i<s.length();i++){
  5. Innter: for(int j=i+1;j<s.length();j++){
  6. if(s.charAt(i)==s.charAt(j)){
  7. System.out.println(s.charAt(i));
  8. break Outer;
  9. }
  10. }
  11. }*/
  12. int[] arr={1,2,3,4,5,0,0,1,3};
  13. Outer:for(int i=0;i<arr.length;i++){
  14. Innter: for(int j=i+1;j<arr.length;j++){
  15. if(arr[i]==arr[j]){
  16. System.out.println(arr[i]);
  17. break Outer;
  18. }
  19. }
  20. }
  21. }

  22. }


                    remove duplicate from array

                           Write a program a remove duplicates from array and return a new array. with out using any collection API.


                    1. import java.util.Arrays;

                    2. class MyArray<E> {
                    3. static int length = 0;
                    4. static int capacity = 10;
                    5. static Object arr[];
                    6. MyArray(){
                    7.  arr = new Object[capacity];
                    8. }
                    9. public void add(E value) {
                    10. if (capacity == length) {
                    11. increaseCapacity();
                    12. }
                    13. arr[length] = value;
                    14. length++;
                    15. }

                    16. public static void increaseCapacity() {
                    17. capacity=capacity*2;
                    18. arr=Arrays.copyOf(arr, capacity);
                    19. }

                    Arraylist implementation

                    Similar question:

                    •  dynamic array implementation
                    •  custom arraylist implementation
                    •  own arraylist implementation


                    1. class MyArray<E> {
                    2. static int length = 0;
                    3. static int capacity = 10;
                    4. static Object arr[];
                    5. MyArray(){
                    6.  arr = new Object[capacity];
                    7. }
                    8. public void add(E value) {
                    9. if (capacity == length) {
                    10. increaseCapacity();
                    11. }
                    12. arr[length] = value;
                    13. length++;
                    14. }

                    15. public static void increaseCapacity() {
                    16. capacity=capacity*2;

                    2017-04-04

                    java ssl socket example

                    ssl socket connection with multi threading application. 

                    Server




                    1. package Test2;

                    2. import java.awt.BorderLayout;
                    3. import java.awt.event.ActionEvent;
                    4. import java.awt.event.ActionListener;
                    5. import java.io.IOException;
                    6. import java.io.ObjectInputStream;
                    7. import java.io.ObjectOutputStream;
                    8. import java.net.InetAddress;
                    9. import java.security.cert.Certificate;
                    10. import java.security.cert.X509Certificate;
                    11. import java.util.ArrayList;

                    12. import javax.net.ssl.SSLServerSocket;
                    13. import javax.net.ssl.SSLServerSocketFactory;
                    14. import javax.net.ssl.SSLSession;
                    15. import javax.net.ssl.SSLSocket;
                    16. import javax.swing.JFrame;
                    17. import javax.swing.JOptionPane;
                    18. import javax.swing.JScrollPane;
                    19. import javax.swing.JTextArea;
                    20. import javax.swing.JTextField;

                    2017-04-01

                    How to install software without administrator rights

                    You can install software without admin rights follow the step

                    You are working in a organization and If you don't have admin rights don't worry we have a trick.

                    Step



                    Right Click on executable file , which you want to install.
                    and select Run Elevated 



                    Application will install in your system with out admin rights.



                    java 10 features

                    java jdk 1.10 release features with examples

                    • Introducing objects without identity (value types)
                    • 64-bit addressable arrays to support large data sets
                    • JSR 354: Money and Currency API



                    java 9 features

                    java jdk 1.9 release features with examples


                    Java 9 should include better support for multi-gigabyte heaps, better native code integration, and a self-tuning JVM.

                      Key Changes in JDK 9

                      These changes affect more than one technology area.

                      Java Platform Module System   
                      Introduces a new kind of Java programing component, the module, which is a named, self-describing collection of code and data. This module system:
                      Introduces a new optional phase, link time, which is in-between compile time and run time, during which a set of modules can be assembled and optimized into a custom runtime image; see the jlink tool in Java Platform, Standard Edition Tools Reference.
                      Adds options to the tools javac, jlink, and java where you can specify module paths, which locate definitions of modules.
                      Introduces the modular JAR file, which is a JAR file with a module-info.class file in its root directory.
                      Introduces the JMOD format, which is a packaging format similar to JAR except it can include native code and configuration files; see the jmod tool.
                      The JDK itself has been divided into a set of modules. This change:

                      Enables you to combine the JDK's modules into a variety of configurations, including: