├── Calculator.iml ├── README.md ├── out └── production │ └── Calculator │ ├── CalculatorApp.class │ ├── constants │ └── CommonConstants.class │ ├── gui │ └── CalculatorGui.class │ └── service │ └── CalculatorService.class └── src ├── CalculatorApp.java ├── constants └── CommonConstants.java ├── gui └── CalculatorGui.java └── service └── CalculatorService.java /Calculator.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Calculator App

2 | 3 |

Overview

4 |

The Calculator App is a Java application that provides a graphical user interface (GUI) for performing basic arithmetic calculations. It uses Swing components for the GUI and includes features such as number input, arithmetic operations, and result display.

5 |

Checkout the full tutorial here

6 |

Files

7 | 8 |
    9 |
  1. CalculatorApp.java: This file contains the main class CalculatorApp, which serves as the entry point for the application. It creates an instance of the CalculatorGui class and makes the GUI visible.
  2. 10 | 11 |
  3. CalculatorGui.java: This file contains the CalculatorGui class, which extends JFrame and represents the main GUI window of the application. It sets up the GUI components, including a display field for showing the numbers and results, and buttons for number input and arithmetic operations. It also handles button click events to perform the calculations.
  4. 12 | 13 |
  5. CalculatorService.java: This file contains the CalculatorService class, which provides the actual arithmetic calculations for the application. It includes methods for addition, subtraction, multiplication, and division.
  6. 14 | 15 |
  7. CommonConstants.java: This file contains the CommonConstants class, which defines common constants used in the application, such as the application name, the size of the GUI window, and the configurations for the display field and buttons.
  8. 16 |
17 | 18 |

Usage

19 | 20 |

To use the Calculator App, follow these steps:

21 | 22 |
    23 |
  1. Compile the Java files:
  2. 24 |
    javac CalculatorApp.java CalculatorGui.java CalculatorService.java CommonConstants.java
    25 | 26 |
  3. Run the compiled Java program:
  4. 27 |
    java CalculatorApp
    28 | 29 |
  5. The Calculator App GUI window will appear. You can enter numbers and perform arithmetic operations using the buttons.
  6. 30 | 31 |
  7. To enter numbers, click the respective number buttons (0-9).
  8. 32 | 33 |
  9. To perform arithmetic operations, click the corresponding operator buttons (+, -, x, /). The current number in the display field will be used as the first operand.
  10. 34 | 35 |
  11. To calculate the result, click the "=" button. The result will be displayed in the display field.
  12. 36 |
37 | 38 |

Class Details

39 | 40 |

1. CalculatorApp.java

41 | 42 |

This class contains the CalculatorApp class, which serves as the entry point for the application.

43 | 44 |

Methods

45 | 48 | 49 |

2. CalculatorGui.java

50 | 51 |

This class represents the main GUI window of the application.

52 | 53 |

Constructors

54 | 57 | 58 |

Methods

59 | 66 | 67 |

3. CalculatorService.java

68 | 69 |

This class provides the actual arithmetic calculations for the application.

70 | 71 |

Methods

72 | 87 | 88 |

4. CommonConstants.java

89 | 90 |

This class contains common constants used in the application.

91 | 92 |

Constants

