Sie sind auf Seite 1von 8

Ex No:8

Scientific Calculator using Swing and AWT Events

Aim:
To write a java program to design a simple calculator using swing and awt.
Algorithm:
1. Create a frame class with border layout. Add display text area in NORTH position.
2. Create a panel for buttons with grid layout and add it to frame in SOUTH position
3. Create two classes (implements ActionListener) one for numbers and one for operators to handle
the clicks on buttons.
4. Create buttons for numbers and create actionlistener object reference and add it to buttons.
5. Similarly create operator buttons and create and add corresponding Action Listener,
6. Add buttons to panel. (Is added in grid form)
7. In class to handle number buttons implement actionPerformed method to print clicked number in
text area.
8. In class to handle operators, save the text in text area when any operator other than '=' is pressed.
9. When '=' is pressed obtain the next value and perform th e proper operationand add th eresult to
display text area.
10. From test class main program, create an object for the frame and set default operations, properties
for frame.
Program:
import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class calc {

public static void main(String[] args) {

cfr cal=new cfr();

cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cal.setVisible(true);

class cfr extends JFrame {

JTextArea disp=new JTextArea(2,15);


JPanel but=new JPanel();

int i,flag;

float res;

String ol;

float old;

public cfr() {

flag=0;

ActionListener val=new handval();

ActionListener com=new handcom();

setLayout(new BorderLayout());

setSize(600,400);

setTitle("Calculator");

//JButton disp=new JButton("0");

disp.setEnabled(false);

add(disp,BorderLayout.NORTH);

but.setLayout(new GridLayout(6,4,10,10));

addb("sin",com);

addb("cos",com);

addb("tan",com);

addb("sqrt",com);

addb("x^2",com);

addb("x^3",com);

addb("x^x",com);

addb("clr",com);
for(i=9;i>=7;i--){

addb(String.valueOf(i),val);

addb("/",com);

for(i=6;i>=4;i--){

addb(String.valueOf(i),val);

addb("*",com);

for(i=3;i>=1;i--){

addb(String.valueOf(i),val);

addb("-",com);

addb(String.valueOf(0),val);

addb(".",val);

addb("=",com);

addb("+",com);

add(but,BorderLayout.SOUTH);

setResizable(false);

pack();

void addb(String i,ActionListener l) {

JButton b=new JButton(i);

b.addActionListener(l);

but.add(b);
}

private class handval implements ActionListener {

public void actionPerformed(ActionEvent e) {

if(flag==0){

disp.setText("");

String va=e.getActionCommand();

disp.append(va);

flag=1;

private class handcom implements ActionListener {

public void actionPerformed(ActionEvent e) {

String co=e.getActionCommand();

if(co=="clr") {

flag=0;

disp.setText("");

old=0;

ol=co;

else if(co=="=") {

if(flag==1){

if(ol=="+") {

float x=Float.parseFloat(disp.getText());
res=old+x;

disp.setText(""+String.valueOf(res));

else if(ol=="x^x") {

float x=Float.parseFloat(disp.getText());

res=(float)Math.pow(old,x);

disp.setText(""+String.valueOf(res));

else if(ol=="-") {

float x=Float.parseFloat(disp.getText());

res=old-x;

disp.setText(""+String.valueOf(res));

else if(ol=="*") {

float x=Float.parseFloat(disp.getText());

res=old*x;

disp.setText(""+String.valueOf(res));

else if(ol=="/") {

float x=Float.parseFloat(disp.getText());

res=old/x;

disp.setText(""+String.valueOf(res));

flag=2;
}

else if(flag==0&& co=="-") {

disp.setText(co);

flag=1;

else if(co=="sqrt") {

float x=Float.parseFloat(disp.getText());

res=(float)Math.sqrt(x);

disp.setText(""+String.valueOf(res));

else if(co=="x^2") {

float x=Float.parseFloat(disp.getText());

res=(float)Math.pow(x,2);

disp.setText(""+String.valueOf(res));

else if(co=="x^3") {

float x=Float.parseFloat(disp.getText());

res=(float)Math.pow(x,3);

disp.setText(""+String.valueOf(res));

else if(co=="sin") {

float x=Float.parseFloat(disp.getText());

res=(float)Math.sin(Math.toRadians(x));
disp.setText(""+String.valueOf(res));

else if(co=="cos") {

float x=Float.parseFloat(disp.getText());

res=(float)Math.cos(Math.toRadians(x));

disp.setText(""+String.valueOf(res));

else if(co=="tan") {

double x=Float.parseFloat(disp.getText());

res=(float)Math.tan(Math.toRadians(x));

disp.setText(""+String.valueOf(res));

else {

old=Float.parseFloat(disp.getText());

disp.setText("");

ol=co;

}
Output:
Result:
Thus a java program to design a simple calculator using swing and awt was executed succesfully.

Das könnte Ihnen auch gefallen