Computer Science, asked by Ankush777, 1 year ago

Coding of this calculator in java

Attachments:

Answers

Answered by pavan107
6
The following Java code is for a simple calculator. There are nine JButtons to represent the numbers 1 to 9, and three JButtons for addition, subtraction and totaling the result. A JTextField at the top keeps track of the numbers being pressed and the result of the arithmetic operation.

The purpose of this Java program is to show how to implement an ActionListener interface for handling JButton button event clicks by using the containing class, an inner class and an anonymous inner class.

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;

public class SimpleCalc implements ActionListener{

JFrame guiFrame;
JPanel buttonPanel;
JTextField numberCalc;
int calcOperation = 0;
int currentCalc;

//Note: Typically the main method will be in a
//separate class. As this is a simple one class
//example it's all in the one class.
public static void main(String[] args) {

//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{

@Override
public void run()
{

new SimpleCalc();
}
});

}

public SimpleCalc()
{
guiFrame = new JFrame();

//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Simple Calculator");
guiFrame.setSize(300,300);

//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);

numberCalc = new JTextField();
numberCalc.setHorizontalAlignment(JTextField.RIGHT);
numberCalc.setEditable(false);

guiFrame.add(numberCalc, BorderLayout.NORTH);

buttonPanel = new JPanel();

//Make a Grid that has three rows and four columns
buttonPanel.setLayout(new GridLayout(4,3));
guiFrame.add(buttonPanel, BorderLayout.CENTER);

//Add the number buttons
for (int i=1;i<10;i++)
{
addButton(buttonPanel, String.valueOf(i));
}

JButton addButton = new JButton("+");
addButton.setActionCommand("+");

OperatorAction subAction = new OperatorAction(1);
addButton.addActionListener(subAction);

JButton subButton = new JButton("-");
subButton.setActionCommand("-");

OperatorAction addAction = new OperatorAction(2);
subButton.addActionListener(addAction);
JButton equalsButton = new JButton("=");
equalsButton.setActionCommand("=");
equalsButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
if (!numberCalc.getText().isEmpty())
{
int number = Integer.parseInt(numberCalc.getText());
if (calcOperation == 1)
{
int calculate = currentCalc + number;
numberCalc.setText(Integer.toString(calculate));
}
else if (calcOperation == 2)
{
int calculate = currentCalc - number;
numberCalc.setText(Integer.toString(calculate));
}
}
}
});

buttonPanel.add(addButton);
buttonPanel.add(subButton);
buttonPanel.add(equalsButton);
guiFrame.setVisible(true);
}

//All the buttons are following the same pattern
//so create them all in one place.
private void addButton(Container parent, String name)
{
JButton but = new JButton(name);
but.setActionCommand(name);
but.addActionListener(this);
parent.add(but);
}

//As all the buttons are doing the same thing it's
//easier to make the class implement the ActionListener
//interface and control the button clicks from one place
@Override
public void actionPerformed(ActionEvent event)
{
//get the Action Command text from the button
String action = event.getActionCommand();

//set the text using the Action Command text
numberCalc.setText(action);
}

private class OperatorAction implements ActionListener
{
private int operator;

public OperatorAction(int operation)
{
operator = operation;
}

public void actionPerformed(ActionEvent event)
{
currentCalc = Integer.parseInt(numberCalc.getText());
calcOperation = operator;
}
}
}

kunaljangid2k3: Whattttt????
kunaljangid2k3: So long!!!!
Answered by AparnaSingh11989198
1

Answer:

Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input.

Example :

Enter the numbers:

2

2

Enter the operator (+,-,*,/)

+

The final result:

2.0 + 2.0 = 4.0

Approach Used:

Take two numbers using the Scanner class. The switch case branching is used to execute a particular section.

Using a switch case to evaluate respective operations.

// Java program for simple calculator

import java.io.*;

import java.lang.*;

import java.lang.Math;

import java.util.Scanner;

public class BasicCalculator {

public static void main(String[] args)

{

// stores two numbers

double num1, num2;

// Take input from the user

Scanner sc = new Scanner(System.in);

System.out.println("Enter the numbers");

// take the inputs

num1 = sc.nextDouble();

num2 = sc.nextDouble();

System.out.println("Enter the operator (+,-,*,/)");

char op = sc.next().charAt(0);

double o = 0;

switch (op) {

// case to add two numbers

case '+':

o = num1 + num2;

break;

// case to subtract two numbers

case '-':

o = num1 - num2;

break;

// case to multiply two numbers

case '*':

o = num1 * num2;

break;

// case to divide two numbers

case '/':

o = num1 / num2;

break;

default:

System.out.println("You enter wrong input");

break;

}

System.out.println("The final result:");

System.out.println();

// print the final result

System.out.println(num1 + " " + op + " " + num2

+ " = " + o);

}

}

Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input.

Example :

Enter the numbers:

2

2

Enter the operator (+,-,*,/)

+

The final result:

2.0 + 2.0 = 4.0

Approach Used:

Take two numbers using the Scanner class. The switch case branching is used to execute a particular section.

Using a switch case to evaluate respective operations.

// Java program for simple calculator

import java.io.*;

import java.lang.*;

import java.lang.Math;

import java.util.Scanner;

public class BasicCalculator {

public static void main(String[] args)

{

// stores two numbers

double num1, num2;

// Take input from the user

Scanner sc = new Scanner(System.in);

System.out.println("Enter the numbers");

// take the inputs

num1 = sc.nextDouble();

num2 = sc.nextDouble();

System.out.println("Enter the operator (+,-,*,/)");

char op = sc.next().charAt(0);

double o = 0;

switch (op) {

// case to add two numbers

case '+':

o = num1 + num2;

break;

// case to subtract two numbers

case '-':

o = num1 - num2;

break;

// case to multiply two numbers

case '*':

o = num1 * num2;

break;

// case to divide two numbers

case '/':

o = num1 / num2;

break;

default:

System.out.println("You enter wrong input");

break;

}

System.out.println("The final result:");

System.out.println();

// print the final result

System.out.println(num1 + " " + op + " " + num2

+ " = " + o);

}

}

Similar questions