Advertisement
fahimkamal63

Calculator

Aug 13th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.00 KB | None | 0 0
  1. /**
  2.  * Calculator 2.0 (Java Swing)
  3.  * Date : 13.08.19
  4.  */
  5. package Swing;
  6. import javax.swing.*;
  7. import java.awt.event.*;
  8. import java.awt.font.*;
  9. import java.awt.Font;
  10.  
  11. public class Calculator implements ActionListener {
  12.     JTextField tf;
  13.     JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
  14.     JButton plus, minus, mult, div, equal, point, sqrt, power, reset;
  15.     String display = "0", temporary = "0";
  16.     double dis = 0, temp = 0;
  17.     int operation = 0;
  18.     boolean pointonof = false;
  19.     boolean poweronof = false;
  20.     Font f1 = new Font("Consolas", Font.BOLD, 40);
  21.     Calculator(){
  22.         JFrame f = new JFrame("Calculator");
  23.         tf = new JTextField("0");
  24.         tf.setBounds(50, 50, 300, 50);
  25.         tf.setHorizontalAlignment(JTextField.RIGHT);
  26.         tf.setFont(f1);
  27.         f.add(tf);
  28.        
  29.         b7 = new JButton("7");
  30.         b7.setBounds(50, 110, 50, 50);
  31.         b7.addActionListener(this);
  32.         f.add(b7);
  33.        
  34.         b8 = new JButton("8");
  35.         b8.setBounds(110, 110, 50, 50);
  36.         b8.addActionListener(this);
  37.         f.add(b8);
  38.        
  39.         b9 = new JButton("9");
  40.         b9.setBounds(170, 110, 50, 50);
  41.         b9.addActionListener(this);
  42.         f.add(b9);
  43.        
  44.         div = new JButton("/");
  45.         div.setBounds(230, 110, 50, 50);
  46.         div.addActionListener(this);
  47.         f.add(div);
  48.        
  49.         sqrt = new JButton("Sqrt");
  50.         sqrt.setBounds(290, 110, 60, 50);
  51.         sqrt.addActionListener(this);
  52.         f.add(sqrt);
  53.        
  54.         b4 = new JButton("4");
  55.         b4.setBounds(50, 170, 50, 50);
  56.         b4.addActionListener(this);
  57.         f.add(b4);
  58.        
  59.         b5 = new JButton("5");
  60.         b5.setBounds(110, 170, 50, 50);
  61.         b5.addActionListener(this);
  62.         f.add(b5);
  63.        
  64.         b6 = new JButton("6");
  65.         b6.setBounds(170, 170, 50, 50);
  66.         b6.addActionListener(this);
  67.         f.add(b6);
  68.        
  69.         mult = new JButton("X");
  70.         mult.setBounds(230, 170, 50, 50);
  71.         mult.addActionListener(this);
  72.         f.add(mult);
  73.        
  74.         power = new JButton("X^Y");
  75.         power.setBounds(290, 170, 60, 50);
  76.         power.addActionListener(this);
  77.         f.add(power);
  78.        
  79.         b1 = new JButton("1");
  80.         b1.setBounds(50, 230, 50, 50);
  81.         b1.addActionListener(this);
  82.         f.add(b1);
  83.        
  84.         b2 = new JButton("2");
  85.         b2.setBounds(110, 230, 50, 50);
  86.         b2.addActionListener(this);
  87.         f.add(b2);
  88.        
  89.         b3 = new JButton("3");
  90.         b3.setBounds(170, 230, 50, 50);
  91.         b3.addActionListener(this);
  92.         f.add(b3);
  93.        
  94.         minus = new JButton("-");
  95.         minus.setBounds(230, 230, 50, 50);
  96.         minus.addActionListener(this);
  97.         f.add(minus);
  98.        
  99.         reset = new JButton("RC");
  100.         reset.setBounds(290, 230, 60, 50);
  101.         reset.addActionListener(this);
  102.         f.add(reset);
  103.        
  104.         b0 = new JButton("0");
  105.         b0.setBounds(50, 290, 50, 50);
  106.         b0.addActionListener(this);
  107.         f.add(b0);
  108.        
  109.         point = new JButton(".");
  110.         point.setBounds(110, 290, 50, 50);
  111.         point.addActionListener(this);
  112.         f.add(point);
  113.        
  114.         equal = new JButton("=");
  115.         equal.setBounds(170, 290, 50, 50);
  116.         equal.addActionListener(this);
  117.         f.add(equal);
  118.        
  119.         plus = new JButton("+");
  120.         plus.setBounds(230, 290, 50, 50);
  121.         plus.addActionListener(this);
  122.         f.add(plus);
  123.        
  124.         f.setSize(420,400);
  125.         f.setLayout(null);
  126.         f.setVisible(true);
  127.     }
  128.     public void actionPerformed(ActionEvent e){
  129.         if(e.getSource() == reset){
  130.             display = "0"; temporary = "0";
  131.             dis = 0; temp = 0;
  132.             operation = 0;
  133.             pointonof = false;
  134.             poweronof = false;
  135.             tf.setText(display);
  136.         }
  137.         else if(e.getSource() == point){
  138.             if(!pointonof){
  139.                 pointonof = true;
  140.                 display = display + '.';
  141.                 tf.setText(display);
  142.             }
  143.         }
  144.         else if(e.getSource() == b0){
  145.             if(display != "0"){
  146.                 display = display + '0';
  147.             }
  148.             dis = Double.parseDouble(display);
  149.             tf.setText(display);
  150.         }
  151.         else if(e.getSource() == b1){
  152.             if(display == "0"){ display = "1"; }
  153.             else display = display + '1';
  154.             dis = Double.parseDouble(display);
  155.             tf.setText(display);
  156.         }
  157.         else if(e.getSource() == b2){
  158.             if(display == "0"){ display = "2"; }
  159.             else display = display + '2';
  160.             dis = Double.parseDouble(display);
  161.             tf.setText(display);
  162.         }
  163.         else if(e.getSource() == b3){
  164.             if(display == "0"){ display = "3"; }
  165.             else display = display + '3';
  166.             dis = Double.parseDouble(display);
  167.             tf.setText(display);
  168.         }
  169.         else if(e.getSource() == b4){
  170.             if(display == "0"){ display = "4"; }
  171.             else display = display + '4';
  172.             dis = Double.parseDouble(display);
  173.             tf.setText(display);
  174.         }
  175.         else if(e.getSource() == b5){
  176.             if(display == "0"){ display = "5"; }
  177.             else display = display + '5';
  178.             dis = Double.parseDouble(display);
  179.             tf.setText(display);
  180.         }
  181.         else if(e.getSource() == b6){
  182.             if(display == "0"){ display = "6"; }
  183.             else display = display + '6';
  184.             dis = Double.parseDouble(display);
  185.             tf.setText(display);
  186.         }
  187.         else if(e.getSource() == b7){
  188.             if(display == "0"){ display = "7"; }
  189.             else display = display + '7';
  190.             dis = Double.parseDouble(display);
  191.             tf.setText(display);
  192.         }
  193.         else if(e.getSource() == b8){
  194.             if(display == "0"){ display = "8"; }
  195.             else display = display + '8';
  196.             dis = Double.parseDouble(display);
  197.             tf.setText(display);
  198.         }
  199.         else if(e.getSource() == b9){
  200.             if(display == "0"){ display = "9"; }
  201.             else display = display + '9';
  202.             dis = Double.parseDouble(display);
  203.             tf.setText(display);
  204.         }
  205.         else if(e.getSource() == equal){
  206.             if(operation == 1){
  207.                 temp += dis;
  208.                 dis = 0;
  209.                 display = String.valueOf(temp);
  210.             }
  211.             else if(operation == 2){
  212.                 temp -= dis;
  213.                 dis = 0;
  214.                 display = String.valueOf(temp);
  215.             }
  216.             else if(operation == 3){
  217.                 temp *= dis;
  218.                 dis = 0;
  219.                 display = String.valueOf(temp);
  220.             }
  221.             else if(operation == 4){
  222.                 temp /= dis;
  223.                 dis = 0;
  224.                 display = String.valueOf(temp);
  225.             }
  226.             tf.setText(display);
  227.             display = "0";
  228.             operation = 0;
  229.             pointonof = false;
  230.         }
  231.         else if(e.getSource() == plus){
  232.             if(operation == 1){
  233.                 temp += dis;
  234.                 dis = 0;
  235.                 display = String.valueOf(temp);
  236.             }
  237.             else if(operation == 2){
  238.                 temp -= dis;
  239.                 dis = 0;
  240.                 display = String.valueOf(temp);
  241.             }
  242.             else if(operation == 3){
  243.                 temp *= dis;
  244.                 dis = 0;
  245.                 display = String.valueOf(temp);
  246.             }
  247.             else if(operation == 4){
  248.                 temp /= dis;
  249.                 dis = 0;
  250.                 display = String.valueOf(temp);
  251.             }
  252.             else{
  253.                 temp = Double.parseDouble(display);
  254.             }
  255.             operation = 1;
  256.             tf.setText(display);
  257.             display = "0";
  258.             pointonof = false;
  259.         }
  260.         else if(e.getSource() == minus){
  261.             if(operation == 1){
  262.                 temp += dis;
  263.                 dis = 0;
  264.                 display = String.valueOf(temp);
  265.             }
  266.             else if(operation == 2){
  267.                 temp -= dis;
  268.                 dis = 0;
  269.                 display = String.valueOf(temp);
  270.             }
  271.             else if(operation == 3){
  272.                 temp *= dis;
  273.                 dis = 0;
  274.                 display = String.valueOf(temp);
  275.             }
  276.             else if(operation == 4){
  277.                 temp /= dis;
  278.                 dis = 0;
  279.                 display = String.valueOf(temp);
  280.             }
  281.             else{
  282.                 temp = Double.parseDouble(display);
  283.             }
  284.             operation = 2;
  285.             tf.setText(display);
  286.             display = "0";
  287.             pointonof = false;
  288.         }
  289.         else if(e.getSource() == mult){
  290.             if(operation == 1){
  291.                 temp += dis;
  292.                 dis = 0;
  293.                 display = String.valueOf(temp);
  294.             }
  295.             else if(operation == 2){
  296.                 temp -= dis;
  297.                 dis = 0;
  298.                 display = String.valueOf(temp);
  299.             }
  300.             else if(operation == 3){
  301.                 temp *= dis;
  302.                 dis = 0;
  303.                 display = String.valueOf(temp);
  304.             }
  305.             else if(operation == 4){
  306.                 temp /= dis;
  307.                 dis = 0;
  308.                 display = String.valueOf(temp);
  309.             }
  310.             else{
  311.                 temp = Double.parseDouble(display);
  312.             }
  313.             operation = 3;
  314.             tf.setText(display);
  315.             display = "0";
  316.             pointonof = false;
  317.         }
  318.         else if(e.getSource() == div){
  319.             if(operation == 1){
  320.                 temp += dis;
  321.                 dis = 0;
  322.                 display = String.valueOf(temp);
  323.             }
  324.             else if(operation == 2){
  325.                 temp -= dis;
  326.                 dis = 0;
  327.                 display = String.valueOf(temp);
  328.             }
  329.             else if(operation == 3){
  330.                 temp *= dis;
  331.                 dis = 0;
  332.                 display = String.valueOf(temp);
  333.             }
  334.             else if(operation == 4){
  335.                 temp /= dis;
  336.                 dis = 0;
  337.                 display = String.valueOf(temp);
  338.             }
  339.             else{
  340.                 temp = Double.parseDouble(display);
  341.             }
  342.             operation = 4;
  343.             tf.setText(display);
  344.             display = "0";
  345.             pointonof = false;
  346.         }
  347.         else if(e.getSource() == sqrt){
  348.             if(operation == 0){
  349.                 dis = Math.sqrt(dis);
  350.                 display = String.valueOf(dis);
  351.                 tf.setText(display);                
  352.             }
  353.             else{
  354.                 display = "ERROR";
  355.                 tf.setText(display);
  356.             }
  357.             display = "0";
  358.             //dis = 0;
  359.             pointonof = false;
  360.             operation = 0;
  361.         }
  362.         else if(e.getSource() == power){
  363.             if(operation == 0){
  364.                 if(poweronof){
  365.                     dis = Math.pow(temp, dis);
  366.                     display = String.valueOf(dis);
  367.                     tf.setText(display);
  368.                     dis = 0;
  369.                     display = "0";
  370.                      //dis = 0;
  371.                     pointonof = false;
  372.                     operation = 0;
  373.                     pointonof = false;
  374.                 }
  375.                 else{
  376.                     poweronof = true;
  377.                     temp = dis;
  378.                     dis = 0;
  379.                     display = "0";
  380.                 }
  381.             }
  382.             else{
  383.                 display = "ERROR";
  384.                 tf.setText(display);
  385.                 display = "0";
  386.                 //dis = 0;
  387.                 pointonof = false;
  388.                 operation = 0;
  389.                
  390.             }
  391.         }
  392.     }
  393.     public static void main(String[] args){
  394.         new Calculator();
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement