Sunday 26 August 2012

Bubble Sorting

package com.vikash.core;

public class BubbleTest1 {
    public static void main(String args[]) {
        int bubbles[] = { 0,5,4,3,78,6,76};
        System.out.println("Beffore Sorting:");
        for(int i:bubbles)
        {
            System.out.print(" "+i+" ");
        }
        System.out.println();
        int t = 0;
        while(t!=bubbles.length){
            for (int j = 0; j < bubbles.length - 1; j++) {
                if (bubbles[j] > bubbles[j + 1]) {
                    t = bubbles[j];
                    bubbles[j] = bubbles[j + 1];
                    bubbles[j + 1] = t;
                }
            }
            t+=1;
        }
        System.out.println("After Sorting");
   
        for (int i : bubbles)
            System.out.print(" " + i + " ");
    }

}

Inheritance


class A
{
  public void show()
{
  System.out.println("Super Class");
}
class B Extends A
{
public void show()
{
System.out.println("Sub Class");
}
}
class TestInheritance()
{
public static void main(String args[])
{
A a=new A();
B b=new B();
a.show();
b.show();
}
}

Note: When you change the access specifier of the overridden method in the subclass then it gives an compile time error i.e. u can't reduce the visibility.

Monday 13 August 2012

String Handling

1. Reverse a given String

package com.vikash.core;

public class ReverseStringTest {
    public static void main(String args[]) {
        String str = "vikash";
        StringBuffer sb = new StringBuffer();
        for (int i = str.length() - 1; i >= 0; i--)
            sb.append(str.charAt(i));
        System.out.println(sb);
    }
}