Monday, March 9, 2009

Rather silly sleep calculator...

Let me start by saying that this exercise (p. 290) was far from pointless, even if the overall program is a complete waste of time. While you could never use a program that makes assumptions like, 8 hours sleep per night, 360 days per year and 30 days per month, you can learn from actually doing the work to set it up.

I learned how to keep an answer down to 2 decimal places. And the equation to work out the details entered in to the program was "fun" but like I said, it is hardly a program that can be used outside of being a learning tool -just ask anyone who has a birthday on the 31st of July for example.

We will also have to learn how to limit the number a person can enter in to a text field. And by that I mean a number numerically, not a number of digits. Unless of course you were one of those lucky people who has a birthday on the 85th of the 17th ;-). What's more, the whole equation could be worked out a lot easier with an if/else statement.

But I digress... So I came up with the following interface design:

I felt this was pretty close to what the description was after. It uses Jokerman regular 20pt for the main heading and CaliforniaFB for the other labels in various sizes. As with all of the other programs we worked on in this chapter, the calculate button does the major share of the work.

As you can see, the user enters the current date and then their birth date and presses the calculate button. The calculate click event then takes all of the information and runs its equation in the background before popping up a label with the result.

When I first did this, I used the decimal data type and it gave me one long number that was really hard to read. So after the ToString function, I gave it a forced literal of number and it comma separated the number, making it easier to read but also giving it 2 decimal places.

I couldn't use an integer because it would still make the result hard to read, so I had to use the number literal. What I did to get around the problem was was use ("N0") after the ToString function.

My code for this part looked like this:
lblTotalHoursSlept.Text = strFirstName.ToString & ", you have slept for a total of " & decTotalHoursSleep.ToString("N0") & " hours"
That took the label and displayed the result with comma separated thousands, minus the decimal places. That is N + zero by the way, not No.

The equation worked out a little differently. I spent the afternoon trying to come up with the best way to attack this problem, and in the end, I subtracted the number in the birth date field from the number in the corresponding current date field and then multiplied the result by the number we were given in the exercise. The numbers we were given (360 days for a year, 30 day for a month) were stored as constants.

My code to work out the equation looked like this:
decTotalHoursSleep = (((intDateYear - intBirthYear) * _cintDaysInYear) _
+ ((intDateMonth - intBirthMonth) * _cintDaysInMonth) _
+ (intDateDay - intBirthDay)) * _cintHoursSleepPerDay

and the constants were declared in the public class like this:
Const _cintHoursSleepPerDay As Integer = 8
Const _cintDaysInYear As Integer = 360
Const _cintDaysInMonth As Integer = 30

You will notice that the entire equation is bracketed so that it is worked out before multiplying it by the hours slept (8).

If anyone else came up with a more efficient way of doing this (under the directions given in the exercise of course), I'd love it if you could leave a comment.

Have a good one,

Tim.

4 comments:

  1. good job..
    but, what if i want to get the current date from the my computer date. I mean I wnat to use my computer system date instaed of using input the current date. I wnat the program to run automaticly just let the user input his or her dateofbirth not inter the current date...

    ReplyDelete
  2. Hi Anonymous.
    Apparently you can use Now() to capture the system date. We haven't covered that in class as yet (I had to do a Google search for that info, so thanks for that, it's taught me something pretty cool) and I would have thought it would be most beneficial as part of a database project -which we should cover soon.
    The problem when relating to my "rather silly sleep calculator" is that it doesn't use standard dates. It uses 30 day months and 360 day years, so it is inherently very inaccurate. If you were setting up a sleep calculator for real though, I would assume that using the Now() function would be the way you would do it, along with a much more complex algorithm that I have used.
    Thanks for your comment.
    Tim.

    ReplyDelete
  3. My.Computer.Clock.LocalTime.Year
    this is the code that shows the year from your computer system, and you can try the month and the day as the same.

    jack

    ReplyDelete
  4. Hi Jack.

    Yeah, I'm still very new to all of this. I get the impression though that the Now() function was for earlier versions of VB. I found a site ( http://programming.top54u.com/post/ASP-Net-VB-Format-Date-Using-ToString-Function.aspx ) that sort of covers what has been asked and they use the function as part of an overall statement (I hope I have the terminology right). I would need to muck around with it to see how I could get that to work in the silly sleep calculator, pretty sure I could get it to work though. Now -no pun intended- I just need to find the time.

    All the best,

    Tim.

    ReplyDelete