0% found this document useful (0 votes)
520 views3 pages

Q1.) Write A Program To Implement Simple Arithmetic Calculator. Perform Appropriate Validations Program Code

This program implements a simple arithmetic calculator in Java. It creates a graphical user interface with buttons for numbers 0-9, operations like add, subtract, multiply and divide, an equals button, and a display field to show calculations and results. When a number button or operation button is pressed, the corresponding value is added to the display field or saved for calculation. When equals is pressed, the program performs the calculation based on the stored operation and values, and displays the result.

Uploaded by

BadBoy Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
520 views3 pages

Q1.) Write A Program To Implement Simple Arithmetic Calculator. Perform Appropriate Validations Program Code

This program implements a simple arithmetic calculator in Java. It creates a graphical user interface with buttons for numbers 0-9, operations like add, subtract, multiply and divide, an equals button, and a display field to show calculations and results. When a number button or operation button is pressed, the corresponding value is added to the display field or saved for calculation. When equals is pressed, the program performs the calculation based on the stored operation and values, and displays the result.

Uploaded by

BadBoy Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1.) Write a program to implement simple arithmetic calculator.

Perform
appropriate validations

Program Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator  extends JFrame implements ActionListener


{
                String msg=" ";
                                int v1,v2,result;
                                JTextField t;
                                JButton b[]=new JButton[10];
                                JButton add,sub,mul,div,clear,equals;
                                char choice;
                                JPanel p,p1;
                                Calculator()
                                {
                                                setLayout(new BorderLayout());
                                                                p =new JPanel();
                                                                t=new JTextField(20);
                                                                p.add(t);
                                                               
                                                                p1=new JPanel();
                                                                p1.setLayout(new GridLayout(5,4));
                                                                for(int i=0;i<10;i++)
                                                                {
                                                                                b[i]=new JButton(""+i);
                                                                }
                                                equals=new JButton("=");
                                                                add=new JButton("+");
                                                                sub=new JButton("-");
                                                                mul=new JButton("*");
                                                                div=new JButton("/");
                                                                clear=new JButton("C");
                                                               
                                                                for(int i=0;i<10;i++)
                                                                {
                                                                                p1.add(b[i]);
                                                                }
                                               
                                                                p1.add(equals);
                                                                p1.add(add);
                                                                p1.add(sub);
                                                                p1.add(mul);
                                                                p1.add(div);
                                                                p1.add(clear);
                                                               
                                                                for(int i=0;i<10;i++)
                                                                {
                                                                                b[i].addActionListener(this);
                                                                }
                                                add.addActionListener(this);
                                                                sub.addActionListener(this);
                                                                mul.addActionListener(this);
                                                                div.addActionListener(this);
                                                                clear.addActionListener(this);
                                                                equals.addActionListener(this);
                                                               
                                                                add(p,BorderLayout.NORTH);
                                                                add(p1);
                                                                setVisible(true);
                                                setSize(500,500);
                                                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
                               
                                }
               
                                public void actionPerformed(ActionEvent ae)
                                {
                                                String str = ae.getActionCommand();
                                                                char ch = str.charAt(0);
                                                                if ( Character.isDigit(ch))
                                                                                t.setText(t.getText()+str);
                                                                else
                                                                                if(str.equals("+"))
                                                                                {
                                                                                                v1=Integer.parseInt(t.getText());
                                                                                                                choice='+';
                                                                                                                t.setText("");
                                                                                }
                                                                                else if(str.equals("-"))
                                                                                {
                                                                                                v1=Integer.parseInt(t.getText());
                                                                                                                choice='-';
                                                                                                                t.setText("");
                                                                                }
                                                                                else if(str.equals("*"))
                                                                                {
                                                                                                v1=Integer.parseInt(t.getText());
                                                                                                                choice='*';
                                                                                                                t.setText("");
                                                                                }
                                                                                else if(str.equals("/"))
                                                                                {
                                                                                                v1=Integer.parseInt(t.getText());
                                                                                                                choice='/';
                                                                                                                t.setText("");
                                                                                }
                                               
                                                                if(str.equals("="))
                                                                {
                                                                                v2=Integer.parseInt(t.getText());
                                                                                                if(choice=='+')
                                                                                                                result=v1+v2;
                                                                                                else if(choice=='-')
                                                                                                                result=v1-v2;
                                                                                                else if(choice=='*')
                                                                                                                result=v1*v2;
                                                                                                else if(choice=='/')
                                                                                                                result=v1/v2;
                                                                                                                               
                                                                                                                                t.setText(""+result);
                                                                }             
                                                if(str.equals("C"))
                                                {
                                                                t.setText("");
                                                }
                                }
                public static void main(String a[])
                {
                                Calculator ob = new Calculator();
                }
}

OUTPUT:

You might also like