Hello, this is a continuation of article on Developing a Mini CalculatorÂ Part-1. In the first part I have explained about the components required for this application. TodayÂ we will see the implementation ofÂ Calculation Logic.

*Note: I will be using Java programming language to demonstrate the examples*

**Calculation Logic**

SinceÂ our calculator only support four operations and each of them take two operands and one operator it will be easy for us to write one single procedure or method which do all the necessary operation and return the result

public static double doCalculate(double operand1, char operator, double operand2) {
double result = 0.0;
switch (operator) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '/': result = operand1 / opernad2; break;
case '*': result = operand1 * operand2; break;
}
return result;
}

The above given method is seems good but what will happen if you divide a number with zero. Yes of course division by zero is not possible. In that case what we will do. One way is to insert an error handling code that do the required when such and error comes. Again think of other scenario where the result is infinity or not a number. What we will do again insert some logic to handle that scenario.

Instead of keeping all calculations in one method we can separate them, like one method for addition, one four subtraction etc. Each of them will take three arguments and return the expected result.

public static double doCalculate(double operand1, char operator, double operand2) {
double result = 0.0;
switch (operator) {
case '+': result = doAddition(operand1, operand2); break;
case '-': result = doSubtraction(operand1, operand2); break;
case '/': result = doDivision(operand1, operand2); break;
case '*': result = doMultiplication(operand1 * operand2); break;
}
return result;
}
private static double doAddition(double operand1, double operand2) {
return (operand1) + (operand2);
}
private static double doSubtraction(double operand1, double operand2) {
return (operand1) - (operand2);
}
private static double doDivision(double operand1, double operand2) {
return (operand1) / (operand2);
}
private static double doMultiplication(double operand1, double operand2) {
return (operand1) * (operand2);
}

Now we can manage each operation logic separately, say for division we can add extra error handling code

private static double doDivision(double operand1, double operand2) {
double result = 0.0;
try {
result = operand1 / operand2;
catch (ArithmeticExceptionÂ ex) {
// set some flag to indicate this error
}
if (new Double(result).isNaN()) {
// set some flag to indicate this
result = 0.0;
} else if (!Double.isFinite(result)) {
// set some flag to indicate this
result = 0.0;
}
return result;
}

If we look at the above program we will see two things. If the division leads to error we will set some flag and return zero. If the result is not a number or infinite we do the same. The flag might be some where else in the program. We have to introduce a state change some where out side the calculation logic. This is okay! because it doesn’t harm anything.

Instead of changing the state out side the calculation logic we can simply return the entire result out side the calculation logic. Say, the result, is it valid?, is it infinite?, is it an error. How we will do this?

public class Result {
private double value;
private boolean error;
private boolean finite;
private boolean nan;
public Result() {
// initialize to default
}
// get and set methods will come here
}

We can create a new Result object to do this. This object will keep all the information related to the operation carried out. The Calculation Logic simply return the final result with necessary information to process the result. So an external module say Core can process the result as required without worrying about the calculations đź™‚

*Calculation Logic: Do the required calculations and return the result*

*Core: Process the result and take the necessary actions*

private static Result doDivision(double operand1, double operand2) {
Result result = new Result();
double value = 0.0;
try {
value = operand1 / operand2;
catch (ArithmeticExceptionÂ ex) {
result.setError(true);
}
result.setValue(value);
if (new Double(value).isNaN()) {
result.setNaN(true);
} else if (!Double.isFinite(result)) {
result.setFinite(false);
}
return result;
}

Here ends the second part.Â Hope you like it đź™‚

In the next part we will see the implementation of other modules.

### Like this:

Like Loading...