3
\$\begingroup\$

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));
  }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Your creating an app for kids so young they are still learning to count and you think it's appropriate to put ads in? \$\endgroup\$ – James Jun 24 '12 at 14:53
  • 2
    \$\begingroup\$ @James You are right, great point. i should take them out until i get into more advanced UI and ages. \$\endgroup\$ – Nicolas Zemel Jun 24 '12 at 15:12
  • \$\begingroup\$ Your class seems to be incomplete - the calculate() method isn't even closed. \$\endgroup\$ – Donald.McLean Jun 25 '12 at 12:37
  • \$\begingroup\$ @Donald.McLean it seems to be? can you elaborate or include what i should do? \$\endgroup\$ – Nicolas Zemel Jun 25 '12 at 15:42
  • \$\begingroup\$ it shows that it is closed at the end, but i am not 100% sure on how to check what you edited in the post. thanks in advance for helping. \$\endgroup\$ – Nicolas Zemel Jun 25 '12 at 15:49
2
\$\begingroup\$

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;
    }
}
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I just woke up and read this, and i'm amazed how easy you made it look. now i can convert that into my own+android version of it. but i plan on making it more than one variable that can be combined with */-+ operations. so like maybe x+6=1. i want to be able to make like this. but i want to figure this out on my own. so thank you for helping me get started. I plan on making a few modifications to the whole thing though, and maybe changing some colors. like if the number is negative make it red. and positive is blue. unreal numbers are purple. (kids=4<->14) \$\endgroup\$ – Nicolas Zemel Jul 4 '12 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.