Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to sort array elements using Insertion Sort algorithm in Java ?

Program to demonstrate how to sort array elements using Insertion Sort algorithm in Java

public class InsertionSort {

   public static void main(String[] args) {

      int[] a = {10,3,4,7,1,9,6};
      int nElems = a.length;

      System.out.println("Before Insertion Sort: ");
      for(int k = 0 ; k < a.length; k++)
  System.out.print(a[k] + " ");

      int in, out;
      for(out=1; out < nElems; out++) 
  {
     int temp = a[out]; 
     in = out; 
     while(in > 0 && a[in-1] >= temp) 
     {
  a[in] = a[in-1]; 
  --in; 
     }
     a[in] = temp;

  }

      System.out.println("");
      System.out.println("After Insertion Sort: ");

      for(int i =0; i < a.length; i++){

   System.out.print(a[i] + " ");
      }
   }

}
Output of the program : 

 

What is a Web Application in Java programming language?

A Web Application also sometimes referred as Web app, is an application that is often accessed using a Web browser over a network such as Internet etc. Many of the World's best web applications are written in PERL and PHP, Java remains most widely used programming language for writing Web applications. When ever we are using a Web based enterprise application Java is most reliable programming language that comes to our mind.

Java EE 5/6/7 provides several useful components such as Java Server Pages, Java Server Faces, Servlets, client-side applets, Java Database Connectivity (JDBC), Enterprise JavaBeans and other web service technologies for writing scalable and enterprise Web applications.

Usually a Web application can be defined as a hierarchy of directories and files in a standard and logical layout. There are usually two ways into which the hierarchies can be accessed:

  1. UnPacked Form : In the unpacked form way, a web application is organized in such a way that each file and directory exists in file system separately. Generally it is used and accessed during application development.
  2. Packed Form : In the packed form way, a web application is organized in such a way that all the directories and sub-directories are zipped together in a packed form known as a Web Archive or WAR file. It is usually done when we are ready to distribute application to be installed.

How to sort array elements using Selection Sort algorithm in Java ?

Program to demonstrate how to sort array elements using Selection Sort algorithm in Java

public class SelectionSort {
   
   public static void main(String[] args) {
  
 int[] a = {10,2,3,4,7,8,9,0};
  
 System.out.println("Before Selection Sort: ");
 for(int k = 0 ; k < a.length; k++)
            System.out.print(a[k] + " ");
  
 int min =0 , out =0 , in = 0;
  
 for(out = 0; out < a.length; out++){
            min = out;
   
            for(in = out+1; in < a.length; in++ ){
  if(a[min] < a[in]){
                   min = in;
  }
     }
     int temp = 0;
     temp = a[min];
     a[min] = a[out];
     a[out] = temp;
 }
  
 System.out.println("\nAfter Selection Sort: ");
        for(int k = a.length - 1; k >= 0; k--)
     System.out.print(a[k] + " ");
 }

}




Output of the program :


Java Polymorphism : Example to show Upcasting in Java

Program to demonstrate how upcasting works in Java through Polymorphism.

class Shape {
   public void draw(Shape s){
 System.out.println("Shape drawn ... " + s);
   }
 
   @Override
   public String toString() {
        return "Shape drawn with Shape Object";
   }
}

class Triangle extends Shape{
   public void draw(Shape s){
 System.out.println("Shape drawn ... " + s);
   }
 
   @Override
   public String toString() {
 return "Shape drawn with Triangle Object";
   }
}

public class UpcastingTest {
 
   public static void main(String[] args) {
  
 Shape s = new Shape();
 s.draw(s);
  
 Triangle t = new Triangle();
 t.draw(t);

 Shape st = new Triangle();
 st.draw(st);
  
 /*Triangle ts = (Triangle) new Shape();
 ts.draw(ts);*/ // Runtime error Shape cannot
                 // be cast to Triangle
   }

}




Output of the program : 


Protected keyword usage in Java Inheritance

Program to demonstrate protected keyword usage in Java Inheritance.

package com.hubberspot.code;

class Shape{
   private int x;
   private int y;
 
   protected int area(int w , int h){ // protected
      this.x = w;                    // available to Rectangle
      this.y = h;
      return x*y;
   }
}

public class Rectangle extends Shape{
  
   public static void main(String[] args) {
  
      Rectangle rect = new Rectangle();
      int area = rect.area(10, 20); // area method declared protected
      System.out.println("The Area of Rectangle is " + area);
   }

}




Output of the program :



Inheritance in Java : Constructor with arguments

