Professional Documents
Culture Documents
Pra 2 (Java)
Pra 2 (Java)
Batch No-B2
Practical No-2
Practical - 02
Aim :- Study different component of swing.
(a) Develop calculator application.
(b) Percentage count for any semester.
(a)
import java.io.*;
import java.lang.String.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class calc extends JFrame
{
public static int op=0;
public static double a=0,b=0,c=0;
public static void main(String args[])
{
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cn=jf.getContentPane();
cn.setLayout(new GridLayout(7,3));
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
final JTextField tf=new JTextField("");
final JButton b1=new JButton("1");
final JButton b2=new JButton("2");
final JButton b3=new JButton("3");
Page
100510107038
Batch No-B2
Practical No-2
Page
100510107038
Batch No-B2
Practical No-2
cn.add(p5);
cn.add(p3);
cn.add(p2);
cn.add(p1);
cn.add(p4);
ActionListener lst=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str,no;
if(e.getSource()==b1)
{
str=tf.getText();
no="1";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b2)
{
str=tf.getText();
no="2";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b3)
{
str=tf.getText();
no="3";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b4)
{
str=tf.getText();
Page
100510107038
Batch No-B2
Practical No-2
no="4";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b5)
{
str=tf.getText();
no="5";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b6)
{
str=tf.getText();
no="6";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b7)
{
str=tf.getText();
no="7";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b8)
{
str=tf.getText();
no="8";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b9)
{
Page
100510107038
Batch No-B2
Practical No-2
str=tf.getText();
no="9";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b0)
{
str=tf.getText();
no="0";
str=str+no;
tf.setText(str);
}
str=tf.getText();
b=Double.parseDouble(str);
if(e.getSource()==b11)
{
op=1;
a=b;
tf.setText("");
}
if(e.getSource()==b12)
{
op=2;
a=b;
tf.setText("");
}
if(e.getSource()==b13)
{
op=3;
a=b;
tf.setText("");
}
if(e.getSource()==b14)
{
op=4;
Page
100510107038
Batch No-B2
Practical No-2
a=b;
tf.setText("");
}
if(e.getSource()==b15)
{
if(op==1)
{
c=a+b;
str=Double.toString(c);
tf.setText(str);
}
if(op==2)
{
if(a>b)
{
c=a-b;
str=Double.toString(c);
tf.setText(str);
}
else
{
c=b-a;
str=Double.toString(c);
tf.setText("-"+str);
}
}
if(op==3)
{
c=a*b;
str=Double.toString(c);
tf.setText(str);
}
if(op==4)
{
if(a>b)
Page
100510107038
Batch No-B2
Practical No-2
{
c=a/b;
str=Double.toString(c);
tf.setText(str);
}
else
{
c=a/b;
str=Double.toString(c);
tf.setText(str);
}
}
}
if(e.getSource()==b16)
{
str=tf.getText();
no=".";
str=str+no;
tf.setText(str);
}
if(e.getSource()==b17)
{
op=0;
a=0;b=0;c=0;
tf.setText("");
}
if(e.getSource()==b18)
{
int size=str.length();
str=str.substring(0,size-1);
tf.setText(str);
}
}
};
b1.addActionListener(lst);
Page
100510107038
Batch No-B2
Practical No-2
b2.addActionListener(lst);
b3.addActionListener(lst);
b4.addActionListener(lst);
b5.addActionListener(lst);
b6.addActionListener(lst);
b7.addActionListener(lst);
b8.addActionListener(lst);
b9.addActionListener(lst);
b0.addActionListener(lst);
b11.addActionListener(lst);
b12.addActionListener(lst);
b13.addActionListener(lst);
b14.addActionListener(lst);
b15.addActionListener(lst);
b16.addActionListener(lst);
b17.addActionListener(lst);
b18.addActionListener(lst);
jf.pack();
jf.show();
}
}
Output :-
Page
100510107038
Batch No-B2
Practical No-2
25.589 * 3.678
(b)
import java.io.*;
import java.lang.String.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Avg extends JFrame
{
public static void main(String args[])
{
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cn=jf.getContentPane();
cn.setLayout(new GridLayout(9,2));
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
Page
100510107038
Batch No-B2
Practical No-2
CD :");
AJT :");
ACT :");
.Net :");
Total Is :");
Page
100510107038
Batch No-B2
Practical No-2
p9.add(l8);
p9.add(tf6);
cn.add(p1);
cn.add(p2);
cn.add(p3);
cn.add(p4);
cn.add(p5);
cn.add(p6);
cn.add(p7);
cn.add(p8);
cn.add(p9);
ActionListener lst=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Double n1,n2,n3,n4,n5,total,avg;
String str,str1;
if(e.getSource()==b1)
{
str=tf.getText();
n1=Double.parseDouble(str);
str=tf1.getText();
n2=Double.parseDouble(str);
str=tf2.getText();
n3=Double.parseDouble(str);
str=tf3.getText();
n4=Double.parseDouble(str);
str=tf4.getText();
n5=Double.parseDouble(str);
total=n1+n2+n3+n4+n5;
avg=(total*100)/500;
str=Double.toString(total);
tf5.setText(str);
if(n1<35||n2<35||n3<35||n4<35||n5<35)
Page
100510107038
Batch No-B2
Practical No-2
{
str1="Faile";
tf6.setText(str1);
}
else
{
str1=Double.toString(avg);
tf6.setText(str1);
}
}
}
};
b1.addActionListener(lst);
jf.pack();
jf.show();
}
}
Output :-
Page