/** * Program Name: Calculator.java * Author: Essam Fahmy * Date: August 8, 2022 * Purpose: This class is used to build and calculate string expressions. */ import java.util.*; public class Calculator { //Creating Required variables private String operand; private String operator; private boolean decimalPressed; private ArrayList list = new ArrayList(); /** * Calculator default constructor */ public Calculator() { operand = ""; operator = ""; decimalPressed = false; } /** * Gets operand * @return operand */ public String getOperand() { return operand; } /** * Sets operand * @param value */ public void setOperand(String value) { this.operand = value; } /** * Gets operator * @return operator */ public String getOperator() { return this.operator; } /** * Sets operator * @param value */ public void setOperator(String value) { this.operator = value; } /** * Gets if decimal is pressed or not * @return decimalPressed */ public boolean getDecimalPressed() { return this.decimalPressed; } /** * Sets if decimal is pressed or not * @param value */ public void setDecimalPressed(Boolean value) { this.decimalPressed = value; } /** * Clears variables */ public void clear() { this.operand = ""; this.operator = ""; this.decimalPressed = false; } /** * Erase 1 char from current operand * @param value * @return value */ public String backspace(String value) throws EmptyOperandException { if (value == "") { throw new EmptyOperandException(value); } else { value = value.substring(0, value.length() - 1); this.operand = value; return value; } } /** * Finds percentage * @param value * @return percent */ public String findPercentage(String value) throws EmptyOperandException { String percent; if (value == "") { throw new EmptyOperandException(value); } else { percent = Double.toString(Double.parseDouble(value) / 100.0); return percent; } } /** * Toggles Plus or Minus * @param flag * @return operand */ public String togglePlusMinus(boolean flag) throws EmptyOperandException { if (this.operand == "") { throw new EmptyOperandException(operand); } else { if (flag == true) { this.operand = "-" + this.operand; } else if (flag == false) { this.operand = this.operand.substring(1); } } return this.operand; } /** * Finds squared * @param value * @return squared */ public String findSquared(String value) throws EmptyOperandException { String squared; if (value == "") { throw new EmptyOperandException(value); } else { squared = Double.toString(Double.parseDouble(value) * Double.parseDouble(value)); return squared; } } /** * Finds square root * @param value * @return squared */ public String findSquareRoot(String value) throws EmptyOperandException { String root; if (value == "") { throw new EmptyOperandException(value); } else { root = Double.toString(Math.sqrt(Double.parseDouble(value))); return root; } } /** * Builds the operand * @param value */ public void buildOperand(String value) throws LongOperandException { if ((this.operand + value).length() > 19) { throw new LongOperandException(value); } else { this.operand = this.operand + value; } } /** * Builds the expression * @param value */ public void buildExpression(String value) throws EmptyOperandException { if (value == "") { throw new EmptyOperandException(value); } else { this.operator = value; list.add(this.operand); list.add(this.operator); clear(); } } /** * Calculates the expression * @return num */ public double calculate() throws EmptyOperandException, ArithmeticException { list.add(this.operand); // Add the last operand to the list if (list.isEmpty() || this.operand.equals("")) { throw new EmptyOperandException("No expression to calculate"); } // First pass for multiplication and division for (int i = 1; i < list.size(); i += 2) { if (list.get(i).equals("*") || list.get(i).equals("/")) { double operand1 = Double.parseDouble(list.get(i - 1)); double operand2 = Double.parseDouble(list.get(i + 1)); double result; if (list.get(i).equals("*")) { result = operand1 * operand2; } else { if (operand2 == 0) throw new ArithmeticException("Division by zero"); result = operand1 / operand2; } list.set(i - 1, Double.toString(result)); // Replace the first operand with the result list.remove(i); // Remove the operator list.remove(i); // Remove the second operand i -= 2; // Move back two steps in the list } } // Second pass for addition and subtraction double result = Double.parseDouble(list.get(0)); for (int i = 1; i < list.size(); i += 2) { double nextOperand = Double.parseDouble(list.get(i + 1)); if (list.get(i).equals("+")) { result += nextOperand; } else if (list.get(i).equals("-")) { result -= nextOperand; } else { throw new IllegalArgumentException("Unknown operator: " + list.get(i)); } } list.clear(); // Clear the list after calculation this.operand = Double.toString(result); // Store result for further calculations return result; } /** * Converts to hex * @param value * @return hex */ public String convertHex(String value) throws EmptyOperandException, LongOperandException { String hex; if(value == "") { throw new EmptyOperandException(value); } else { hex = Double.toHexString(Double.parseDouble(value)); } if(hex.length() > 19) { throw new LongOperandException(value); } else { return hex; } } /** * Convert to Octal * @param value * @return octalStr */ public String convertOct(String value) throws EmptyOperandException, LongOperandException { double remainder, octal = 0.0; String octalStr; double i = 1; if(value == "") { throw new EmptyOperandException(value); } else { double num = Double.parseDouble(value); while (num > 0) { remainder = num % 8; octal += i * remainder; num = num / 8; i = i * 10; } octalStr = Double.toString(octal); } if(octalStr.length() > 19) { throw new LongOperandException(value); } else { return octalStr; } } /** * Convert to binary * @param value * @return binary */ public String convertBin(String value) throws EmptyOperandException, LongOperandException { String binary; if(value == "") { throw new EmptyOperandException(value); } else { binary = Long.toBinaryString(Long.parseLong(value)); } if(binary.length() > 19) { throw new LongOperandException(value); } else { return binary; } } /** * Convert/Retrieve Decimal * @param value * @return value */ public String convertDec(String value) throws EmptyOperandException, LongOperandException{ if(value == "") { throw new EmptyOperandException(value); } else if(value.length() > 19) { throw new LongOperandException(value); } else { return value; } } @Override public String toString() { return "This is Version 1 of a Calculator with a GUI By Essam Fahmy\n\nThe currently stored variables are:\n\nCurrent Operand = " + getOperand() + "\nCurrent Operator = " + getOperator() + "\nDecimal Pressed = " + getDecimalPressed() + "\nCurrent Expression = " + String.join("", list); } }