Program to demonstrate Constructor with arguments in Java Inheritance

package com.hubberspot.code;

class Drawing {
 
  public Drawing(int d){
     System.out.println("Drawing Constructor called " +
   "with argument as "+ d);
  }  
}

class Shape extends Drawing{
 
  public Shape(int i){
     super(11); // Compile time error if skipped
     System.out.println("Shape Constructor called " +
   "with argument as "+ i);
  } 
}

public class Circle extends Shape{
 
  public Circle(){
     super(10); // Compile time error if skipped
     System.out.println("Circle Constructor called");
  }
 
  public static void main(String[] args) {
  
     Circle circle = new Circle();
 
  }

}




Output of the program :




Working example of Constructor behavior in Java Inheritance

Program to demonstrate working example of Constructor behavior in Java Inheritance

package com.hubberspot.code;

class Drawing {
 
   public Drawing(){
 System.out.println("Drawing Constructor called");
   }  
}

class Shape extends Drawing{
 
   public Shape(){
 System.out.println("Shape Constructor called");
   } 
}

public class Circle extends Shape{
 
   public Circle(){
 System.out.println("Circle Constructor called");
   }
 
   public static void main(String[] args) {
  
 Circle circle = new Circle();
 
   }

}


Output of the program :



How Inheritance works in Java through extends keyword ?

Program to demonstrate how Inheritance works in Java through extends keyword.

 class Shape {
    private String s = " Shape: ";
 
    public void draw(){
 name(" draw() ");
    }
    public void redraw(){
 name(" redraw() ");
 }
    public void area(){
 name(" area() ");
    }
    public void circumference(){
 name(" circumference() ");
    }
    public void name(String a){
 s+=a;
    }
    public String toString() {
 return s;
    }
 
    public static void main(String[] args) {
  
    Shape s = new Shape();
    s.draw();
    s.redraw();
    s.area();
    s.circumference();
    System.out.println(s);

    }

}

 
public class Circle extends Shape{

    public void area(){
 name(" Circle.area() ");
    }
    public void circumference(){
 name(" Circle.circumference() ");
    }
    
    public static void main(String[] args) {
  
 Circle c = new Circle();
    c.draw();
    c.redraw();
    c.area();
    c.circumference();

    System.out.println(c);
    Shape.main(args);


 }

}




Output of the program :



How Composition (Has-A) relationship works in Java

Program to demonstrate how Composition (Has-A) relationship works in Java

class SimCard {
 
   private String cardNumber;
 
   public SimCard(){
  
 System.out.println("SimCard Object Constructed");
 cardNumber = "New SimCard Constructed";
  
   }
 
   @Override
   public String toString() {
 
 return cardNumber;
 
   } 
}

public class Mobile {
 
   private SimCard sim = new SimCard();
   private String mobile = "Nokia";
   private int model = 1100;
 
   @Override
   public String toString() {
 
 return sim + " for " + mobile + " " + model;
 
   }
 
   public static void main(String[] args) {

 Mobile mob = new Mobile();
 System.out.println(mob);

   }

}


Output of the program : 

How Literals provide additional information to compiler in Java ?

Program to demonstrate Literal usage in Java


public class LiteralsInfo {
 
  public static void main(String[] args) {
  
     byte b1 = 127;
     byte b2 = 128; // Compilation Error
  
     short s1 = 32767;
     short s2 = 32768; // Compilation Error
  
     int i1 = 2147483647;
     int i2 = 2147483648; // Compilation Error
     int i3 = 0x1f;  // For Hexadecimal numbers
     int i4 = 0x1F;  // For Hexadecimal numbers
     int i5 = 0176;
  
     long n1 = 300L; // long suffix
     long n2 = 300l; // long suffix 
     long n3 = 300;
  
     float f1 = 3;
     float f2 = 3F; // float suffix
     float f3 = 3f; // float suffix
  
     double d1 = 1d; // double suffix
     double d2 = 1D; // double suffix
  
     char c = 'a';

 }
}


Short Circuiting in Java Programming Language

Program to demonstrate Short Circuiting in Java

public class ShortCircuiting {
   
  public static boolean one(int i){
  
     System.out.println("Evaluated : one");
     return i < 2;
  
  }
 
  public static boolean two(int i){
  
     System.out.println("Evaluated : two");
     return i < 2;
  
  }
 
  public static boolean three(int i){
  
    System.out.println("Evaluated : three");
    return i < 4;
  
  }
 
