Wednesday, April 24, 2019

sub package



step1:: go to D:\p1\p2

step2: p.java


package p1.p2;
import java.util.Scanner;
public class p
{
public void f(int x,int y){
System.out.println(x);
System.out.println(y);
}
}

step3:
D:\

step4 ::cl5.java

import p1.p2.p;
import java.util.Scanner;
class cl4 extends p
{
}


public class cl5
{
public static void main(String args[])
{
cl4 ob=new cl4();
ob.f(2,3);
}
}



step5:

D:\>cd p1

D:\p1>cd p2

D:\p1\p2>javac p.java

D:\p1\p2>cd ..

D:\p1>cd ..

D:\>javac cl5.java

D:\>java cl5
2
3

D:\>java cl5

package

1. single level package -                        text - click here


                                                               video - step by step video 



2.       two level package                            text - click here                                                                                                                                                                                        




Example 3 ::  two level                                                                                                                                                                                                                                      



4. difference between default and public                                                                                                                                                                                                             

                                                                          






hierarchical interface


                                       



interface A
{
public void input();
}


interface B extends A
{
void output();
}

interface c extends A
{
void output();
}




class cl1 implements B
{
int a,b;
public void input()
{
a=3;
b=7;
}
public void output()
{
int min;
min=a+b;
System.out.println(min);
}
}




class cl2 implements c
{
int a,b;
public void input()
{
a=4;
b=8;
}
public void output()
{
int min;
min=a-b;
System.out.println(min);
}
}
public class diya1
{
public static void main(String args[])
{

cl1 ob1=new cl1();
ob1.input();
ob1.output();

cl2 ob2=new cl2();
ob2.input();
ob2.output();
}
}


D:\>javac diya1.java

D:\>java diya1
10
-4

D:\>

                                                                                                                                                                            edited by diya

interface in java



1. 


                    click


2.



                    click



3.


                  click


4. 

     
                         click

multilevel interface

     





interface A
{
void input();
}
interface B extends  A
{
public void output();
}
class cl implements B
{
int a,b;
public void input()
{

a=2;
b=3;}
public void output()
{
int sum;
sum=a+b;
System.out.println(sum);
}
}
public class diya1
{
public static void main(String args[])
{
cl ob=new cl();
ob.input();
ob.output();
}}


D:\>javac diya1.java

D:\>java diya1
5

D:\>
                                                                                                                     edited by diya

multiple inheritence






interface A
{
void input();
}
interface B
{
public void output();
}
class cl implements A,B
{
int a,b;
public void input()
{

a=2;
b=3;}
public void output()
{
int sum;
sum=a+b;
System.out.println(sum);
}
}
public class diya1
{
public static void main(String args[])
{
cl ob=new cl();
ob.input();
ob.output();
}
}

D:\>javac diya1.java


D:\>java diya1
5

D:\>
                                                                                                                                   edited by diya

one level interface


interface A
{
void input();
void sum();
}

 class cl implements A
{
int a,b;
public void input()
{
a=2;
b=3;
}

public void sum()
{
int c;
c=a+b;
System.out.println(c);
}
}
public class p2
{
public static void main(String args[])
{
cl ob=new cl();
ob.input();
ob.sum();
}
}

Output::

D:\>javac p2.java

D:\>java p2
5

D:\>javac diya1.java
                                                                                                                                   edited by diya

Tuesday, April 23, 2019

basics of event handling



1.  label: 

 l=new Label();  
 l.setText("string");



2. textbox:

TextField tf;
String s=tf.getText();
tf3.setText(text);


3. text area:

area=new TextArea();
 String text=area.getText();

    textArea = new TextArea(2, 10);
    textArea.setSize(300, 100);

    textArea.setText("123456 789123");


4. checkbox:

Checkbox checkbox2 = new Checkbox("Java"true);  



5. checkbox group :
  
