Advertisement
Kulas_Code20

cal

Nov 19th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. package com.midtermexam;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.*;
  8.  
  9. public class Calculator extends JFrame implements ActionListener {
  10.     JButton b10, b11, b12, b13, b14, b15;
  11.     JButton b[] = new JButton[10];
  12.     int i, r, n1, n2;
  13.     JTextField res;
  14.     char op;
  15.  
  16.     public Calculator() {
  17.         super("Calulator");
  18.         setLayout(new BorderLayout());
  19.         JPanel p = new JPanel();
  20.         p.setLayout(new GridLayout(4, 4));
  21.         for (int i = 0; i <= 9; i++) {
  22.             b[i] = new JButton(i + "");
  23.             p.add(b[i]);
  24.             b[i].addActionListener(this);
  25.         }
  26.         b10 = new JButton("+");
  27.         p.add(b10);
  28.         b10.addActionListener(this);
  29.  
  30.         b11 = new JButton("-");
  31.         p.add(b11);
  32.         b11.addActionListener(this);
  33.  
  34.         b12 = new JButton("*");
  35.         p.add(b12);
  36.         b12.addActionListener(this);
  37.  
  38.         b13 = new JButton("/");
  39.         p.add(b13);
  40.         b13.addActionListener(this);
  41.  
  42.         b14 = new JButton("=");
  43.         p.add(b14);
  44.         b14.addActionListener(this);
  45.  
  46.         b15 = new JButton("C");
  47.         p.add(b15);
  48.         b15.addActionListener(this);
  49.  
  50.         res = new JTextField(10);
  51.         add(p, BorderLayout.CENTER);
  52.         add(res, BorderLayout.NORTH);
  53.         setVisible(true);
  54.         setSize(200, 200);
  55.     }
  56.  
  57.     public void actionPerformed(ActionEvent ae) {
  58.         JButton pb = (JButton) ae.getSource();
  59.         if (pb == b15) {
  60.             r = n1 = n2 = 0;
  61.             res.setText("");
  62.         } else if (pb == b14) {
  63.             n2 = Integer.parseInt(res.getText());
  64.             eval();
  65.             res.setText("" + r);
  66.         }
  67.  
  68.         else {
  69.             boolean opf = false;
  70.             if (pb == b10) {
  71.                 op = '+';
  72.                 opf = true;
  73.             }
  74.             if (pb == b11) {
  75.                 op = '-';
  76.                 opf = true;
  77.             }
  78.             if (pb == b12) {
  79.                 op = '*';
  80.                 opf = true;
  81.             }
  82.             if (pb == b13) {
  83.                 op = '/';
  84.                 opf = true;
  85.             }
  86.  
  87.             if (opf == false) {
  88.                 for (i = 0; i < 10; i++) {
  89.                     if (pb == b[i]) {
  90.                         String t = res.getText();
  91.                         t += i;
  92.                         res.setText(t);
  93.                     }
  94.                 }
  95.             } else {
  96.                 n1 = Integer.parseInt(res.getText());
  97.                 res.setText("");
  98.             }
  99.         }
  100.     }
  101.  
  102.     int eval() {
  103.         switch (op) {
  104.         case '+':
  105.             r = n1 + n2;
  106.             break;
  107.         case '-':
  108.             r = n1 - n2;
  109.             break;
  110.         case '*':
  111.             r = n1 * n2;
  112.             break;
  113.         case '/':
  114.             r = n1 / n2;
  115.             break;
  116.  
  117.         }
  118.         return 0;
  119.     }
  120.  
  121.     public static void main(String arg[]) {
  122.         new Calculator();
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement