ghorardim

Calculator

Dec 10th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.14 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class MyCalculator extends Frame {
  6.  
  7.     public boolean setClear = true;
  8.     double number, memValue;
  9.     char op;
  10.     String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "."};
  11.     String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "="};
  12.     String memoryButtonText[] = {"MC", "MR", "MS", "M+"};
  13.     String specialButtonText[] = {"Backspc", "C", "CE"};
  14.     MyDigitButton digitButton[] = new MyDigitButton[digitButtonText.length];
  15.     MyOperatorButton operatorButton[] = new MyOperatorButton[operatorButtonText.length];
  16.     MyMemoryButton memoryButton[] = new MyMemoryButton[memoryButtonText.length];
  17.     MySpecialButton specialButton[] = new MySpecialButton[specialButtonText.length];
  18.     Label displayLabel = new Label("0", Label.RIGHT);
  19.     Label memLabel = new Label(" ", Label.RIGHT);
  20.     final int FRAME_WIDTH = 325, FRAME_HEIGHT = 325;
  21.     final int HEIGHT = 30, WIDTH = 30, H_SPACE = 10, V_SPACE = 10;
  22.     final int TOPX = 30, TOPY = 50;
  23.  
  24.     MyCalculator(String frameText) {
  25.         super(frameText);
  26.  
  27.         int tempX = TOPX, y = TOPY;
  28.         displayLabel.setBounds(tempX, y, 240, HEIGHT);
  29.         displayLabel.setBackground(Color.BLUE);
  30.         displayLabel.setForeground(Color.WHITE);
  31.         add(displayLabel);
  32.  
  33.         memLabel.setBounds(TOPX, TOPY + HEIGHT + V_SPACE, WIDTH, HEIGHT);
  34.         memLabel.setBackground(Color.red);
  35.         memLabel.setForeground(Color.WHITE);
  36.         add(memLabel);
  37.  
  38.  
  39.         tempX = TOPX;
  40.         y = TOPY + 2 * (HEIGHT + V_SPACE);
  41.         for (int i = 0; i < memoryButton.length; i++) {
  42.             memoryButton[i] = new MyMemoryButton(tempX, y, WIDTH, HEIGHT, memoryButtonText[i], this);
  43.             memoryButton[i].setForeground(Color.RED);
  44.             y += HEIGHT + V_SPACE;
  45.         }
  46.         tempX = TOPX + 1 * (WIDTH + H_SPACE);
  47.         y = TOPY + 1 * (HEIGHT + V_SPACE);
  48.         for (int i = 0; i < specialButton.length; i++) {
  49.             specialButton[i] = new MySpecialButton(tempX, y, WIDTH * 2, HEIGHT, specialButtonText[i], this);
  50.             specialButton[i].setForeground(Color.RED);
  51.             tempX = tempX + 2 * WIDTH + H_SPACE;
  52.         }
  53.         int digitX = TOPX + WIDTH + H_SPACE;
  54.         int digitY = TOPY + 2 * (HEIGHT + V_SPACE);
  55.         tempX = digitX;
  56.         y = digitY;
  57.         for (int i = 0; i < digitButton.length; i++) {
  58.             digitButton[i] = new MyDigitButton(tempX, y, WIDTH, HEIGHT, digitButtonText[i], this);
  59.             digitButton[i].setForeground(Color.BLUE);
  60.             tempX += WIDTH + H_SPACE;
  61.             if ((i + 1) % 3 == 0) {
  62.                 tempX = digitX;
  63.                 y += HEIGHT + V_SPACE;
  64.             }
  65.         }
  66.  
  67.         int opsX = digitX + 2 * (WIDTH + H_SPACE) + H_SPACE;
  68.         int opsY = digitY;
  69.         tempX = opsX;
  70.         y = opsY;
  71.         for (int i = 0; i < operatorButton.length; i++) {
  72.             tempX += WIDTH + H_SPACE;
  73.             operatorButton[i] = new MyOperatorButton(tempX, y, WIDTH, HEIGHT, operatorButtonText[i], this);
  74.             operatorButton[i].setForeground(Color.RED);
  75.             if ((i + 1) % 2 == 0) {
  76.                 tempX = opsX;
  77.                 y += HEIGHT + V_SPACE;
  78.             }
  79.         }
  80.  
  81.         addWindowListener(new WindowAdapter() {
  82.             public void windowClosing(WindowEvent ev) {
  83.                 System.exit(0);
  84.             }
  85.         });
  86.  
  87.         setLayout(null);
  88.         setSize(FRAME_WIDTH, FRAME_HEIGHT);
  89.         setVisible(true);
  90.     }
  91.  
  92.     static String getFormattedText(double temp) {
  93.         String resText = "" + temp;
  94.         if (resText.lastIndexOf(".0") > 0) {
  95.             resText = resText.substring(0, resText.length() - 2);
  96. // System.out.println("hiiiiiii\n");
  97.         }
  98.         return resText;
  99.     }
  100.  
  101.     public static void main(String[] args) {
  102.         new MyCalculator("Calculator ( Jabir - Bhaggo)");
  103.     }
  104. }
  105.  
  106. class MyDigitButton extends Button implements ActionListener {
  107.  
  108.     MyCalculator cl;
  109.  
  110.     MyDigitButton(int x, int y, int width, int height, String cap, MyCalculator clc) {
  111.         super(cap);
  112.         setBounds(x, y, width, height);
  113.         this.cl = clc;
  114.         this.cl.add(this);
  115.         addActionListener(this);
  116.     }
  117.  
  118.     static boolean isInString(String s, char ch) {
  119.         for (int i = 0; i < s.length(); i++) {
  120.             if (s.charAt(i) == ch) {
  121.                 return true;
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.  
  127.     public void actionPerformed(ActionEvent ev) {
  128.         String tempText = ((MyDigitButton) ev.getSource()).getLabel();
  129.  
  130.         if (tempText.equals(".")) {
  131.             if (cl.setClear) {
  132.                 cl.displayLabel.setText("0.");
  133.                 cl.setClear = false;
  134.             } else if (!isInString(cl.displayLabel.getText(), '.')) {
  135.                 cl.displayLabel.setText(cl.displayLabel.getText() + ".");
  136.             }
  137.             return;
  138.         }
  139.  
  140.         int index = 0;
  141.         try {
  142.             index = Integer.parseInt(tempText);
  143.         } catch (NumberFormatException e) {
  144.             return;
  145.         }
  146.         if (index == 0 && cl.displayLabel.getText().equals("0")) {
  147.             return;
  148.         }
  149.  
  150.         if (cl.setClear) {
  151.             cl.displayLabel.setText("" + index);
  152.             cl.setClear = false;
  153.         } else {
  154.             cl.displayLabel.setText(cl.displayLabel.getText() + index);
  155.         }
  156.     }
  157. }
  158.  
  159. class MyOperatorButton extends Button implements ActionListener {
  160.  
  161.     MyCalculator cl;
  162.  
  163.     MyOperatorButton(int x, int y, int width, int height, String cap, MyCalculator clc) {
  164.         super(cap);
  165.         setBounds(x, y, width, height);
  166.         this.cl = clc;
  167.         this.cl.add(this);
  168.         addActionListener(this);
  169.     }
  170.  
  171.     public void actionPerformed(ActionEvent ev) {
  172.         String opText = ((MyOperatorButton) ev.getSource()).getLabel();
  173.  
  174.         cl.setClear = true;
  175.         double temp = Double.parseDouble(cl.displayLabel.getText());
  176.  
  177.         if (opText.equals("1/x")) {
  178.             try {
  179.                 double tempd = 1 / (double) temp;
  180.                 cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));
  181.             } catch (ArithmeticException excp) {
  182.                 cl.displayLabel.setText("Divide by 0.");
  183.             }
  184.             return;
  185.         }
  186.         if (opText.equals("sqrt")) {
  187.             try {
  188.                 double tempd = Math.sqrt(temp);
  189.                 cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));
  190.             } catch (ArithmeticException excp) {
  191.                 cl.displayLabel.setText("Divide by 0.");
  192.             }
  193.             return;
  194.         }
  195.         if (!opText.equals("=")) {
  196.             cl.number = temp;
  197.             cl.op = opText.charAt(0);
  198.             return;
  199.         }
  200.  
  201.         switch (cl.op) {
  202.             case '+':
  203.                 temp += cl.number;
  204.                 break;
  205.             case '-':
  206.                 temp = cl.number - temp;
  207.                 break;
  208.             case '*':
  209.                 temp *= cl.number;
  210.                 break;
  211.             case '%':
  212.                 try {
  213.                     temp = cl.number % temp;
  214.                 } catch (ArithmeticException excp) {
  215.                     cl.displayLabel.setText("Divide by 0.");
  216.                     return;
  217.                 }
  218.                 break;
  219.             case '/':
  220.                 try {
  221.                     temp = cl.number / temp;
  222.                 } catch (ArithmeticException excp) {
  223.                     cl.displayLabel.setText("Divide by 0.");
  224.                     return;
  225.                 }
  226.                 break;
  227.         }
  228.  
  229.         cl.displayLabel.setText(MyCalculator.getFormattedText(temp));
  230.     }
  231. }
  232.  
  233. class MyMemoryButton extends Button implements ActionListener {
  234.  
  235.     MyCalculator cl;
  236.  
  237.     MyMemoryButton(int x, int y, int width, int height, String cap, MyCalculator clc) {
  238.         super(cap);
  239.         setBounds(x, y, width, height);
  240.         this.cl = clc;
  241.         this.cl.add(this);
  242.         addActionListener(this);
  243.     }
  244.  
  245.     public void actionPerformed(ActionEvent ev) {
  246.         char memop = ((MyMemoryButton) ev.getSource()).getLabel().charAt(1);
  247.  
  248.         cl.setClear = true;
  249.         double temp = Double.parseDouble(cl.displayLabel.getText());
  250.  
  251.         switch (memop) {
  252.             case 'C':
  253.                 cl.memLabel.setText(" ");
  254.                 cl.memValue = 0.0;
  255.                 break;
  256.             case 'R':
  257.                 cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue));
  258.                 break;
  259.             case 'S':
  260.                 cl.memValue = 0.0;
  261.             case '+':
  262.                 cl.memValue += Double.parseDouble(cl.displayLabel.getText());
  263.                 if (cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0")) {
  264.                     cl.memLabel.setText(" ");
  265.                 } else {
  266.                     cl.memLabel.setText("M");
  267.                 }
  268.                 break;
  269.         }
  270.     }
  271. }
  272.  
  273. class MySpecialButton extends Button implements ActionListener {
  274.  
  275.     MyCalculator cl;
  276.  
  277.     MySpecialButton(int x, int y, int width, int height, String cap, MyCalculator clc) {
  278.         super(cap);
  279.         setBounds(x, y, width, height);
  280.         this.cl = clc;
  281.         this.cl.add(this);
  282.         addActionListener(this);
  283.     }
  284.  
  285.     static String backSpace(String s) {
  286.         String Res = "";
  287.         for (int i = 0; i < s.length() - 1; i++) {
  288.             Res += s.charAt(i);
  289.         }
  290.         return Res;
  291.     }
  292.  
  293.     public void actionPerformed(ActionEvent ev) {
  294.         String opText = ((MySpecialButton) ev.getSource()).getLabel();
  295.         if (opText.equals("Backspc")) {
  296.             String tempText = backSpace(cl.displayLabel.getText());
  297.             if (tempText.equals("")) {
  298.                 cl.displayLabel.setText("0");
  299.             } else {
  300.                 cl.displayLabel.setText(tempText);
  301.             }
  302.             return;
  303.         }
  304.         if (opText.equals("C")) {
  305.             cl.number = 0.0;
  306.             cl.op = ' ';
  307.             cl.memValue = 0.0;
  308.             cl.memLabel.setText(" ");
  309.         }
  310.         cl.displayLabel.setText("0");
  311.         cl.setClear = true;
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment