Last Updated:

Calculator in Java

Calculator in Java

We have to write a Java program in such a way that it creates a calculator that allows us to perform the basic operations of addition, subtraction, multiplication, and division.

Expected inputs and outputs

To create a calculator, we can have the following different sets of inputs and outputs.

Addition:

When you enter the addition expression "98+102", the result is expected to be displayed as "98+102=200.0".

Subtraction:

If you enter the subtraction expression "200.0-58.75", the result is expected to be displayed as "200.0-58.75=141.25".

Multiplication:

When you enter the multiplication expression "141.25*20", the result is expected to be displayed as "141.25*20=2825.0".

To perform division : When the denominator is not zero

When you enter the tick mark "2825.0/5", the result is expected to be displayed as "2825.0/5=565.0".

To perform division : When the denominator is zero

When you enter the tick mark "565.0/0," the error is expected to appear as "565.0/0=Zero Division Error."

Solution to the problem

  1. Create a text box to accept the expression and display the output.
  2. Create buttons for numbers and decimal points.
  3. Create a button to clear the full expression.
  4. Create buttons for operations, that is, for addition, subtraction, multiplication, and division, and an equal button for calculating the result.
  5. Add an ActionListener to all buttons.
  6. Calculate the result and display it in the text box.

Program/Source Code

 

Here is the source code of the Java program to create a basic calculator. The program has been successfully compiled and tested using the compiler. The output of the program is also shown below.

/*A program to demonstrate a basic calculator using an applet*/
importjava.awt.*;
importjava.applet.*;
importjava.awt.event.*;
public class Calculator extends the applet and implements ActionListener
{
TextFieldinp;
//Function to add objects to the frame
publicvoidinit()
{
setBackground(Color.black);
setLayout(null);
int i;
inp = new TextField();
inp.setBounds(150,100,270,50);
this.add(inp);
Button button[] = new Button[10];
for(i=0;i<10;i++)
{
button[i] = new Button(String.valueOf(9-i));
button[i].setBounds(150+((i%3)*50),150+((i/3)*50),50,50);
this.add(button[i]);
button[i].addActionListener(this);
}
Button dec=new Button(«.»);
dec.setBounds(200,300,50,50);
this.add(dec);
dec.addActionListener(this);

Button clr=new Button(«C»);
clr.setBounds(250,300,50,50);
this.add(clr);
clr.addActionListener(this);

Button operator[] = new Button[5];
operator[0]=new Button(«/»);
operator[1]=new Button(«*»);
operator[2]=new Button(«-«);
operator[3]=new Button(«+»);
operator[4]=new Button(«=»);
for(i=0;i<4;i++)
{
operator[i].setBounds(300,150+(i*50),50,50);
this.add(operator[i]);
operator[i].addActionListener(this);
}
operator[4].setBounds(360,310,80,60);
this.add(operator[4]);
operator[4].addActionListener(this);
}
String number1=»»;
String op=»»;
String number2=»»;
//Function for evaluating
the expression public void actionPerformed(ActionEvent e)
{
String button = e.getActionCommand();
charch = button.charAt(0);
if(ch>='0′ &&ch<='9'|| ch=='.')
{
if (!op.equals(«»))
number2 = number2 + button;
else
number1 = number1 + button;
inp.setText(number1+op+number2);
}
else if(ch==’C’)
{
number1 = op = number2 = «»;
inp.setText(«»);
}
else if (ch =='=')
{
if(!num1.equals(«") && !number2.equals("))
{
double temp;
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
if(n2==0 &&op.equals(/"))
{
inp.setText(number1+op+number2+" = Split by zero");
number1 = op = number2 = "";
}
else
{
if (op.equals(«+»))
temp = n1 + n2;
else if (op.equals(«-«))
temp = n1 — n2;
else if (op.equals(«/»))
temp = n1/n2;
else
temp = n1*n2;
inp.setText(num1+op+num2+» = «+temp);
num1 = Double.toString(temp);
op = num2 = «»;
}
}
else
{
num1 = op = num2 = «»;
inp.setText(«»);
}
}
else
{
if (op.equals(«») || number2.equals(«»))
op = button;
else
{
double temp;
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
if(n2==0 &&op.equals(«/»))
{
inp.setText(number1+op+number2+» = Zero Division Error»);
number1 = op = number2 = «»;
}
else
{
if (op.equals(«+»))
temp = n1 + n2;
else if (op.equals(«-«))
temp = n1 — n2;
else if (op.equals(«/»))
temp = n1/n2;
else
temp = n1*n2;
number1 = Double.toString(temp);
op = button;
number2 = «»;
}
}
inp.setText(number1+op+number2);
}
}
}
/*
<applet code = Calculator.class width=600 height=600>
</applet>

*/

Explanation of the program

  1. When you press any button on the calculator, the actionPerformed function is called. Get the button pressed with getActionCommand.
  2. If the button pressed is a number or decimal point, one of the following actions is performed:
    a) The digit is connected to the second number if the operator has not been encountered.
    b) Otherwise, the digit is connected to the first number.
  3. If the button is on the "clear" button, then clear the input field.
  4. If the button pressed is an equals operator, one of the following actions is performed:
    a) If neither the first nor the second number is empty, then calculate the result and display it in frame
    b) Otherwise, clear the input field.
  5. When you click any of the {+,-,*,/} buttons, one of the following actions is performed:
    (a) If either the operator or the second number is empty, set the button pressed as the operator.
    b) Otherwise, calculate the result and display it in the frame.