2

I'm creating a calculator app for Android. I've never really created a calculator before so I only know how to do basic calculations. The way I do an equation right now is when I press a number button (1,2,3,etc) that number gets added into a string called theEquation. When I press an operation button (+,-,/,*) theEquation is parsed into a double variable called "a" then theEquation is cleared. After pressing more numbers when I press "=" it sets theEquation to another double variable called "b". Then it has a case/switch based on the operation and either adds, subtracts, multiplies or divides it. This is well for a basic calculator, but I want to make an advanced calculator to be able to do equations like this: "5 + 6 - 4 / 50". I've been trying to do this for a couple of hours but can't wrap my mind around how I should do this.

Any help would be appreciated. By the way, here is my code:

public class MainActivity extends Activity
{
// MESSY CLASS :(
private TextView answerTextView;
private Button number1, number2, number3, number4, number5, number6, number7, number8, number9, number0;
private Button equalsBtn, plusBtn, subBtn, mulBtn, divBtn, clrBtn, delBtn;

private String theEquation = "";
private double answer = 0;

// temp fields
private double a = 0, b = 0;
private int operator = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    answerTextView = (TextView) findViewById(R.id.answerTextView);
    number1 = (Button) findViewById(R.id.number1);
    number2 = (Button) findViewById(R.id.number2);
    number3 = (Button) findViewById(R.id.number3);
    number4 = (Button) findViewById(R.id.number4);
    number5 = (Button) findViewById(R.id.number5);
    number6 = (Button) findViewById(R.id.number6);
    number7 = (Button) findViewById(R.id.number7);
    number8 = (Button) findViewById(R.id.number8);
    number9 = (Button) findViewById(R.id.number9);
    number0 = (Button) findViewById(R.id.number0);

    equalsBtn = (Button) findViewById(R.id.equalsBtn);
    plusBtn = (Button) findViewById(R.id.plusBtn);
    subBtn = (Button) findViewById(R.id.subBtn);
    mulBtn = (Button) findViewById(R.id.mulBtn);
    divBtn = (Button) findViewById(R.id.divBtn);
    clrBtn = (Button) findViewById(R.id.btnClr);
    delBtn = (Button) findViewById(R.id.delBtn);

    numberPresses();
    operationPresses();
}

public void numberPresses()
{
    number1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "1";
            answerTextView.setText(theEquation);
        }
    });
    number2.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "2";
            answerTextView.setText(theEquation);
        }
    });
    number3.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "3";
            answerTextView.setText(theEquation);
        }
    });
    number4.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "4";
            answerTextView.setText(theEquation);
        }
    });
    number5.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "5";
            answerTextView.setText(theEquation);
        }
    });
    number6.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "6";
            answerTextView.setText(theEquation);
        }
    });
    number7.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "7";
            answerTextView.setText(theEquation);
        }
    });
    number8.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "8";
            answerTextView.setText(theEquation);
        }
    });
    number9.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "9";
            answerTextView.setText(theEquation);
        }
    });
    number0.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "0";
            answerTextView.setText(theEquation);
        }
    });
}

public void operationPresses()
{
    plusBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 1;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    subBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 2;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    mulBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 3;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    divBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 4;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    clrBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation = "";
            a = 0;
            b = 0;
            answerTextView.setText(theEquation);
        }
    });
    delBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (theEquation.length() != 0)
                theEquation = theEquation.substring(0, theEquation.length() - 1);
            answerTextView.setText(theEquation);
        }
    });
    equalsBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            b = Double.parseDouble(theEquation);
            try
            {
                switch (operator)
                {
                case 1:
                    answer = a + b;
                    break;
                case 2:
                    answer = a - b;
                    break;
                case 3:
                    answer = a * b;
                    break;
                case 4:
                    answer = a / b;
                    break;
                default:
                    answer = 0;
                    break;
                }
            } catch (Exception e)
            {
                theEquation = "Error!";
                e.printStackTrace();
            }
            theEquation = "" + answer;
            answerTextView.setText(theEquation);
        }
    });
}
}
  • Take a look at: http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence – Morrison Chang Oct 10 '15 at 04:37
  • 2
    Side comment: surely you could make an array of 10 `Buttons` for the digits, instead of duplicating code 10 times? – ajb Oct 10 '15 at 04:52
  • You can start by using what you've already saved. The users types `5 + 6 -`. At the point where you handle the `-` button, `a` is 5 and `operator` is 1 (plus). Your string is `"6"`. Unfortunately, your `onClickListener` for `-` throws away the previous values of `a` and `operator`, and assigns new values to them. Don't throw them away--use them! You already have the logic to do that, in your `=` listener. You'll need to use it on all your operator listeners. That will get you halfway there (continued...) – ajb Oct 10 '15 at 05:01
  • you'll have to figure out how to deal with `/` in your example equation, since you can't do the `5 + 6 - 4` right away, but I'll bet you can come up with something, and it would be instructive for you to try. – ajb Oct 10 '15 at 05:03
  • I'm a big fan of using runtime compilation to handle arithmetic and order of operations. [Runtime Compilation in Java](http://www.javaworld.com/article/2071777/design-patterns/add-dynamic-java-code-to-your-application.html) – Timothy Stepanski Oct 10 '15 at 04:26
  • How would you do that in Android which doesn't natively have a Java compiler on the device? – Morrison Chang Oct 10 '15 at 04:32
  • Web service call? I missed the Android part TBH... – Timothy Stepanski Oct 10 '15 at 04:44

0 Answers0