Tuesday, 23 August 2011

Thread (Two Threads working to gather)

public class MyThread1 extends Thread{

  MyThread1(String name){
        super(name);

         }

public void run(){

for(int i=1; i<=15; i++){
System.out.println("I am "+getName()+i);
        try{
       Thread.sleep(1000);
   }catch(Exception e){}
                    }
System.out.println("I Finished"+getName());



}


 public static void main(String agr[]){
          MyThread1  mt1=new MyThread1("Thread1");
          mt1.start();  
          MyThread1  mt2=new MyThread1("Thread2");
          mt2.start();  
 

      

 }

}

Friday, 19 August 2011

Exception Handling in Java

public class MyException{

public void myFun() {
int[] array = new int[3];
  try{

 for(int i=0;i<4;++i){
  array[i] = i;
  }
 System.out.println(array);
  }catch(ArrayIndexOutOfBoundsException e){
   //printed just to inform that we have entered the catch block
                       System.out.println(e.toString()+"\n");
   //System.out.println("Oops, we went to far, better go back to 0!");
  }
 }


 public static void main(String Args[]){
 MyException me=  new  MyException();
   try{
   me.myFun();
                        me.myMethod();

  }catch(Exception e){System.out.println(e.toString()+"\n");}

        }

public void  myMethod() throws ArithmeticException{
     throw new ArithmeticException();
}


}

Applet Class

//applet  wil run in web pages


import java.awt.*;
import java.applet.Applet;
public class MyApplet extends Applet{

public void init(){
Button b=new Button("OK");
this.add(b);


}

public void paint(Graphics g){
  g.drawRect(150,150,200,200);  
  g.setColor(Color.RED);
  g.fillRect(150,150,200,200); 
}

public static void main(String n[]){

MyApplet ma=new MyApplet();

ma.init();
ma.paint();
ma.stop();

}//end of main


}//end of class




//its html file would be
<HTML>
<APPLET CODE="MyApplet.class"  height=400 width=400> </applet>
</HTML>

Friday, 12 August 2011

Frame Class Populated with MenuBar,Menu and MenuItem along with TextField

import java.awt.*;
import java.awt.event.*;



class MyFrame extends Frame implements ActionListener{
   
        MyFrame(){
     Frame f = new Frame();
     MenuBar mb =new MenuBar();
                  Menu filem = new Menu("File");
                 Menu filem1 = new Menu("Edit");
                MenuItem mi = new MenuItem("Save");
                 MenuItem m2 = new MenuItem("Exit");
               MenuItem mi1 = new MenuItem("abc");
                    filem.add(mi);
                    filem.add(m2);                 
                    filem1.add(mi1);
                   mb.add(filem);
                   mb.add(filem1);
                   f.setMenuBar(mb);
                      f.setLayout(new GridLayout(2,2));
                Label l=new Label("user");
                 Label l1=new Label("password");
                TextField tf = new TextField(20);
                 TextField tf1 = new TextField(20);
             f.add(l);  
              f.add(tf);
                f.add(l1); 
                  f.add(tf1);
              
                   f.setSize(400,400);

    f.setVisible(true);
    }//end of my fram

public void actionPerformed(ActionEvent ae){}


public static void main(String m[]){
 new MyFrame();
        
    }//end of void

}//end of class

Tuesday, 9 August 2011

HOW to Extends Frame Class and How to implement an Interface for ActionListener


import java.awt.*;
import java.awt.event.*;
class MyPackage extends Frame implements ActionListener{


               MyPackage(){
Frame f=new Frame();
Button b=new Button();
b.addActionListener(this);
b.setLabel("OK");
f.add(b);
f.setLayout(new FlowLayout());
f.setVisible(true);
System.out.println("Testing Package");

}


public static void main(String m[]){
new MyPackage();

/*Button b=new Button();
b.addActionListener(this);
//b.addActionListener(f);
 b.setLabel("OK");
f.add(b);
f.setLayout(new FlowLayout());
f.setVisible(true);
System.out.println("Testing Package");*/

}


public void actionPerformed(ActionEvent s){
System.exit(0);
}

}

Saturday, 6 August 2011

Constructor is Called Automatically when the Object of MySelf Class is Created

class MySelf{
String Name;
int id;
// constructor 1
MySelf(){
Name="Mamoon";
id=7;
System.out.println("Name:"+Name+"_____"+"id:"+id);
}//end of 1
// constructor 2
MySelf(String n,int i){
Name=n;
id=i;
System.out.println("Name:"+Name+"_____"+"id:"+id);
}// end of 2
public void setName(String name){
Name=name;
}
public void setId(int j){
id=j;
}

public String getName(){
return Name;
}
public int getId(){
return id;
}
public int getId(int Nid){
id=Nid*2;
return id;
}

public static void main(String m[]){
//MySelf() ms =new MySelf();
MySelf ms=new MySelf("Mamoon",4);
ms.setName("MAMOON");
ms.getId(17);
System.out.println("Name:"+ms.getName()+"________"+"id:"+ms.getId());
}

}//end of main

Tuesday, 2 August 2011

Constructor- Is a Function which is Automatically called when Object is created using new operator

class Student extends Human{
  int RollNo;
 String ClassName;

 Student(){

System.out.println("I am constructor of Student Class");
}

public void setRollNo(int r){
RollNo=r;
}
public int getRollNo(){
return RollNo;
}


public void setClassName(String s){
ClassName=s;
}
public String getClassName(){
return ClassName;
}

public static void main(String agr[]){
                new Student();

                                               }

}