/simple-calculator

simple calculator with java

Primary LanguageJava

Java Swing Calculator

This is a simple calculator application built using Java Swing. The calculator can perform basic arithmetic operations such as addition, subtraction, multiplication, and division. It also logs operations to a file.

Features

  • Basic arithmetic operations: addition, subtraction, multiplication, and division.
  • Logs operations to a file (log.txt).
  • Simple and intuitive GUI built using Java Swing.

Getting Started

Prerequisites

  • Java Development Kit (JDK) installed on your machine. You can download it from here.

Running the Calculator

  1. Clone the repository or download the source code.

    git clone https://github.com/maryamjbr/simple-calculator.git
    cd simple-calculator
  2. Compile the Java source file.

    javac calculator.java
  3. Run the compiled Java program.

    java calculator

Using the Calculator

  • Numbers: Click on the number buttons (0-9) to input numbers.
  • Operations: Click on the operation buttons (+, -, *, /) to perform arithmetic operations.
  • Decimal Point: Click on the . button to add a decimal point.
  • Equals: Click on the = button to compute the result of the operation.
  • Clear: Click on the clear button to reset the input fields.
  • Log: Click on the log button to print the log of operations to the console.

Code Structure

  • Main Class: The calculator class contains the main method and the GUI components.
  • Method: The answer method computes the result of the arithmetic operation.

Example

To add two numbers:

  1. Click on 2.
  2. Click on +.
  3. Click on 3.
  4. Click on =.

The result (5) will be displayed in the text field.

Logging

All operations are logged in the log.txt file. You can view the log by clicking the log button, which will print the log to the console.

GUI

Screenshot 6_8_2024 8_04_50 PM