HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Basic android calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
androidcalculatorbasic

Problem

So this is my code and I would like to add subtraction, multiplication and division. If you have any suggestions on algorithms to use I would be very happy to see it. I do not wish to copy the code, I just want an outline of how to simply, and efficiently create a calculator. I want to make a calculator for KIDS with a friendly UI maybe showing dogs, cats, food etc. as the buttons. With 1 dog/cat for the 1 button and so on.....

I wish to put ads in this application, but I want the ads just to be under a donation/ad tab.

Tell me what you think, questions, comments and concerns would be very much appreciated to help develop my app :)

package com.nickmarcoose.addingcalculatorfree;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Addition extends Activity
{
  EditText amount1;
  EditText amount2;
  TextView views;
  Button Addition1;
  double a;
  double b;
  double c;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initControls();
  }

  private void initControls()
  {
    amount1=(EditText)findViewById(R.id.amount1);
    amount2=(EditText)findViewById(R.id.amount2);
    views=(TextView)findViewById(R.id.view);
    Addition1=(Button)findViewById(R.id.start);
    Addition1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick (View  view) { calculate();}
    });
  }

  private void calculate()
  {

    a=Double.parseDouble(amount1.getText().toString());
    b=Double.parseDouble(amount2.getText().toString());
    c=b+a;
    views.setText(Double.toString(c));
  }
}

Solution

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;
    }
}

Code Snippets

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;
}
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;
    }
}

Context

StackExchange Code Review Q#13005, answer score: 2

Revisions (0)

No revisions yet.