Tuesday, August 4, 2015

Calculator VII: The Final Mathening



My first app, completed without the assistance of any kind of tutorial, is finally finished. 

Last Steps


The issue I had before with the operation method was easily fixed. I just needed to call my calculate method before I changed the value of the operator. 

I also added a toast that will let users know if they've gone over the maximum number of digits. To facilitate this addition, I refactored all the keypresses on my number pad into a keypress method. This method only adds a number to the screen if the user is under the limit and displays the toast otherwise.

It could be recency bias, but it really felt like getting the formatting right was the most difficult and frustrating part of this entire project. When previous parts of this didn't work I generally had some idea why, here I was completely unsure of what was going wrong. At first, it seemed pretty easy, I even jumped the gun a bit and made a "final" commit after my first formatting method appeared to be working. I noticed though, that it was formatting pretty much everything: a number like 742.34348 would end up in exponent notation. 

In my method, I had set up a variable to determine if a '.' existed in a number and where it was. Then I tried to set up conditions based on this that would determine how to format the number. No matter how I changed the conditions, the results were always the same, so I realized that my formatting, with the exception of the one in my final "else" clause, wasn't being used at all. 

Instead of using breakpoints and debugging, I thought it would be easier to isolate the issue in a context where I could quickly manipulate data, so I just pasted my formatting method into Eclipse. I had the method return just the decimal index for certain values, and the results were not as I expected. So then I made the method output just the value of the number and I found my problem. The numbers, which I had defined as Doubles, were being displayed in exponent notation before I even had a chance to format them. To remedy this, and I left this commented out in my code, I thought that I could use my original formatting and then format it again based the value of the exponent. This wouldn't have worked anyway, and was crashing my app because I was trying to format a String like it was a Double.  

After a bit of research, I found out about the BigDecimal. This seemed perfect, my numbers would not be converted before I got a chance to format them, and I could get my output to look the way I wanted. I thought that I could just plug them into my code in place of Doubles. The problem is, BigDecimals are immutable, so I couldn't do arithmetic on them and there's not an easy way to get their square root. To make this work, before a Double is displayed onscreen, I convert it into a new BigDecimal, and then use my formatter on the BigDecimal. Now I've got a working calculator. 

So, that's the app I thought would be easy. I can't wait to see what it's like dealing with something more complex, like something with more than one activity for instance. Enemies, I'd like to finish next, but I'm also going to start working on a to do list app and maybe an alarm clock. 

No comments:

Post a Comment