Sunday 8 January 2017

Scientific Calculator in JAVA with GUI

Code:

import java.lang.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;

public class Calculator implements ActionListener{

JFrame frmCalculator;
double result = 0;
double num1 = 0;
double num2 = 0;
String TempStr;
String operation,op;
JTextField display;

/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Calculator window = new Calculator();
window.frmCalculator.setVisible(true);
} catch (Exception e) {
System.out.println("Error");
}
}

/**
* Create the application.
*/
public Calculator() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmCalculator = new JFrame();
frmCalculator.setResizable(false);
frmCalculator.setTitle("Calculator");
frmCalculator.setSize(502, 315);
frmCalculator.setLocationRelativeTo(null);
frmCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCalculator.getContentPane().setLayout(null);
JPanel dispPanel = new JPanel();
dispPanel.setBounds(0, 0, 496, 70);
frmCalculator.getContentPane().add(dispPanel);
dispPanel.setLayout(null);
display = new JTextField();
display.setBackground(Color.WHITE);
display.setFont(new Font("Tahoma", Font.BOLD, 24));
display.setBounds(0, 22, 496, 48);
dispPanel.add(display);
display.setColumns(10);
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
JPanel controlPanel = new JPanel();
controlPanel.setBounds(0, 69, 496, 217);
frmCalculator.getContentPane().add(controlPanel);
controlPanel.setLayout(null);
JButton btnSin = new JButton("sin");
btnSin.setBounds(290, 139, 60, 30);
controlPanel.add(btnSin);
btnSin.addActionListener(this);
JButton btnCos = new JButton("cos");
btnCos.setBounds(290, 94, 60, 30);
controlPanel.add(btnCos);
btnCos.addActionListener(this);
JButton btnTan = new JButton("tan");
btnTan.setBounds(290, 53, 60, 30);
controlPanel.add(btnTan);
btnTan.addActionListener(this);
JButton btn9 = new JButton("9");
btn9.setFont(new Font("Tahoma", Font.BOLD, 12));
btn9.setBounds(150, 11, 60, 30);
controlPanel.add(btn9);
btn9.addActionListener(this);
JButton btn8 = new JButton("8");
btn8.setFont(new Font("Tahoma", Font.BOLD, 12));
btn8.setBounds(80, 11, 60, 30);
controlPanel.add(btn8);
btn8.addActionListener(this);
JButton btn7 = new JButton("7");
btn7.setFont(new Font("Tahoma", Font.BOLD, 12));
btn7.setBounds(10, 11, 60, 30);
controlPanel.add(btn7);
btn7.addActionListener(this);
JButton btn6 = new JButton("6");
btn6.setFont(new Font("Tahoma", Font.BOLD, 12));
btn6.setBounds(150, 52, 60, 30);
controlPanel.add(btn6);
btn6.addActionListener(this);
JButton btn5 = new JButton("5");
btn5.setFont(new Font("Tahoma", Font.BOLD, 12));
btn5.setBounds(80, 52, 60, 30);
controlPanel.add(btn5);
btn5.addActionListener(this);
JButton btn4 = new JButton("4");
btn4.setFont(new Font("Tahoma", Font.BOLD, 12));
btn4.setBounds(10, 52, 60, 30);
controlPanel.add(btn4);
btn4.addActionListener(this);
JButton btn3 = new JButton("3");
btn3.setFont(new Font("Tahoma", Font.BOLD, 12));
btn3.setBounds(150, 97, 60, 30);
controlPanel.add(btn3);
btn3.addActionListener(this);
JButton btn2 = new JButton("2");
btn2.setFont(new Font("Tahoma", Font.BOLD, 12));
btn2.setBounds(80, 97, 60, 30);
controlPanel.add(btn2);
btn2.addActionListener(this);
JButton btn1 = new JButton("1");
btn1.setFont(new Font("Tahoma", Font.BOLD, 12));
btn1.setBounds(10, 97, 60, 30);
controlPanel.add(btn1);
btn1.addActionListener(this);
JButton btnPlMi = new JButton("±");
btnPlMi.setFont(new Font("Tahoma", Font.BOLD, 12));
btnPlMi.setBounds(150, 138, 60, 30);
controlPanel.add(btnPlMi);
btnPlMi.addActionListener(this);
JButton btnDot = new JButton(".");
btnDot.setFont(new Font("Tahoma", Font.BOLD, 18));
btnDot.setBounds(80, 138, 60, 30);
controlPanel.add(btnDot);
btnDot.addActionListener(this);
JButton btn0 = new JButton("0");
btn0.setFont(new Font("Tahoma", Font.BOLD, 12));
btn0.setBounds(10, 138, 60, 30);
controlPanel.add(btn0);
btn0.addActionListener(this);
JButton btnLog = new JButton("log10");
btnLog.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnLog.setBounds(430, 11, 60, 30);
controlPanel.add(btnLog);
btnLog.addActionListener(this);
JButton btnCrt = new JButton("3v ");
btnCrt.setBounds(430, 138, 60, 30);
controlPanel.add(btnCrt);
btnCrt.addActionListener(this);
JButton btnSrt = new JButton("v ");
btnSrt.setBounds(430, 93, 60, 30);
controlPanel.add(btnSrt);
btnSrt.addActionListener(this);
JButton btnSub = new JButton("-");
btnSub.setFont(new Font("Tahoma", Font.BOLD, 18));
btnSub.setBounds(220, 11, 60, 30);
controlPanel.add(btnSub);
btnSub.addActionListener(this);
JButton btnBac = new JButton("<--");
btnBac.setFont(new Font("Tahoma", Font.BOLD, 12));
btnBac.setBounds(290, 11, 60, 30);
controlPanel.add(btnBac);
btnBac.addActionListener(this);
JButton btnSqr = new JButton("x2");
btnSqr.setBounds(360, 93, 60, 30);
controlPanel.add(btnSqr);
btnSqr.addActionListener(this);
JButton btnAdd = new JButton("+");
btnAdd.setFont(new Font("Tahoma", Font.BOLD, 12));
btnAdd.setBounds(220, 138, 60, 70);
controlPanel.add(btnAdd);
btnAdd.addActionListener(this);
JButton btnCub = new JButton("x3");
btnCub.setBounds(360, 138, 60, 30);
controlPanel.add(btnCub);
btnCub.addActionListener(this);
JButton btnMul = new JButton("X");
btnMul.setFont(new Font("Tahoma", Font.BOLD, 12));
btnMul.setBounds(220, 52, 60, 30);
controlPanel.add(btnMul);
btnMul.addActionListener(this);
JButton btnEq = new JButton("=");
btnEq.setFont(new Font("Tahoma", Font.BOLD, 18));
btnEq.setBounds(360, 179, 130, 30);
controlPanel.add(btnEq);
btnEq.addActionListener(this);
JButton btnLn = new JButton("ln");
btnLn.setBounds(360, 11, 60, 30);
controlPanel.add(btnLn);
btnLn.addActionListener(this);
JButton btnDiv = new JButton("/");
btnDiv.setFont(new Font("Tahoma", Font.BOLD, 12));
btnDiv.setBounds(220, 93, 60, 30);
controlPanel.add(btnDiv);
btnDiv.addActionListener(this);
JButton btnFact = new JButton("n!");
btnFact.setBounds(430, 53, 60, 30);
controlPanel.add(btnFact);
btnFact.addActionListener(this);

