diff --git a/1/src/Calculator/Calculator.java b/1/src/Calculator/Calculator.java new file mode 100644 index 0000000..c678d19 --- /dev/null +++ b/1/src/Calculator/Calculator.java @@ -0,0 +1,181 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Calculator extends JFrame implements ActionListener { + // Display screen + JTextField display; + // Buttons for digits and operations + JButton[] numberButtons; + JButton[] functionButtons; + JButton add, sub, mul, div, equal, clear, decimal; + JPanel panel; + double num1 = 0, num2 = 0, result = 0; + char operator; + boolean start; + + public Calculator() { + // Set the title of the frame + setTitle("Calculator"); + // Set the default close operation + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + // Set the size of the frame + setSize(400, 500); + // Set the layout of the frame + setLayout(null); + + // Create display screen + display = new JTextField(); + display.setBounds(30, 50, 320, 50); + display.setFont(new Font("Arial", Font.BOLD, 24)); + display.setEditable(false); + add(display); + + // Create buttons + numberButtons = new JButton[10]; + functionButtons = new JButton[5]; + + for (int i = 0; i < 10; i++) { + numberButtons[i] = new JButton(String.valueOf(i)); + numberButtons[i].setFont(new Font("Arial", Font.BOLD, 24)); + numberButtons[i].addActionListener(this); + } + + add = new JButton("+"); + sub = new JButton("-"); + mul = new JButton("*"); + div = new JButton("/"); + equal = new JButton("="); + clear = new JButton("C"); + decimal = new JButton("."); + + add.setFont(new Font("Arial", Font.BOLD, 24)); + sub.setFont(new Font("Arial", Font.BOLD, 24)); + mul.setFont(new Font("Arial", Font.BOLD, 24)); + div.setFont(new Font("Arial", Font.BOLD, 24)); + equal.setFont(new Font("Arial", Font.BOLD, 24)); + clear.setFont(new Font("Arial", Font.BOLD, 24)); + decimal.setFont(new Font("Arial", Font.BOLD, 24)); + + functionButtons[0] = add; + functionButtons[1] = sub; + functionButtons[2] = mul; + functionButtons[3] = div; + functionButtons[4] = equal; + + for (int i = 0; i < 5; i++) { + functionButtons[i].addActionListener(this); + } + + clear.addActionListener(this); + decimal.addActionListener(this); + + // Set bounds and add buttons to panel + panel = new JPanel(); + panel.setBounds(30, 120, 320, 300); + panel.setLayout(new GridLayout(4, 4, 10, 10)); + + panel.add(numberButtons[1]); + panel.add(numberButtons[2]); + panel.add(numberButtons[3]); + panel.add(add); + + panel.add(numberButtons[4]); + panel.add(numberButtons[5]); + panel.add(numberButtons[6]); + panel.add(sub); + + panel.add(numberButtons[7]); + panel.add(numberButtons[8]); + panel.add(numberButtons[9]); + panel.add(mul); + + panel.add(decimal); + panel.add(numberButtons[0]); + panel.add(equal); + panel.add(div); + + panel.add(clear); + + add(panel); + + // Set visibility of the frame + setVisible(true); + } + + @Override + public void actionPerformed(ActionEvent e) { + for (int i = 0; i < 10; i++) { + if (e.getSource() == numberButtons[i]) { + display.setText(display.getText() + i); + } + } + + if (e.getSource() == add) { + num1 = Double.parseDouble(display.getText()); + operator = '+'; + display.setText(""); + start = true; + } + + if (e.getSource() == sub) { + num1 = Double.parseDouble(display.getText()); + operator = '-'; + display.setText(""); + start = true; + } + + if (e.getSource() == mul) { + num1 = Double.parseDouble(display.getText()); + operator = '*'; + display.setText(""); + start = true; + } + + if (e.getSource() == div) { + num1 = Double.parseDouble(display.getText()); + operator = '/'; + display.setText(""); + start = true; + } + + if (e.getSource() == equal) { + num2 = Double.parseDouble(display.getText()); + + switch (operator) { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + } + + display.setText(String.valueOf(result)); + start = false; + } + + if (e.getSource() == clear) { + display.setText(""); + num1 = 0; + num2 = 0; + result = 0; + start = false; + } + + if (e.getSource() == decimal) { + display.setText(display.getText() + "."); + } + } + + public static void main(String[] args) { + new Calculator(); + } +} \ No newline at end of file