This project is an Android-based calculator app that performs basic arithmetic and power calculations. The app features a clean user interface and supports expressions with parentheses, decimal values, and exponents. Below is a detailed description of the app and instructions for building and running it.
-
Basic Arithmetic Operations:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
)
- Addition (
-
Exponentiation:
- The
^
operator is used to calculate powers. It is converted internally toMath.pow()
for accurate results.
- The
-
Parentheses Handling:
- Users can add parentheses
(
and)
for complex expressions. - Bracket balancing is enforced to avoid invalid inputs.
- Users can add parentheses
-
Backspace Functionality:
- Users can remove the last entered character using the backspace button.
-
Clear Button:
- Resets the entire input and result display.
-
Scientific Notation Support:
- Results are displayed in scientific notation (
E
format) if necessary.
- Results are displayed in scientific notation (
-
Error Handling:
- Detects and handles invalid expressions (e.g., unmatched parentheses, invalid syntax).
-
Formatted Results:
- Results are formatted to display up to 8 significant digits for precision and clarity.
- A screenshot of the calculator app's interface is included in the repository.
- The UI consists of the following components:
- Workings TextView: Displays the user-entered expression.
- Results TextView: Displays the calculated result.
- Buttons: Number keys (0-9), operators, parentheses, clear, backspace, and equals.
Screenrecorder-2025-01-05-01-17-48-565.mp4
The app is built using Java for Android. Below is an overview of the key files:
- Handles the logic for the calculator's functionality.
- Includes methods to handle button clicks and update the display.
- Uses the
ScriptEngine
from Java'sjavax.script
package to evaluate mathematical expressions.
-
setWorkings(String givenValue)
:- Updates the expression displayed in the workings TextView.
-
equalOnClick(View view)
:- Evaluates the current expression and displays the result.
- Converts the
^
operator toMath.pow()
for power calculations.
-
checkForPowerOf()
:- Processes the input expression to identify and replace the
^
operator withMath.pow()
syntax.
- Processes the input expression to identify and replace the
-
changeFormula(Integer index)
:- Handles parentheses and numeric extraction for the
^
operator. - Ensures proper syntax for power calculations.
- Handles parentheses and numeric extraction for the
-
findClosingParenthesis(int openIndex)
andfindOpeningParenthesis(int closeIndex)
:- Identify matching parentheses for complex expressions.
-
backSpaceOnClick(View view)
:- Removes the last character from the input expression.
- Defines the layout of the calculator app.
- Includes buttons for digits, operators, and other controls.
-
Prerequisites:
- Android Studio installed on your computer.
- Java Development Kit (JDK) configured.
-
Clone the Repository:
git clone <repository_url> cd <repository_folder>
-
Open in Android Studio:
- Open Android Studio.
- Select "Open an Existing Project" and navigate to the cloned repository.
-
Build the Project:
- Click on "Build" > "Make Project" or press
Ctrl+F9
.
- Click on "Build" > "Make Project" or press
-
Run the App:
- Connect an Android device or start an emulator.
- Click the "Run" button or press
Shift+F10
.
- Enter a mathematical expression using the buttons.
- Use
=
to calculate the result. - Use
C
to clear all input, or the backspace button to remove the last character.
7 + 3 * (10 / (12 / (3 + 1) - 1))
2^3
5 + (6 * 7) - 8 / 4


- Currently, the app supports only basic arithmetic and power operations.
- Advanced mathematical functions (e.g., trigonometry, logarithms) are not included.
- Add support for additional operators and functions (e.g.,
sin
,cos
,log
). - Provide an option to switch between
E
and10^
notations. - Improve error messages for invalid input.
- Enhance UI for better usability on different screen sizes.
This project is licensed under the MIT License - see the (https://en.wikipedia.org/wiki/MIT_License#:~:text=The%20MIT%20License%20is%20a,therefore%20has%20high%20license%20compatibility.) for details.
Feel free to suggest improvements or report issues by creating a new issue in the repository!