JButton btnPer = new JButton("%");
btnPer.setBounds(290, 178, 60, 30);
controlPanel.add(btnPer);
btnPer.addActionListener(this);
JButton btnExp = new JButton("EXP");
btnExp.setBounds(150, 178, 60, 30);
controlPanel.add(btnExp);
btnExp.addActionListener(this);
JButton btnClr = new JButton("C");
btnClr.setFont(new Font("Tahoma", Font.BOLD, 12));
btnClr.setBounds(10, 179, 130, 30);
controlPanel.add(btnClr);
btnClr.addActionListener(this);
JButton btnRec = new JButton("1/x");
btnRec.setBounds(360, 52, 60, 30);
controlPanel.add(btnRec);
btnRec.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
String actionCom = e.getActionCommand();
if(actionCom.equals("0")||actionCom.equals("1")||actionCom.equals("2")||actionCom.equals("3")||actionCom.equals("4")||actionCom.equals("5")||actionCom.equals("6")||actionCom.equals("7")||actionCom.equals("8")||actionCom.equals("9")||actionCom.equals(".")){
if (TempStr != null){
TempStr = TempStr + actionCom;
}else if(actionCom.equals(".")){
TempStr = "0"+actionCom;
}else{
TempStr = actionCom;
}
display.setText(TempStr);
}else if(actionCom.equals("/")||actionCom.equals("X")||actionCom.equals("+")||actionCom.equals("-")){
operation = actionCom;
if(op==null)
{
op=operation;
}
switch (op){
case "+":
if (num1==0)
{
num1 = Double.parseDouble(TempStr);
display.setText(null);
}
else {
num2= Double.parseDouble(TempStr);
num1+=num2;
display.setText(null);
num2=0;
}
op=operation;
operation=null;
break;
case "-":
if (num1==0)
{
num1 = Double.parseDouble(TempStr);
display.setText(null);
op=operation;
operation=null;
}
else {
num2= Double.parseDouble(TempStr);
num1=num1-num2;
display.setText(null);
op=operation;
operation=null;
num2=0;
}
break;
case "/":
if (num1==0)
{
num1 = Double.parseDouble(TempStr);
display.setText(null);
op=operation;
operation=null;
}
else {
num2= Double.parseDouble(TempStr);
num1=num1/num2;
display.setText(null);
op=operation;
operation=null;
num2=0;
}
break;
case "X":
if (num1==0)
{
num1 = Double.parseDouble(TempStr);
display.setText(null);
op=operation;
operation=null;
}
else {
num2= Double.parseDouble(TempStr);
num1=num1*num2;
display.setText(null);
op=operation;
operation=null;
num2=0;
}
break;
}
TempStr = null;
}else if(actionCom.equals("v ")||actionCom.equals("3v ")){
switch(actionCom){
case "v ":
num1 = Double.parseDouble(TempStr);
TempStr = null;
double temp = Math.sqrt(num1);
display.setText(""+temp);
break;
case "3v ":
num1 = Double.parseDouble(TempStr);
TempStr = null;
double temp1 = Math.cbrt(num1);
display.setText(""+temp1);
break;
}
}else if(actionCom.equals("x3")||actionCom.equals("x2")){
switch(actionCom){
case "x3":
num1 = Double.parseDouble(TempStr);
TempStr = null;
double temp = num1*num1*num1;
display.setText(""+temp);
break;
case "x2":
num1 = Double.parseDouble(TempStr);
TempStr = null;
double temp1 = num1*num1;
display.setText(""+temp1);
break;
}
}else if(actionCom.equals("n!")){
num1 = Double.parseDouble(TempStr);
TempStr = null;
double temp = 1;
for (int i = 1; i <= num1; i++) {
temp = temp*i;
display.setText(""+temp);
}
}else if(actionCom.equals("ln")||actionCom.equals("log10")){
switch(actionCom){
case "ln":
if(TempStr != null){
num1 = Double.parseDouble(TempStr);
TempStr = null;
String temp = "" + Math.log(num1);
display.setText(temp);
}else{
display.setText(null);
TempStr = null;
}
break;
case "log10":
if(TempStr != null){
num1 = Double.parseDouble(TempStr);
TempStr = null;
String temp = "" + Math.log10(num1);
display.setText(temp);
}else{
display.setText(null);
TempStr = null;
}
break;
}
}else if(actionCom.equals("C")||actionCom.equals("=")){
switch(actionCom){
case "C":
result = 0;
num1 = 0;
num2 = 0;
TempStr = null;
operation = null;
display.setText(null);
break;
case "=":
num2 = Double.parseDouble(TempStr);
TempStr = null;
if (op.equals(null)){
display.setText(""+num2);
}else{
switch(op){
case "/":
if(num2 != 0){
result = num1/num2;
display.setText(""+result);
operation = null;
}else{
display.setText("Error! Can't Divide by zero");
}
break;
case "X":
result = num1 * num2;
display.setText(""+result);
operation = null;
break;
case "+":
result = num1 + num2;
display.setText(""+result);
operation = null;
break;
case "-":
result = num1 - num2;
display.setText(""+result);
operation = null;
break;
}
}
break;
}
}else if(actionCom.equals("sin")||actionCom.equals("cos")||actionCom.equals("tan")){
num1 = Double.parseDouble(TempStr);
TempStr = null;
switch(actionCom){
case "sin":
result = Math.sin(num1);
display.setText(""+result);
break;
case "cos":
result = Math.cos(num1);
display.setText(""+result);
break;
case "tan":
result = Math.tan(num1);
display.setText(""+result);
break;
}
}else if(actionCom.equals("<--")){
if (TempStr.length() > 0) {
TempStr = TempStr.substring(0, TempStr.length()-1);
display.setText(TempStr);
}else{
display.setText(TempStr);
}
}else if (actionCom.equals("±")){
if(TempStr != null){
TempStr = "-"+TempStr;
display.setText(TempStr);
}
}else if (actionCom.equals("1/x")){
if(TempStr != null){
num1 = Double.parseDouble(TempStr);
result = 1/num1;
display.setText(""+result);
}
}else if (actionCom.equals("EXP")){
if(TempStr != null){
num1 = Double.parseDouble(TempStr);
result = Math.exp(num1);
display.setText(""+result);
}
}else if (actionCom.equals("%")){
if(TempStr != null){
num2= Double.parseDouble(TempStr);
result = (num2/num1)*100;
display.setText(""+result);
}
}
}
}


Output:


No comments:

Post a Comment