Saturday 1 September 2012

How to performed inheritance in vb.net.Explain use of mybase , me and myclass in vb.net


Module Module1
    Class Button
        Public Sub New()
            Console.WriteLine("in base calss")
        End Sub
        Public Overridable Sub Paint()
            ' Paint button here. '        
            Console.WriteLine("in base calss's paint")
        End Sub
    End Class

    Class B1
        Inherits Button
        Public p As Integer = 10
        Public Sub New()
            Console.WriteLine("in base calss b1")
        End Sub
        Public Overrides Sub Paint()
            ' First, paint the button. '
            'MyBase.Paint()
            ' Now, paint the banner. … '
            Console.WriteLine("in base calss 2's paint")
        End Sub

    End Class
    Class B2
        Inherits B1
        Dim p As Integer = 15
        Public Sub New()
            Console.WriteLine("in base calss last")
        End Sub
        Public Overrides Sub Paint()
            Dim p As Integer = 5
            Console.WriteLine("use of me " + Me.p.ToString) 'also mybase can be used but mostly mybase is used for static
            Console.WriteLine("use of mybase" + MyBase.p.ToString)
            Console.WriteLine("use of local" + p.ToString)

            ' First, paint the button. '
            MyBase.Paint()
            Console.WriteLine("in child calss's paint")
            ' Now, paint the banner. … '
        End Sub

    End Class
    Public Sub main()
        Dim b As New B2()
        Dim str As String = "hello"
        'Console.Write(Mid(str, 2))
        'Console.Write(substring(str, 2))

        b.Paint()
        Console.Read()

    End Sub

End Module


CONSOLE OUTPUT IS:--

Wednesday 29 August 2012

Program for calculating total elapsed time using the concept of multithreading and System.currentTimeMillis() method.


import  java.io.*;
class Child extends Thread
{
Child(String str)
{
super(str);
start();
}
public void run()
{

try
{
for(int n=1;n<=5;n++)
{
System.out.println("Child thread: "+n);
Thread.sleep(1000);
}

}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}

}
}

class prog
{
public static void main(String a[]) throws Exception
{
long timestart = System.currentTimeMillis();
new Child("child");

Thread t = Thread.currentThread();
//System.out.println("current thread"+t);
t.setName("my thread");

try
{

for(int n=1;n<=5;n++)
{
System.out.println("main thread: "+n);
Thread.sleep(1000);
}
}
catch(Exception e)
{
System.out.println("not  valid");
}
long timelast = System.currentTimeMillis();
float result =(timelast-timestart)/1000;
System.out.println("total time elapsed in seconds :  "+result);

}
}


main thread: 1
Child thread: 1
main thread: 2
Child thread: 2
main thread: 3
Child thread: 3
Child thread: 4
main thread: 4
main thread: 5
Child thread: 5
total time elapsed in seconds :  5.0

Sunday 19 August 2012

insertion and deletion of node (value) in singly linked list in java

import java.io.*;
class Node
{
    private Node next;
    private int data;
    Node(int data)
    {
        this.data=data; 
    } 
 
    public    Node getNext()
    {
        return next;
    }

  public void setNext(Node next)
    {
        this.next=next;
    }

    public    int getData()
    {
        return data;
    }
    public void setData(int data)
    {
        this.data=data;

    }
}



class LinkList
{
    Node head=null;
 
    public void addValue(int data,int pdata)
    {
        Node newNode=new Node(data);
        if(head == null)
        {
            head=newNode;
        }
        else if(head.getData() == pdata && head.getNext() == null)
        {
            head.setNext(newNode);
     
        }
        else
        {
            Node trav=head;
            //Node prev=null;
            while(trav != null && trav.getData() != pdata)
            {
                //prev=trav;
                trav=trav.getNext();
             
            }
         
            if(trav == null)
            {
                System.out.println("Give Value not found");
            }
            else
            { 
                Node temp= trav.getNext();
                newNode.setNext(temp);
                trav.setNext(newNode);
            }
         
        }
    }
    public void deleteValue(int data)
    {
     
        if(head==null)
        {
            System.out.println("LInk LIst is empty");
        }
        else if(head.getData() == data)
        {
            head=head.getNext();
     
        }
        else
        {
            Node trav=head;
            Node prev=null;
            while(trav != null &&  trav.getData() != data)
            {
                prev=trav;
                trav=trav.getNext();
             
            }
         
            if(trav == null)
                System.out.println("Give Value not found");
         
            else
            {
                prev.setNext(trav.getNext());
            }
         
        }
    }
    public void display()
    {
        if(head==null)
        {
            System.out.println("LInk LIst is empty");
        }
        else
        {
            Node trav=head; 
            while(trav.getNext() !=null )
            {
                System.out.print(trav.getData()+"->");
                trav=trav.getNext();
             
            }
         
            System.out.println(trav.getData());
        }
    }
}

class PracQuiz
{
        public static void main(String args[]) throws IOException
        {
            LinkList ls=new LinkList();
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int ch,d,p;
            do
            {
                System.out.println("Menu----");     
                System.out.println("1.Insert after value"); 
                System.out.println("2.Delete By value"); 
                System.out.println("3.Display"); 
                System.out.println("4.Exit"); 
                System.out.println("ENter Your Choice"); 
                ch=Integer.parseInt(br.readLine());
             
                switch(ch)
                {
                case 1:
                    System.out.println("Insert data after you want your New Node"); 
                    d=Integer.parseInt(br.readLine());
                    System.out.println("Insert  value  after you want to enter new Node"); 
                    p=Integer.parseInt(br.readLine());
                    ls.addValue( d, p);
                    break;
                case 2:
                    System.out.println("Insert  data of node that you want to delete"); 
                    d=Integer.parseInt(br.readLine());
                    ls.deleteValue(d);
                    break;
                case 3:
                    System.out.println("Your Link list is:");
                    ls.display();
                    break;
                case 4:         
                    break;
                }
             
            }while(ch!=4);
                   
        }
 
}