  public static void main(String[] args) {
  
    boolean b = ( one(1) && two(2) && three(3) );
  
    System.out.println("Evaluation result :" + b);

  }

}


Output of the program :



How == and equals() affects Object equivalence in Java ?

Program to demonstrate how Object Equivalence works in Java

class MyInteger{
 
  public int i;
 
  MyInteger(int i){
    this.i =i;
  }
 
}

public class ObjectEquivalenceTest {
 
  public static void main(String[] args) {
  
  Integer one = new Integer(10);
  Integer two = new Integer(10);
  
  System.out.println("one == two : "+ (one == two));
  System.out.println("one != two : "+ (one != two));
  System.out.println("one.equals(two) : "+ (one.equals(two)));
  
  System.out.println();
  
  MyInteger mone = new MyInteger(10);
  MyInteger mtwo = new MyInteger(10);
        
  System.out.println("mone == mtwo : "+ (mone == mtwo));
  System.out.println("mone != mtwo : "+ (mone != mtwo));
  System.out.println("mone.equals(mtwo) : "+ (mone.equals(mtwo))); 

  }

}


Output of the program :



How Auto Increment/Decrement operator works in Java ?

Program to demonstrate how Auto Increment/Decrement operator works in Java

public class IncrementDecrement {
 
   public static void main(String[] args) {
  
      int x = 1;
  
      System.out.println("The value of x: " + x);
      System.out.println("The value of ++x: " + ++x);
      System.out.println("The value of x++: " + x++);
      System.out.println("The value of x now: " + x);
      System.out.println("The value of --x: " + --x);
      System.out.println("The value of x--: " + x--);
      System.out.println("The value of x now: " + x);
  
   }

}

Output of the program :



How Aliasing affects Java's reference variable ?.

Program to demonstrate how aliasing affects Java's reference variable

class Triangle {
 
  public int width;
 
}

public class AliasingEffect {
 
  public static void triangleAlias(Triangle triangle){
  
    triangle.width = 20;
  
  }
 
  public static void main(String[] args) {
  
    Triangle triangleOne = new Triangle();
    triangleOne.width = 10;
  
    System.out.println("1: triangleOne's width = " + triangleOne.width);
  
    triangleAlias(triangleOne);
  
    System.out.println("2: triangleOne's width = " + triangleOne.width);

  }

}




Output of the program :



How assignment operator works with reference variables in Java ?

Program to demonstrate how assignment operator works with reference variables in Java

class Triangle {
 
  public int width;
 
}

public class AssignmentReferenceTest {
 
  public static void main(String[] args) {
  
     Triangle triangleOne = new Triangle();
     Triangle triangleTwo = new Triangle();
  
     triangleOne.width = 10;
     triangleTwo.width = 20;
  
     System.out.println("1: triangleOne's width = " + triangleOne.width);
     System.out.println("1: triangleTwo's width = " + triangleTwo.width);
  
     triangleOne= triangleTwo;
  
     System.out.println("2: triangleOne's width = " + triangleOne.width);
     System.out.println("2: triangleTwo's width = " + triangleTwo.width);
  
     triangleOne.width = 30;
      
     System.out.println("3: triangleOne's width = " + triangleOne.width);
     System.out.println("3: triangleTwo's width = " + triangleTwo.width);
 
   }

}


Output of the program :




How Operator Precedence works in Java ?

Program to demonstrate how Operator Precedence works in Java

public class PrecedenceTest {
 
  public static void main(String[] args) {
  
    int a = 1, b = 2, c = 3;
  
    int x = a + b - 2/2 + c;
  
    int y = a + (b - 2)/(2 + c);
  
    int z = (a + b) - 2/(2 + c);
  
    System.out.println("x = " + x);
    System.out.println("y = " + y);
    System.out.println("z = " + z);

  }

}


Output of the program :



How to use Javadoc Comments in Java program for Documentation ?

Program to use Javadoc Comments in Java for Documentation

/**
 * @see java.util.Date
 * */
import java.util.Date;


/** A simple program demonstrating
 * Java Documentation by placing following 
 * annotations :   
 * @author Jonty
 * @author www.hubberspot.com
 * @version 3.0 
 * */
public class TodaysDate {

/**Entry to main method that prints 
 * todays date. 
 * @param args
 * @exception throws no exceptions
 */
   public static void main(String[] args) {
      System.out.println("Hello, it’s: ");
      System.out.println(new Date());

   }

}




Usage command :
javadoc TodaysDate.java

HTML Generated as:




 
© 2021 Learn Java by Examples Template by Hubberspot