Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Age on Mercury

The length of a Mercurian year is 88 Earth days.  Write a program that requests your date of birth and computes your Mercurian age.  See the figure below.

Mercury Age

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmMercuryAge Mercury Age Form Holds Controls
 mtbDateOfBirth MaskedTextBox Captures date of birth
 btnCompute Compute Age on Mercury Button Triggers event to display Mercurian age
 txtToday TextBox Displays current date
 txtMercuryAge TextBox Displays age on Mercury

Hints:

  • For mtbDateOfBirth, set the Masked property to Date.
  • For txtToday and txtMercuryAge, set the Read Only property to True.
  • To calculate the years on Mercury, first calculate the total number of days that have elapsed since the date of birth by using the DateInterval function.  Divide the total number of days by 88 to get the age on Mercury.

Write the Code:

' Project: Age on Mercury
' Programmer: Your Name Here
' Date: July 17, 2014
' Description: Receives the user's name as input.
' Displays the user's age if he was on the planet mercury.

Public Class frmMercuryAge

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim todaysDate As Date = Today
      Dim dateOfBirth As Date = CDate(mtbDateOfBirth.Text)
      Dim ageInDays As Double = DateDiff(DateInterval.Day, dateOfBirth, todaysDate)
      Dim mercuryAge As Double = ageInDays / 88
      txtToday.Text = Today.ToString("d")
      txtMercuryAge.Text = mercuryAge.ToString("N") & " Mercurian years."
   End Sub

End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2023 · Metro Pro Theme on Genesis Framework · WordPress · Log in