Monday, January 12, 2015

Java StopWatch: the down buttons

I'm still working on my Java Stop Watch.  I got the down buttons working over the course of about 20 minutes.  10 or so of was copy/pasting the code, and updating a few definitions.  The other 10 was troubleshooting what wasn't working properly.

There is a simple realization when you code: it will be wrong.  From there, you will need to fix it.  Life works much the the same way.  Anyways, here's how the buttons work.

First the mintues up button.

        ones = ones - 1;
                if(ones < 0 && tens > 0)
                {
                    ones = 9;
                    tens = tens - 1;
                }
                else if(ones < 0 && tens == 0)
                {
                    ones = 0;
                    tens = 0;
                }

Now, the explanation.  In this system, you have two separate scenarios.  One is that the number becomes negative.  We don't want that.  The other option is a reduction in place value.   I deal with them in the program in reverse order.  So we'll go with what the program covers.

The reduction in place value is a move is a dual comparison.  So...  If the ones' place is negative, and the tens place is greater than or equal to 1, then reduce the tens place by one and set the ones place to 9.

The other option is the value goes negative.   You can't measure negative time, or at least that I know of.  So, if the ones place is negative and the tens place is zero, set both places to zero.

The main point of both of these systems is the ones place reduction happens immediately.  That way, if the either comparison fails, then nothing is needed to be done and the function is complete (mostly) complete.  You still have to output the information, but that was covered earlier.

After writing the part that decrements the seconds, I changed the seconds incrementer to be consistent.  Previously, the seconds incrementer would go to from 59 to 00 and start back over.  But I didn't write the reduction to work that way, so I changed the system to stay at 59.   No rollover.  It's a personal program, so I'm not concerned about the system going in circles.


Looks like everything is working properly.  Next time, I need to write the threaded portion of the system.  Basically, during the "on tick" event, reduce the the display by 1 second until countdown is complete.

Maybe I'll get that working tonight.  Or some time.   Don't know.  We'll see.



No comments:

Post a Comment