93 | 109 | -------------------------------------------------------------------------------- /out/production/Calculator/CalculatorApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/curadProgrammer/Java-Calculator/1a5e7eb3c83f76422fde8936cc376d63989377eb/out/production/Calculator/CalculatorApp.class -------------------------------------------------------------------------------- /out/production/Calculator/constants/CommonConstants.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/curadProgrammer/Java-Calculator/1a5e7eb3c83f76422fde8936cc376d63989377eb/out/production/Calculator/constants/CommonConstants.class -------------------------------------------------------------------------------- /out/production/Calculator/gui/CalculatorGui.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/curadProgrammer/Java-Calculator/1a5e7eb3c83f76422fde8936cc376d63989377eb/out/production/Calculator/gui/CalculatorGui.class -------------------------------------------------------------------------------- /out/production/Calculator/service/CalculatorService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/curadProgrammer/Java-Calculator/1a5e7eb3c83f76422fde8936cc376d63989377eb/out/production/Calculator/service/CalculatorService.class -------------------------------------------------------------------------------- /src/CalculatorApp.java: -------------------------------------------------------------------------------- 1 | import gui.CalculatorGui; 2 | 3 | public class CalculatorApp { 4 | public static void main(String[] args){ 5 | new CalculatorGui().setVisible(true); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/constants/CommonConstants.java: -------------------------------------------------------------------------------- 1 | package constants; 2 | 3 | public class CommonConstants { 4 | // APP CONFIGS 5 | public static final String APP_NAME = "Calculator"; 6 | public static final int[] APP_SIZE = {400, 600}; // {width, height} 7 | 8 | // TEXTFIELD CONFIGS 9 | public static final int TEXTFIELD_LENGTH = 10; 10 | public static final int TEXTFIELD_FONTSIZE = 36; 11 | public static final int TEXTFIELD_SPRINGLAYOUT_NORTHPAD = 30; 12 | public static final int TEXTFIELD_SPRINGLAYOUT_WESTPAD = 33; 13 | 14 | // BUTTON CONFIGS 15 | public static final int BUTTON_ROWCOUNT = 4; 16 | public static final int BUTTON_COLCOUNT = 4; 17 | public static final int BUTTON_COUNT = 16; 18 | public static final int BUTTON_FONTSIZE = 46; 19 | public static final int BUTTON_SPRINGLAYOUT_NORTHPAD = 150; 20 | public static final int BUTTON_SPRINGLAYOUT_WESTPAD = 33; 21 | public static final int BUTTON_HGAP = 25; 22 | public static final int BUTTON_VGAP = 25; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/gui/CalculatorGui.java: -------------------------------------------------------------------------------- 1 | package gui; 2 | 3 | import constants.CommonConstants; 4 | import service.CalculatorService; 5 | 6 | import javax.swing.*; 7 | import java.awt.*; 8 | import java.awt.event.ActionEvent; 9 | import java.awt.event.ActionListener; 10 | 11 | public class CalculatorGui extends JFrame implements ActionListener { 12 | private final SpringLayout springLayout = new SpringLayout(); 13 | private CalculatorService calculatorService; 14 | 15 | // display field 16 | private JTextField displayField; 17 | 18 | // buttons 19 | private JButton[] buttons; 20 | 21 | // flags 22 | private boolean pressedOperator = false; 23 | private boolean pressedEquals = false; 24 | 25 | public CalculatorGui(){ 26 | super(CommonConstants.APP_NAME); 27 | setSize(CommonConstants.APP_SIZE[0], CommonConstants.APP_SIZE[1]); 28 | setDefaultCloseOperation(EXIT_ON_CLOSE); 29 | setResizable(false); 30 | setLocationRelativeTo(null); 31 | setLayout(springLayout); 32 | 33 | calculatorService = new CalculatorService(); 34 | 35 | addGuiComponents(); 36 | } 37 | 38 | public void addGuiComponents(){ 39 | // add display components 40 | addDisplayFieldComponents(); 41 | 42 | // add button components 43 | addButtonComponents(); 44 | } 45 | 46 | public void addDisplayFieldComponents(){ 47 | JPanel displayFieldPanel = new JPanel(); 48 | displayField = new JTextField(CommonConstants.TEXTFIELD_LENGTH); 49 | displayField.setFont(new Font("Dialog", Font.PLAIN, CommonConstants.TEXTFIELD_FONTSIZE)); 50 | displayField.setEditable(false); 51 | displayField.setText("0"); 52 | displayField.setHorizontalAlignment(SwingConstants.RIGHT); 53 | 54 | displayFieldPanel.add(displayField); 55 | 56 | this.getContentPane().add(displayFieldPanel); 57 | springLayout.putConstraint(SpringLayout.NORTH, displayFieldPanel, CommonConstants.TEXTFIELD_SPRINGLAYOUT_NORTHPAD, SpringLayout.NORTH, this); 58 | springLayout.putConstraint(SpringLayout.WEST, displayFieldPanel, CommonConstants.TEXTFIELD_SPRINGLAYOUT_WESTPAD, SpringLayout.WEST, this); 59 | } 60 | 61 | public void addButtonComponents(){ 62 | GridLayout gridLayout = new GridLayout(CommonConstants.BUTTON_ROWCOUNT, CommonConstants.BUTTON_COLCOUNT); 63 | JPanel buttonPanel = new JPanel(); 64 | buttonPanel.setLayout(gridLayout); 65 | buttons = new JButton[CommonConstants.BUTTON_COUNT]; 66 | for(int i = 0; i < CommonConstants.BUTTON_COUNT; i++){ 67 | JButton button = new JButton(getButtonLabel(i)); 68 | button.setFont(new Font("Dialog", Font.PLAIN, CommonConstants.BUTTON_FONTSIZE)); 69 | button.addActionListener(this); 70 | 71 | buttonPanel.add(button); 72 | } 73 | 74 | gridLayout.setHgap(CommonConstants.BUTTON_HGAP); 75 | gridLayout.setVgap(CommonConstants.BUTTON_VGAP); 76 | 77 | this.getContentPane().add(buttonPanel); 78 | 79 | springLayout.putConstraint(SpringLayout.NORTH, buttonPanel, CommonConstants.BUTTON_SPRINGLAYOUT_NORTHPAD, SpringLayout.NORTH, this); 80 | springLayout.putConstraint(SpringLayout.WEST, buttonPanel, CommonConstants.BUTTON_SPRINGLAYOUT_WESTPAD, SpringLayout.WEST, this); 81 | } 82 | public String getButtonLabel(int buttonIndex){ 83 | switch(buttonIndex){ 84 | case 0: 85 | return "7"; 86 | case 1: 87 | return "8"; 88 | case 2: 89 | return "9"; 90 | case 3: 91 | return "/"; 92 | case 4: 93 | return "4"; 94 | case 5: 95 | return "5"; 96 | case 6: 97 | return "6"; 98 | case 7: 99 | return "x"; 100 | case 8: 101 | return "1"; 102 | case 9: 103 | return "2"; 104 | case 10: 105 | return "3"; 106 | case 11: 107 | return "-"; 108 | case 12: 109 | return "0"; 110 | case 13: 111 | return "."; 112 | case 14: 113 | return "+"; 114 | case 15: 115 | return "="; 116 | } 117 | return ""; 118 | } 119 | 120 | @Override 121 | public void actionPerformed(ActionEvent e) { 122 | String buttonCommand = e.getActionCommand(); 123 | if(buttonCommand.matches("[0-9]")){ 124 | if(pressedEquals || pressedOperator || displayField.getText().equals("0")) 125 | displayField.setText(buttonCommand); 126 | else 127 | displayField.setText(displayField.getText() + buttonCommand); 128 | 129 | // update flags 130 | pressedOperator = false; 131 | pressedEquals = false; 132 | }else if(buttonCommand.equals("=")){ 133 | // calculate 134 | calculatorService.setNum2(Double.parseDouble(displayField.getText())); 135 | 136 | double result = 0; 137 | switch(calculatorService.getMathSymbol()){ 138 | case '+': 139 | result = calculatorService.add(); 140 | break; 141 | case '-': 142 | result = calculatorService.subtract(); 143 | break; 144 | case '/': 145 | result = calculatorService.divide(); 146 | break; 147 | case 'x': 148 | result = calculatorService.multiply(); 149 | break; 150 | } 151 | 152 | // update the display field 153 | displayField.setText(Double.toString(result)); 154 | 155 | // update flags 156 | pressedEquals = true; 157 | pressedOperator = false; 158 | 159 | }else if(buttonCommand.equals(".")){ 160 | if(!displayField.getText().contains(".")){ 161 | displayField.setText(displayField.getText() + buttonCommand); 162 | } 163 | }else{ // operator 164 | if(!pressedOperator) 165 | calculatorService.setNum1(Double.parseDouble(displayField.getText())); 166 | 167 | calculatorService.setMathSymbol(buttonCommand.charAt(0)); 168 | 169 | // update flags 170 | pressedOperator = true; 171 | pressedEquals = false; 172 | } 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/service/CalculatorService.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | public class CalculatorService { 4 | private double num1; 5 | private double num2; 6 | private char mathSymbol; 7 | 8 | public char getMathSymbol() { 9 | return mathSymbol; 10 | } 11 | 12 | public void setMathSymbol(char mathSymbol) { 13 | this.mathSymbol = mathSymbol; 14 | } 15 | 16 | 17 | public void setNum1(double num1) { 18 | this.num1 = num1; 19 | } 20 | 21 | public void setNum2(double num2) { 22 | this.num2 = num2; 23 | } 24 | 25 | public double add(){ 26 | return this.num1 + this.num2; 27 | } 28 | 29 | public double subtract(){ 30 | return this.num1 - this.num2; 31 | } 32 | 33 | public double multiply(){ 34 | return this.num1 * this.num2; 35 | } 36 | 37 | public double divide(){ 38 | return this.num1/this.num2; 39 | } 40 | 41 | } 42 | --------------------------------------------------------------------------------