Saturday, July 25, 2015

Calculator III

What Did I Learn?


The difference between a double and a Double:

It turns out that a Double can equal null and  a double cannot. This was very important for my project, at least I thought it was. I've got values that need to be doubles, because of the calculations I'm doing, and I need to check them against null because that will determine what a button will do when pressed.


A bit more about the difference between public and private:

I've only got the one MainActivity class, so I figured I could make all my methods private, including the ones for button presses. When I ran my app, as soon as I pressed a button, it crashed. This happened because all those buttons are referenced in my xml. When a method is private, it's invisible to everything outside it's class.

Android Studio's warnings are not so bad:

I always thought of them as nitpicking nuisances, that mainly wanted me to extract my strings to string resources, but today, warnings reminded me to change a == comparison with strings to .equals(). I thought this would save my broken app, it didn't, but it was helpful nonetheless.


This is tricky.

I talked a big game about how easy this would be, and maybe this is easy, but I just spent hours trying to work out the logic of this calculator and it still doesn't work. I was trying to work it out in my head, but I kept losing track of my variables and what I needed them to do. So, I decided to make a spread sheet. I listed each variable, and what their values would be for each button press I went through.



What did I actually do?


First of all, I added a few conditions to limit the number of characters that users can enter, and that makes sure only 1 decimal point can be entered. 

My first plan was to have each button handle its operation. So when the add button was pressed, the value on screen would be stored in a holder value, and if there was already a value in the holder, the values would be added and returned to the screen. Pretty messy. I completely scrapped that idea, and decided to make a calculate method.


Calculate Method

The calculate method has three parameters: an operator string and two doubles. It contains a switch statement that the performs an action with the two numbers depending on the operator:
case "+":
     result = num1 + num2;

Operator Buttons

The plan for each operator button is to, when pressed, check for a value on the screen. If there's not one, nothing will happen. I made a couple global Double variables (number1 & number 2) to hold values, and a string to hold the operator. The value from the screen is stored in  number1 and the operator will be set to whatever button is pressed and the screen is cleared.

I also want users to be able to add(or whatever) to totals. So can do 2 + 2, then press plus again, which clears the screen, and  they can perform an operation on the total(4), instead of 2+2, pressing = and then performing another operation. So based on my spreadsheet, I found the situation where this is the case(number 1 != null && number2 == null) and added this condition to my operator.  In these cases, number2 is set to the value on screen, I call the calculate method and store the result in number1, then clear the screen.

This logic works for all the operators, so I was going to copy and paste it, just replacing the operators. Instead, remembering the tenant DRY, I refactored this into the operator method, which takes an operator(+, -, /, *, %) as a parameter.


Square Root

This was a little different(and its the only thing working currently), because it just takes what's on screen and shoots back it's square root. So that's not a part of the operator method, but it does factor into the Equals method. And it sets the operator to "SR."


Equal Method

For this I first need to check to see if the operator is SR, if that's the case, the squareRoot method is called. This way users can enter 81, press SR, and get 9, then press = and get 3. This piece of functionality wasn't important to me, but I wanted to be sure that after a square root press, if users do press equal, the button wouldn't be referring to an older operator.

In other cases the equal method will convert the value on the screen to a Double, store it in number2, call the calculate method, and display the results.


What went wrong?

My best guess is that the problem lies in where I'm setting the values of my number variables. Maybe they are in if statements, when they should come before them, and the proper conditions aren't being..This made less and less sense as I typed it. I think that my logic is just off. I'm going to do some debugging and see what the values actually are when buttons are pressed. And I'll probably need to overhaul my operation method.



No comments:

Post a Comment