Not very familiar with Android development, so some of this may be totally off the rails.
Depending on how educational you want this to be, you could visually demonstrate simple algorithms to teach how various operations work (e.g. animations). Also, since you're targeting little kids, I'm not sure double is necessary.
Kid-friendly algorithms you could demonstrate below. You could step through each stage of an operation, e.g. by moving animals from pile to pile. You could skip demonstrations if the numbers are large, or make them run in the background/interruptable.
public int add(int left, int right)
{
if (right == 0)
{
return left;
}
return add(left + 1, right - 1);
}
public int subtract(int left, int right)
{
if (right == 0)
{
return left;
}
return subtract(left - 1, right - 1);
}
public int multiply(int left, int right)
{
if (left == 0 || right == 0)
{
return 0;
}
if (left == 1)
{
return right;
}
else if (right == 1)
{
return left;
}
if (left < right)
{
return multiplyByAdding(right, left, 0);
}
return multiplyByAdding(left, right, 0);
}
private int multiplyByAdding(int baseNumber, int timesLeft, int productSoFar)
{
if (timesLeft == 0)
{
return productSoFar;
}
return multiplyByAdding(baseNumber, timesLeft - 1, productSoFar + baseNumber);
}
//division by zero needs to be handled at UI level to explain why it doesn't work
//you might want to do something other than throwing an exception here
public int divide(int dividend, int divisor) throws ArithmeticException
{
if (divisor == 0)
{
throw new ArithmeticException();
}
return divideBySubtracting(dividend, divisor, 0);
}
private int divideBySubtracting(int dividend, int divisor, int quotientSoFar)
{
divident -= divisor;
if (dividend <= 0)
{
return quotientSoFar;
}
return divideBySubtracting(divident, divisor, quotientSoFar + 1);
}
//division explains this, so we can cheat here
public int modulo(int dividend, int divisor)
{
return dividend % divisor;
}
In the code you posted, you have UI and logic mixed together. You probably want to separate them into different modules to keep them clean.
The following is rough pseudocode for how I might organize things. I've left out a lot of stuff that would need to be filled in to make this work, of course.
public enum Operation
{
Addition,
Subtraction,
Multiplication,
Division
}
public class Calculator
{
public int calculate(int left, int right, Operation op)
{
switch(op)
{
case Operation.Addition:
return add(left, right);
case Operation.Subtraction:
return subtract(left, right);
case Operation.Multiplication:
return multiply(left, right);
case Operation.Division:
return divide(left, right);
default:
throw new UnsupportedOperationException(operation.toString() + " not implemented.");
}
}
}
public class MyActivity extends Activity
{
Operation operation;
//...
void calculate()
{
a = calculator.parse(leftAmount);
b = calculator.parse(rightAmount);
answer = calculator.calculate(operation, a, b);
if (operation == Operation.Division)
{
remainder = calculator.modulo(a, b);
}
update();
}
//called when leftOp, rightOp, and/or answer change
//numbers greater than 10 could just be printed numbers
void update(int leftOp, int rightOp, int answer)
{
remainderView.clear();
if (leftOp != previousLeftOp)
{
leftView.setContent( ImageFactory.getImageByNumber(leftOp), leftOp );
}
if (rightOp != previousRightOp)
{
rightView.setContent( ImageFactory.getImageByNumber(rightOp), rightOp );
}
if (answer != previousAnswer)
{
answerView.setContent( ImageFactory.getImageByNumber(answer), answer );
if (operation == Operation.Division && remainder != 0)
{
remainderView.setContent( ImageFactory.getImageByNumber(remainder), remainder );
}
}
previousLeftOp = leftOp;
previousRightOp = rightOp;
previousAnswer = answer;
}
}
calculate()
method isn't even closed. \$\endgroup\$