Exercise: Fitness Tracker Log
Problem statement
You are building a fitness application that logs daily running distances. Users can request their logged distance in either kilometers or miles, requiring the system to return different data types based on the requested format.
Task requirements
Create a method named
GetDistancethat accepts a string parameter namedformat.Return the raw integer distance if the format is “metric”.
Return a formatted string containing the converted distance if the format is “imperial” (multiply the kilometers by 0.62).
Call the method from the main program for both formats and print the results to the console.
Constraints
The
GetDistancemethod must use thedynamicreturn type.You must store the method’s result in a
dynamicvariable in the main program before printing it.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use an
ifstatement insideGetDistanceto check theformatparameter.Since the return type is
dynamic, you do not need to cast the integer or the string before returning them.Declare a variable using the
dynamickeyword inProgram.csand reassign it for both method calls.
Exercise: Fitness Tracker Log
Problem statement
You are building a fitness application that logs daily running distances. Users can request their logged distance in either kilometers or miles, requiring the system to return different data types based on the requested format.
Task requirements
Create a method named
GetDistancethat accepts a string parameter namedformat.Return the raw integer distance if the format is “metric”.
Return a formatted string containing the converted distance if the format is “imperial” (multiply the kilometers by 0.62).
Call the method from the main program for both formats and print the results to the console.
Constraints
The
GetDistancemethod must use thedynamicreturn type.You must store the method’s result in a
dynamicvariable in the main program before printing it.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use an
ifstatement insideGetDistanceto check theformatparameter.Since the return type is
dynamic, you do not need to cast the integer or the string before returning them.Declare a variable using the
dynamickeyword inProgram.csand reassign it for both method calls.