Code Monkey home page Code Monkey logo

java-calculator's Introduction

Calculator App

Overview

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.

Checkout the full tutorial here

Files

  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. 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.
  3. 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.
  4. 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.

Usage

To use the Calculator App, follow these steps:

  1. Compile the Java files:
  2. javac CalculatorApp.java CalculatorGui.java CalculatorService.java CommonConstants.java
  3. Run the compiled Java program:
  4. java CalculatorApp
  5. The Calculator App GUI window will appear. You can enter numbers and perform arithmetic operations using the buttons.
  6. To enter numbers, click the respective number buttons (0-9).
  7. To perform arithmetic operations, click the corresponding operator buttons (+, -, x, /). The current number in the display field will be used as the first operand.
  8. To calculate the result, click the "=" button. The result will be displayed in the display field.

Class Details

1. CalculatorApp.java

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

Methods

  • main(String[] args): The main method creates an instance of CalculatorGui and makes the GUI visible.

2. CalculatorGui.java

This class represents the main GUI window of the application.

Constructors

  • CalculatorGui(): Constructs a new CalculatorGui object. It sets up the JFrame with the application name, size, and close operation. It then calls the addGuiComponents() method to add the GUI components.

Methods

  • addGuiComponents(): This method sets up the GUI components, including a display field for showing numbers and results, and buttons for number input and arithmetic operations. It also sets up the layout using SpringLayout and adds event handling for button clicks.
  • getButtonLabel(int buttonIndex): This method returns the label for the buttons based on the button index.
  • actionPerformed(ActionEvent e): This method handles button click events and performs the corresponding arithmetic calculations.

3. CalculatorService.java

This class provides the actual arithmetic calculations for the application.

Methods

  • setMathSymbol(char mathSymbol): Sets the mathematical symbol for the arithmetic operation.
  • setNum1(double num1): Sets the first operand for the arithmetic operation.
  • setNum2(double num2): Sets the second operand for the arithmetic operation.
  • add(): Performs addition and returns the result.
  • subtract(): Performs subtraction and returns the result.
  • multiply(): Performs multiplication and returns the result.
  • divide(): Performs division and returns the result.

4. CommonConstants.java

This class contains common constants used in the application.

Constants

  • APP_NAME: Represents the name of the application.
  • APP_SIZE: Represents the size of the main GUI window as an array of two integers: width and height.
  • TEXTFIELD_LENGTH: Represents the length of the display field.
  • TEXTFIELD_FONTSIZE: Represents the font size of the display field.
  • TEXTFIELD_SPRINGLAYOUT_NORTHPAD: Represents the north padding of the display field in the layout.
  • TEXTFIELD_SPRINGLAYOUT_WESTPAD: Represents the west padding of the display field in the layout.
  • BUTTON_ROWCOUNT: Represents the number of rows for the button grid.
  • BUTTON_COLCOUNT: Represents the number of columns for the button grid.
  • BUTTON_COUNT: Represents the total number of buttons.
  • BUTTON_FONTSIZE: Represents the font size of the buttons.
  • BUTTON_SPRINGLAYOUT_NORTHPAD: Represents the north padding of the button panel in the layout.
  • BUTTON_SPRINGLAYOUT_WESTPAD: Represents the west padding of the button panel in the layout.
  • BUTTON_HGAP: Represents the horizontal gap between buttons in the grid layout.
  • BUTTON_VGAP: Represents the vertical gap between buttons in the grid layout.

java-calculator's People

Contributors

curadprogrammer avatar

Stargazers

 avatar Gratitude avatar Siddharth Kar avatar SkyZzo avatar Justice Bellen avatar Priyam Sharma avatar RezgarKarimiii avatar miloszbugiel avatar  avatar  avatar Niklas  avatar  avatar Ryan Hung avatar Adrayle Perez Ebon avatar

Watchers

 avatar

java-calculator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.