Sunday 9 April 2017

Java program which shows the use of finally block for handling exception

class ExceptionDemo
{
 public static void main(String args[])
 {
   try
   {
    int a,b;
    a=5;
    b=a/0;
   }
  catch(ArithmeticException e)
  {
   System.out.println("Divide by Zero\n");
  }
  System.out.println("...Executed catch statement");
 }
}




class ExceptionThrow
{
 public static void main(String args[])
 {
  try
  {
   throw new ArithmeticException("Testing Throw");
  }
  catch(ArithmeticException e)
  {
   System.out.println("Caught exception: "+e);
  }
 }
}






class ExceptionProg
{
  static void fun(int a[]) throws ArrayIndexOutOfBoundsException
  {
    int c;
    try
    {
      c=a[0]/a[2];
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
     System.out.println("Caught exception: "+e);
    }
}
public static void main(String args[])
{
  int a[]={10,5};
  fun(a);
}
}



/*
This is a java program which shows the use of finally block for handling exception
*/
class finallyDemo
{
 public static void main(String args[])
 {
  int a=10,b=-1;
  try
  {
    b=a/0;
  }
  catch(ArithmeticException e)
  {
   System.out.println("In catch block: "+e);
  }
  finally
  {
   if(b!=-1)
      System.out.println("Finally block executes without occurrence of exception");
   else
     System.out.println("Finally block executes on occurrence of exception"); 
  }
 }
}


Saturday 11 March 2017

Multiple inheritance in java with its example

Multiple inheritance is the ability of a single class to inherit from multiple classes. Java does not have this capability.

The designers of Java considered multiple inheritance to be too complex, and not in line with the goal of keeping Java simple.

Program:

class Base
{
  int val;

  public void set_val(int i)
  {
    val=i;
  }
}
class A extends Base implements interface1
{
 
  public void show_val()
  {
   System.out.println("The value of a= "+val);
  }
}
class B extends Base implements interface1
{
  public void show_val()
  {
    System.out.println("The value of b= "+val*5);
  }
}

class MultipleInherit
{
  public static void main(String args[])
  {
    interface1 obj_A=new A();
    interface1 obj_B=new B();     
    obj_A.set_val(10);
    obj_B.set_val(20);
    obj_A.show_val();
    obj_B.show_val();
   }
}

Type Cast Program in java

By the end of this tutorial "Java Data Type Casting Type Conversion", you will be comfortable with converting one data type to another either implicitly or explicitly.

Program


  1. public class TypeCastProg
  2. {
  3.   public static void main(String[] args)
  4.   {
  5.      double x;
  6.      int y;
  7.      x=25.50;
  8.      System.out.println("Value of [double] x: "+x);
  9.      System.out.println("Conversion of double to integer");
  10.      y=(int)x;
  11.      System.out.println("Value of [integer] y: "+y);
  12.      int m;
  13.      double n;
  14.      m=10;
  15.      n=(double)m;
  16.      System.out.println("Value of [integer] m: "+m);
  17.      System.out.println("Conversion of integer to double");
  18.      System.out.println("Value of [double] n: "+n);

  19.   }
  20. }


Shows the use of class in the program in java

This is a Java program which shows the use of class in the program

Program:

  1. class Rectangle
  2. {
  3.  int height;
  4.  int width;
  5.  void area()
  6.  {
  7.   int result=height*width;
  8.   System.out.println(“The area is ”+result); 
  9.  }
  10. }
  11. class classDemo
  12. {
  13.  public static void main(String args[])
  14.  {
  15.   Rectangle obj=new Rectangle();
  16.   obj.height=10;//setting the attribute values 
  17.   obj.width=20;//from outside of class
  18.   obj.area();//using object method of class is called
  19.  }
  20. }


Variables scope program in java


Scope of a variable is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can determined at compiler time and independent of function call stack.

Program:

public class VarScopeProg
{
 public static void main(String[] args)
 {
   int A=10;
   {
     int B=20;
     System.out.println("Block A: "+A);
     System.out.println("Block B: "+B);
   }//scope of B=20 ends here
   {
   int B=30;//scope of B=30 starts here
   System.out.println("Block A: "+A);
   System.out.println("Block B: "+B);
   }//scope of B=30 ends here
   int B=40;//scope of B=40 starts here
   {
     System.out.println("Block A: "+A);
     System.out.println("Block B: "+B);

   }
 }//scope of all the variables end here
}