CheckboxGroup cbg = new CheckboxGroup(); 
Checkbox checkBox1 = new Checkbox("C++", cbg, false);                       Checkbox checkBox2 = new Checkbox("Java", cbg, false); 



6. list box:

Choice c=new Choice();  
        c.setBounds(100,100, 75,75);  
        c.add("Item 1");  
        c.add("Item 2");  
        c.add("Item 3");  
        c.add("Item 4");  

        c.add("Item 5"); 


print string using textbox and commandbutton (method 2)

import java.awt.*; 
import java.awt.event.*; 
class p1 extends Frame implements ActionListener{ 
TextField tf; 
p1(){ 
 
//create components 
tf=new TextField(); 
tf.setBounds(60,50,170,20); 
Button b=new Button("click me"); 
b.setBounds(100,120,80,30); 
 
//register listener 
b.addActionListener(this);//passing current instance 
 
//add components and set size, layout and visibility 
add(b);add(tf); 
setSize(300,300); 
setLayout(null); 
setVisible(true); 

public void actionPerformed(ActionEvent e){ 
tf.setText("Welcome"); 

public static void main(String args[]){ 
new p1(); 



output::





reference -- javapoint.com
  • Button
    • public void addActionListener(ActionListener a){}
  • MenuItem
    • public void addActionListener(ActionListener a){}
  • TextField
    • public void addActionListener(ActionListener a){}
    • public void addTextListener(TextListener a){}
  • TextArea
    • public void addTextListener(TextListener a){}
  • Checkbox
    • public void addItemListener(ItemListener a){}
  • Choice
    • public void addItemListener(ItemListener a){}
  • List
    • public void addActionListener(ActionListener a){}
    • public void addItemListener(ItemListener a){}





reference -- javapoint.com

print string using text box and command buttton

import java.awt.*; 
import java.awt.event.*; 
public class p1 { 
public static void main(String[] args)
 { 
    Frame f=new Frame("Button Example"); 
    final TextField tf=new TextField(); 
    tf.setBounds(50,50, 150,20); 
    Button b=new Button("Click Here"); 
    b.setBounds(50,100,60,30); 
 
 b.addActionListener(new ActionListener()
                                       { 
                                       public void actionPerformed(ActionEvent e)
                                        { 
                                           tf.setText("this is a text box");
                                        } 
                                      }
                                   ); 
    f.add(b);f.add(tf); 
    f.setSize(400,400); 
    f.setLayout(null); 
    f.setVisible(true); 




output:::



set bound() method in java

public void setBounds(int x, int y, int width, int height)

Friday, April 12, 2019

exception handling

1. divide by zero without exception  -link
2. divide by zero with exception - link
3. null pointer - link
4. number format exception - link
5. array index out of bound- link
6. own exception - link
7. nested try -link


threads

1. thread using thread class -link
2. Java Thread Example by implementing Runnable inter...  link
3. multithreading with run() -- link
4. multithreading with start() -- link
5. multithreading with join method -- link
6. thread setname(),getname(),getid() -- link
7. current thread name - link
8. thread priority - link
9. set priority of thread - link
10. daemon thread -  link
11. thread suspend()/resume() -- link
12. without synchronization -- link
13. synchronization - i) synchronized method -- link
                                  ii) synchronized block -- link










dynamic 2d array in java

class cll
{
public static void main(String args[])
{

int a[][];
a=new int[2][];
for (int i=0;i<2;i++)
{
   a[i]=new int [i+2];
   for(int j=0;j<i+2;j++)
     {
            a[i][j]=i*j;
     }
}

System.out.println("matrix  ");
for (int i=0;i<2;i++)
{
 
   for(int j=0;j<i+2;j++)
     {
            System.out.print(a[i][j]+" ");
     }
     System.out.println(" ");
}

System.out.println("total row "+ a.length);
System.out.println("total column in row 0="+a[0].length);
System.out.println("total column in row 1="+a[1].length);

}
}




D:\>javac cll.java

D:\>java cll
matrix
0 0
0 1 2
total row 2
total column in row 0=2
total column in row 1=3

D:\>



exception handling



public class cl {
 public static void main(String args[]) {
  int d = 0;
  int n = 20;
  try {
   int fraction = n / d;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}




$javac cl.java
$java -Xmx128M -Xms16M cl
In the catch Block due to Exception = java.lang.ArithmeticException: / by zero
End Of Main

exception handling

public class cl {
 public static void main(String args[]) {
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
 at cl.main(cl.java:4)

exception handling

{
 public static void main(String args[]) {
  String s="abc";
int i=Integer.parseInt(s);//NumberFormatException

  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:580)
 at java.lang.Integer.parseInt(Integer.java:615)
 at cl.main(cl.java:4)

exception handling

public class cl {
 public static void main(String args[]) {
  String s=null;
System.out.println(s.length());//NullPointerException
  System.out.println("End Of Main");
 }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.NullPointerException
 at cl.main(cl.java:4)

exception handling

public class cl {
 public static void main(String args[]) {
  int d = 0;
  int n = 20;
  try {
   int fraction = n / d;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
In the catch Block due to Exception = java.lang.ArithmeticException: / by zero
End Of Main

exception handling

public class cl {
   public static void main(String args[]){
      int d = 0;
      int n = 20;
      int fraction = n/d;
     System.out.println("End Of Main");
   }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at cl.main(cl.java:5)

thread suspend()/resume()

public class cl extends Thread{
 public void run(){
  for(int i=1;i<=3;i++){
   try{
    Thread.sleep(500);
   }catch(Exception e){System.out.println(e);}
  System.out.println(i);
  }
 }
public static void main(String args[]){
 cl t1=new cl();
 cl t2=new cl();
 cl t3=new cl();
 t1.start();
 t2.start();
 t3.start();
 }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
1
1
1
2
2
2
3
3
3




public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 
 t1.start(); 
 t2.start(); 
 t2.suspend();
 t3.start(); 
 t2.resume();
 } 
}

daemon thread

public class cl extends Thread{
 public void run(){
  if(Thread.currentThread().isDaemon()){
   System.out.println("daemon thread");
  }
  else{
  System.out.println("user thread ");
 }
 }
 public static void main(String[] args){
  cl t1=new cl();
  cl t2=new cl();
  cl t3=new cl();

  t1.setDaemon(true);
 
  t1.start();
  t2.start();
  t3.start();
 }
}

$javac cl.java
$java -Xmx128M -Xms16M cl
daemon thread
user thread 
user thread 

set priority of thread


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" ");
   System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); 

 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
  t1.setPriority(Thread.MIN_PRIORITY); 
  t2.setPriority(Thread.MAX_PRIORITY);
 
 t1.start(); 
 t2.start(); 
 t3.start(); 


 } 



$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 running thread priority is:1
1
Thread-1 running thread priority is:10
1
Thread-2 running thread priority is:5
1
Thread-0 running thread priority is:1
2
Thread-1 running thread priority is:10
2
Thread-2 running thread priority is:5
2
Thread-0 running thread priority is:1
3
Thread-1 running thread priority is:10
3
Thread-2 running thread priority is:5
3

thread priority


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" ");
   System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); 

 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 



$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 running thread priority is:5
1
Thread-0 running thread priority is:5
2
Thread-0 running thread priority is:5
3
Name of t1:dj thread
Thread-1 running thread priority is:5
1
Thread-2 running thread priority is:5
1
Thread-1 running thread priority is:5
2
Thread-2 running thread priority is:5
2
Thread-1 running thread priority is:5
3
Thread-2 running thread priority is:5
3

current thread name

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" "); 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 1
Thread-0 2
Thread-0 3
Name of t1:dj thread
Thread-1 1
Thread-2 1
Thread-1 2
Thread-2 2
Thread-1 3
Thread-2 3

thread setname(),getname(),getid()

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
1
2
3
Name of t1:dj thread
1
1
2
2
3
3