Sunday, January 1, 2012

Program to demonstrate usage of allocating memory dynamically to second dimension of array, by displaying following pattern in Java


             X
          XXX
        XXXXX
      XXXXXXX
   XXXXXXXXX
XXXXXXXXXXX
   XXXXXXXXX
     XXXXXXX
       XXXXX
        XXX
           X
Example

import java.util.*;
class A404307
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,j,k,i;
System.out.println("Enter The Value Of Number of Pattern ::");
a=sc.nextInt();
for(i=1;i<=a;i++)
{
for(j=a-i;j>0;j--)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print(" *");
}
System.out.println();
}
for(i=a;i>=1;i--)
{
for(j=a-i;j>0;j--)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print(" *");
}
System.out.println();
}
}
}

Instanceof operator in Java

Example



class A
{
int i,j;
}
class B
{
int i,j;
}
class C extends A
{
int k;
}
class D extends A
{
int k;
}
class A404306
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
D d=new D();
if (a instanceof A)
System.out.println(" a is instance of A");
if (b instanceof B)
System.out.println(" b is instance of B");
if (c instanceof C)
System.out.println(" c is instance of C");
if (c instanceof A)
System.out.println(" c can be cast of A");
if (a instanceof C)
System.out.println(" a can be case of C");
System.out.println();
A ob;
ob=d;
System.out.println("ob now refers to d");
if(ob instanceof D)
System.out.println("ob is instance of D");
ob=c;
System.out.println("ob now refers to c");
if(ob instanceof D)
System.out.println("ob can be cast to D");
else
System.out.println("ob cannot be cast to D");

if(ob instanceof A)
System.out.println("ob can be cast to A");
System.out.println();

if(a instanceof Object)
System.out.println("a may be cast to Object");
if(b instanceof Object)
System.out.println("b may be cast to Object");
if(c instanceof Object)
System.out.println("c may be cast to Object");
if(d instanceof Object)
System.out.println("d may be cast to Object");
}
}

Usage of 'this' keyword in preventing instance variable hiding in Java

Example



import java.io.*;
class SuperClass
{
int a;
SuperClass()
{}
v
oid Display1()

{
if (a%2==0)
{
System.out.println("first value is divisible by 2");
}
else
{
System.out.println("first value is not divisible by 2");
}
}
}
class SubClass extends SuperClass
{
int c;
SubClass(int d,int e)
{
this.a=d;
c=e;
}
void Display()
{
if (this.a%c==0)
{
System.out.println(this.a+": is divisible by :"+c);
}
else
{
System.out.println(this.a+":is not divisible by:"+c);
}
}
}
public class A404305
{
public static void main(String args[])
throws IOException
{
int f,g;
BufferedReader br;
br=new BufferedReader(new InputStreamReader(System.in));
f=Integer.parseInt(br.readLine());
g=Integer.parseInt(br.readLine());
SubClass s=new SubClass(f,g);
s.Display();
SuperClass s1=new SuperClass();
s1.Display1();


}
}

Usage of static block in Java

Example



import java.io.*;
class Add
{
    
public static int Add1(int a,int b)

{
return (a+b);
}

}


public class A404304 
{
public static void main(String args[])
throws IOException
{
int c,d,e;
BufferedReader br;
br=new BufferedReader(new InputStreamReader(System.in));
c=Integer.parseInt(br.readLine());
d=Integer.parseInt(br.readLine());
e=Add.Add1(c,d);
System.out.println(e);
}
}

Super keyword. in Java

Example



import java.io.*;
class SuperClass
{
int a,b;
SuperClass(int c,int d)
{
a=c;
b=d;
}
void Display()
{
if (a>b)
{
System.out.println(a+"::Greater than::"+b);
}
else
{
System.out.println(b+"::Greater than::"+a);
}
}
}
class SubClass extends SuperClass
{
int e;
SubClass(int f,int g,int h)
{
super(f,g);
e=h;
}
void Display()
{
System.out.println("For 1st two value");
super.Display();
System.out.println("--------------------------------");
System.out.println("For three value");
if (e>super.a && e>super.b)
{
System.out.println(e+"::Greater than::"+super.a+ " "+super.b);
}
else if(super.a>e && super.a>super.b)
{
System.out.println(super.a+"::Greater than::"+e+" "+super.b);
}
else
{
System.out.println(super.b+"::Greater than::"+e+" "+super.a);
}
}
}
public class A404303
{
public static void main(String args[])
throws IOException
{
int p,q,r;
BufferedReader br;
br=new BufferedReader(new InputStreamReader(System.in));
p=Integer.parseInt(br.readLine());
q=Integer.parseInt(br.readLine());
r=Integer.parseInt(br.readLine());
SubClass s=new SubClass(p,q,r);
s.Display();
}

Demostrate the use of try..catch in Java

Example



import java.io.*;
public class A15
{
public static void main(String args[])

{

try
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
System.out.println("Subtraction of two number:"+(a-b));

}
catch(Exception e)
{
System.out.println("Enter number to Divide");
}
}
}

Create a custom exception class in Java

Example



class MyException extends Exception
{
private int detail;
MyException(int a)
{
detail=a;
}
public String toString()
{
return "MyException["+detail+"]";
}
}
class A14


{
static void compute(int a)throws MyException
{
System.out.println("called compute("+a+")");
if(a>10)
throw new MyException(a);
System.out.println("normal exit");
}
public static void main(String args[])
{
try
{
compute(1);
compute(20);
}
catch(MyException e)
{
System.out.println("caught "+e);
}
}
}