Professional Documents
Culture Documents
D:\khubaib\java>java threadtest
From thread A0
From thread A1
From thread A2
From thread B0
From thread B1
From thread B2
From thread B3
From thread A3
From thread A4
Exit from A
From thread B4 Exit
from B
b)write to demonstrate thread using yield() stop() and sleep() method.
Input:
}
System.out.println("Exit from c");
} } class
threadtest1 {
public static void main(String args[])
{
A a=new A();
B b=new B(); c
C=new c();
a.start();
b.start();
C.start(); }
}
Output:
D:\khubaib\ java>java
threadtest1 from thread A0
from thread B0 from thread A1
from thread B1 from thread A2
from thread B2 from thread B3
from thread A3 from thread A4
Exit from
Practical no 9:-
A)Design a AWT program to print the factorial for an input value.
b1=new Button("Answer");
b2=new Button("cancle");
add(l1); add(t1);
add(l2); add(t2);
add(b1);
add(b2);
setLayout(new FlowLayout());
setSize(200,300);
setVisible(true);
D:\khubaib\java>javac awtfact.java
b1=new Button("Answer");
b2=new Button("cancle");
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(l4); add(t4);
add(b1);
add(b2);
setLayout(new FlowLayout());
setSize(200,300);
setVisible(true);
b1.addActionListener(new ok());
b2.addActionListener(new exit());
}
class ok implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
s1=t1.getText();
s2=t2.getText(); String
concat; String oriStr;
oriStr=(s1);
String revStr = "";
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]); c=a/b;
catch(Exception e)
} finally
Output:-
D:\khubaib\java>javac
exceptdivision.java D:\khubaib\
is:1
Practical no .10:-
A)Design an AWT application that contains the interface to add student
information and display the same
import java.awt.*; import
java.awt.event.*; class
awtstudent extends Frame
{ intrn,ag
e;
String s1,s2,s3,s4,s5,s6,s7,s8;
String s11,s12,s13,s14,s15,s16,s17,s18;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9;
TextField t1,t2,t3,t4,t5,t6,t7,t8;
Button b1,b2; awtstudent()
{
l1=new Label("student"); l2=new
Label("Roll no:"); l3=new
Label("First Name:"); l4=new
Label("Middle Name:"); l5=new
Label("Last Name:"); l6=new
Label("class Name:"); l7=new
Label("college Name:"); l8=new
Label("Age:");
l9=new Label("Gender:");
b1=new Button("Submit");
b2=new Button("cancel");
add(l1); add(l2);
add(t1); add(l3);
add(t2); add(l4);
add(t3); add(l5);
add(t4); add(l6);
add(t5); add(l7);
add(t6); add(l8);
add(t7); add(l9);
add(t8);
add(b1);
add(b2);
setLayout(new
FlowLayout());
setSize(300,300
);
setVisible(true);
b1.addActionListener(new Submit());
b2.addActionListener(new exit());
}
class Submit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{ s1=t1.getText(); s2=t2.getText();
s3=t3.getText(); s4=t4.getText();
s5=t5.getText(); s6=t6.getText();
s7=t7.getText(); s8=t8.getText();
s11=String.valueOf(s1);
s12=String.valueOf(s2);
s13=String.valueOf(s3);
s14=String.valueOf(s4);
s15=String.valueOf(s5);
s16=String.valueOf(s6);
s17=String.valueOf(s7);
s18=String.valueOf(s8);
System.out.println("\n roll no:"+s11);
System.out.println("\n name:"+s12);
System.out.println("\n middle name:"+s13);
System.out.println("\n last Name:"+s14);
System.out.println("\n class name:"+s15);
System.out.println("\n college Name:"+s16);
System.out.println("\n age:"+s17);
System.out.println("\n gender:"+s18);
}}
class exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}}
public static void main(String args[])
{ newawtstudent(
);
}
}
Output
roll no:8
name:Khubaib
middle name:Asif
last Name:Khatib
age:18
gender:Female
Calculator()
{
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bm = new Button("*");
bd = new Button("/");
bs = new Button("-"); ba
= new Button("+"); be =
new Button("=");
bc = new Button("Clear");
b7.setBounds(10,100,50,50);
b8.setBounds(60,100,50,50);
b9.setBounds(110,100,50,50);
bm.setBounds(160,100,50,50);
b4.setBounds(10,150,50,50);
b5.setBounds(60,150,50,50);
b6.setBounds(110,150,50,50);
bd.setBounds(160,150,50,50);
b1.setBounds(10,200,50,50);
b2.setBounds(60,200,50,50);
b3.setBounds(110,200,50,50);
bs.setBounds(160,200,50,50);
b0.setBounds(10,250,50,50);
be.setBounds(60,250,100,50);
ba.setBounds(160,250,50,50);
bc.setBounds(10,80,200,20);
bc.setBackground(Color.yellow);
be.setBackground(Color.gray);
b1.setBackground(Color.gray);
b2.setBackground(Color.gray);
b3.setBackground(Color.gray);
b4.setBackground(Color.gray);
b5.setBackground(Color.gray);
b6.setBackground(Color.gray);
b7.setBackground(Color.gray);
b8.setBackground(Color.gray);
b9.setBackground(Color.gray);
b0.setBackground(Color.gray);
bd.setBackground(Color.gray);
be.setBackground(Color.red);
bs.setBackground(Color.gray);
ba.setBackground(Color.gray);
bc.setBackground(Color.gray);
bm.setBackground(Color.gray);
screen.setBounds(10,50,200,30);
add(b7); add(b8);
add(b9);
add(bm);
add(b4); add(b5);
add(b6); add(bd);
add(b1); add(b2);
add(b3);
add(bs);
add(b0);
add(be);
add(ba); add(bc);
add(screen);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bd.addActionListener(this);
bm.addActionListener(this);
bs.addActionListener(this);
ba.addActionListener(this);
be.addActionListener(this);
bc.addActionListener(this);
setSize(220,310); setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public void append(int a)
{
int c;
if(screen.getText().equals(""))
{
screen.setText(Integer.toString(a));
}
else
{
c = Integer.parseInt(screen.getText());
screen.setText(Integer.toString((c*10)+a));
}
}
if(e.getSource()==b0)
{
append(0);
}
if(e.getSource()==b1)
{
append(1);
}
if(e.getSource()==b2)
{
append(2);
}
if(e.getSource()==b3)
{
append(3);
}
if(e.getSource()==b4)
{
append(4);
}
if(e.getSource()==b5)
{
append(5);
}
if(e.getSource()==b6)
{
append(6);
}
if(e.getSource()==b7)
{
append(7);
}
if(e.getSource()==b8)
{
append(8);
}
if(e.getSource()==b9)
{
append(9);
}
if(e.getSource()==be)
{
System.out.print(n1);
if(n1>0)
{
n2 = Integer.parseInt(screen.getText());
if(sign < 4)
{
switch(sign)
{
case 0:
screen.setText(Integer.toString(n1*n2));
break; case 1:
screen.setText(Integer.toString(n1/n2));
break; case 2:
screen.setText(Integer.toString(n1-n2));
break; default:
screen.setText(Integer.toString(n1+n2));
break;
}
}
}
else
{
screen.setText("");
}
}
if(e.getSource()==bd || e.getSource()==bm ||
e.getSource()==bs || e.getSource()==ba
|| e.getSource()==bc)
{
if(screen.getText().equals(""))
{
screen.setText("");
}
else if(e.getSource()==bm)
{
n1 = Integer.parseInt(screen.getText());
System.out.print(n1); screen.setText("");
sign = 0;
}
else if(e.getSource()==bd)
{
n1 = Integer.parseInt(screen.getText());
screen.setText("");
sign = 1;
}
else if(e.getSource()==bs)
{
n1 = Integer.parseInt(screen.getText());
screen.setText("");
sign = 2;
}
else if(e.getSource()==ba)
{
n1 = Integer.parseInt(screen.getText());
screen.setText("");
sign = 3;
}
else if(e.getSource()==bc)
{
screen.setText("");
}
}
}
public static void main(String[] args)
{
new Calculator();
}
}
Output:- 6
D:\khubaib\java>javac calculator.java