You are on page 1of 17

Practical no 7:-

A)Write a java program to implement the vectors.


INPUT:
class A extends Thread
{
int i;
public void run()
{
for(i=0;i<5;i++)
{
System.out.println("From thread A"+i);
}
System.out.println("Exit from A");
}}
class B extends Thread
{
int j;
public void run()
{
for(j=0;j<5;j++)
{
System.out.println("From thread B"+j);
}
System.out.println("Exit from B");
}}
class threadtest
{
public static void main(String args[])
{
A a=new A();
a.start();
B b=new B();
b.start();
}
}
OUTPUT:

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:

class A extends Thread


{
int i;
public void run()
{
for(i=0;i<5;i++)
{ yield();
System.out.println("from thread A"+i);
}
System.out.println("Exit from A");
}
}

class B extends Thread


{
int j;
public void run()
{
for(j=0;j<5;j++)
{
System.out.println("from thread B"+j);
if(j==3) stop();
}
System.out.println("Exit from B");
}}
class c extends Thread
{ int
k;
publi
c
void
run()
{ for
(k=0
;k<5
;k+
+)
{
System.out.println("from thread c"+k);
if(k==1) { sleep(2000);
}

}
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.

import java.awt.*; import


java.awt.event.*;
class awtfact extends Frame
{
int n,fact=1; String
s1;
Label l1,l2;
TextField t1,t2;
Button b1,b2;
awtfact() {
l1=new Label("Enter a number"); l2=new
Label("Factorial of number");

t1=new TextField(15); t2=new


TextField(15);

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);

b1.addActionListener(new ok()); b2.addActionListener(new


exit());
}
class ok implements ActionListener
{
public void actionPerformed(ActionEvent e)
{ s1=t1.getText();
n=Integer.parseInt(s1);
int fact = 1;
for(int i = n; i> 0; --i)
{ fact *=
i; }
s1=Strin
g.value
Of(fact);
t2.setTe
xt(s1);
}}
class exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}}
public static void main(String args[])
{ newawtfact(
);
}
}
Output:-

D:\khubaib\java>javac awtfact.java

D:\khubaib\java>java awtfact extends Frame

B)Design an AWT program to perform various string operations like


reverse string, string concatenation etc.
import java.awt.*; import
java.awt.event.*;
class awtstring extends Frame
{
int n;
String s1,s2,s3;
Label l1,l2,l3,l4;
TextField t1,t2,t3,t4; Button
b1,b2;
awtstring()
{
l1=new Label("Enter a first string"); l2=new
Label("Enter a second string"); l3=new
Label("The reverse string is");
l4=new Label("The concated string is");

t1=new TextField(15); t2=new


TextField(15); t3=new
TextField(15); t4=new
TextField(15);

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 = "";

for (int i = 0; i<oriStr.length(); i++)


{
revStr = oriStr.charAt(i) + revStr;
}
s1=String.valueOf(revStr); t3.setText(s1);
s3=oriStr;
String oper = s3.concat(s2); s2=String.valueOf(oper);
t4.setText(s2);
}
}
class exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String args[])
{
new awtstring();
}
}
Output:-
D:\khubaib\java>javac revstrgui.java
D:\khubaib\java>java awtstring extends Frame
c)write a java program to display division by taking second number zero
in exception handling.
class exceptzero

public static void main(String args[])

int a,b,c=0; try

a=Integer.parseInt(args[0]);

b=Integer.parseInt(args[1]); c=a/b;

catch(Exception e)

System.out.println("\n Not divisible by zero");

} finally

System.out.println("\n the division is:"+c);

Output:-

D:\khubaib\java>javac

exceptdivision.java D:\khubaib\

java>java exceptzero 3 3 the division

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:");

t1=new TextField(15); t2=new


TextField(15); t3=new
TextField(15); t4=new
TextField(15); t5=new
TextField(15); t6=new
TextField(15); t7=new
TextField(20); t8=new
TextField(15);

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

D:\khubaib\java>java awtstudent extends Frame

roll no:8

name:Khubaib

middle name:Asif

last Name:Khatib

class name:SY BSC IT

college Name:GURUKUL COLLEGE CHIPLUN

age:18

gender:Female

b) Design a calculator based on AWT application.


import java.awt.*; import
java.awt.event.*;
class Calculator extends Frame implements ActionListener
{
Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bm, bd, bs, ba, be, bc; TextField
screen;
int n1 = 0, sign = 4;

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 = new TextField();

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));
}
}

public void actionPerformed(ActionEvent e)


{
int n2 = 1, res;

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

D:\khubaib\java>java Calculator extends Frame implements ActionListener

You might also like