Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

100510107038

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)

Develop calculator application.

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

Alpha College of Engineering & Technology

Page

100510107038

Batch No-B2

Practical No-2

final JButton b4=new JButton("4");


final JButton b5=new JButton("5");
final JButton b6=new JButton("6");
final JButton b7=new JButton("7");
final JButton b8=new JButton("8");
final JButton b9=new JButton("9");
final JButton b0=new JButton("0");
final JButton b11=new JButton("+");
final JButton b12=new JButton("-");
final JButton b13=new JButton("*");
final JButton b14=new JButton("/");
final JButton b15=new JButton("=");
final JButton b16=new JButton(".");
final JButton b17=new JButton("AC");
final JButton b18=new JButton("DEL");
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b13);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b12);
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b11);
p4.add(b16);
p4.add(b0);
p4.add(b15);
p4.add(b14);
p5.add(b17);
p5.add(b18);
cn.add(tf);

Alpha College of Engineering & Technology

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

Alpha College of Engineering & Technology

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

Alpha College of Engineering & Technology

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;

Alpha College of Engineering & Technology

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)

Alpha College of Engineering & Technology

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

Alpha College of Engineering & Technology

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 :-

Alpha College of Engineering & Technology

Page

100510107038

Batch No-B2

Practical No-2

25.589 * 3.678

(b)

Percentage count for any semester.

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

Alpha College of Engineering & Technology

Page

100510107038

Batch No-B2

Practical No-2

JPanel p5=new JPanel();


JPanel p6=new JPanel();
JPanel p7=new JPanel();
JPanel p8=new JPanel();
JPanel p9=new JPanel();
final JLabel l1=new JLabel("Enter Your Marks Here :");
final JLabel l2=new JLabel("

CD :");

final JTextField tf=new JTextField("",5);


final JLabel l3=new JLabel(" WCMP :");
final JTextField tf1=new JTextField("",5);
final JLabel l4=new JLabel("

AJT :");

final JTextField tf2=new JTextField("",5);


final JLabel l5=new JLabel("

ACT :");

final JTextField tf3=new JTextField("",5);


final JLabel l6=new JLabel("

.Net :");

final JTextField tf4=new JTextField("",5);


final JButton b1=new JButton("Count");
final JLabel l7=new JLabel("

Total Is :");

final JTextField tf5=new JTextField("",5);


final JLabel l8=new JLabel("Percentage :");
final JTextField tf6=new JTextField("",5);
p1.add(l1);
p2.add(l2);
p2.add(tf);
p3.add(l3);
p3.add(tf1);
p4.add(l4);
p4.add(tf2);
p5.add(l5);
p5.add(tf3);
p6.add(l6);
p6.add(tf4);
p7.add(b1);
p8.add(l7);
p8.add(tf5);

Alpha College of Engineering & Technology

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)

Alpha College of Engineering & Technology

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 :-

Alpha College of Engineering & Technology

Page

You might also like