Exercise: Fitness Tracker Log

Implement a method with a dynamic return type to process fitness data.

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 GetDistance that accepts a string parameter named format.

  • 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 GetDistance method must use the dynamic return type.

  • You must store the method’s result in a dynamic variable 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 if statement inside GetDistance to check the format parameter.

  • 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 dynamic keyword in Program.cs and reassign it for both method calls.

Exercise: Fitness Tracker Log

Implement a method with a dynamic return type to process fitness data.

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 GetDistance that accepts a string parameter named format.

  • 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 GetDistance method must use the dynamic return type.

  • You must store the method’s result in a dynamic variable 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 if statement inside GetDistance to check the format parameter.

  • 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 dynamic keyword in Program.cs and reassign it for both method calls.

C# 14.0
namespace FitnessApp;
public class FitnessTracker
{
public int DistanceRunInKm { get; set; }
// Add the GetDistance method here
}