Debugging Challenge 1

The Challenge

Below you will find a program that does not work as expected due to coding errors. Prove that your C# skills are up to scratch by fixing the bugs in it. Expected output is included in the source code.

This challenge is meant to be solved in 10-15 minutes max and does not require any software. Your browser is enough.

Click here to run the code on ideone.com

using System;

public class Launcher
{
 public static void Main()
 {
 Challenge1 challengeObj = new Challenge1();

challengeObj.Test();
 }
}

///
/// Challenge1.
///
public class Challenge1
{
 private void ChangeTime(DateTime targetTime, string hhmm)
 {
 int hh = Convert.ToInt32(hhmm.Substring(0, 2));
 int mm = Convert.ToInt32(hhmm.Substring(2, 2));

 targetTime.AddHours(targetTime.Hour * -1).AddHours(hh);
 targetTime.AddMinutes(targetTime.Minute * -1).AddMinutes(mm);
 }

 /// RULE#1: Thou shalt not change the return type of this method
 private void CalculateSchedule(DateTime wakeupTime, DateTime bedTime)
 {
 ChangeTime(wakeupTime, "0600");
 ChangeTime(bedTime, "2230");
 }

 public void Test()
 {
 //Get objects with current date and time
 DateTime bedTime = DateTime.Now;
 DateTime wakeupTime = DateTime.Now;

 //Leave dates the same but change the time part
 CalculateSchedule(wakeupTime, bedTime);

 /// EXPECTED OUTPUT (if program runs on 12/22/2013):
 /// I hope you woke up at 12/22/2013 6:00:40 AM
 /// Your bedtime is 12/22/2013 10:30:40 PM
 Console.WriteLine("I hope you woke up at " + wakeupTime.ToString());
 Console.WriteLine("Your bedtime is " + bedTime.ToString());
 }
}

Notes

If you’re unable to fix the defects above read the following notes:

  1. DateTime is immutable

    DateTime, like String and primitive types like int and float, is immutable. What this means is that when you change an object of DateTime using a method like AddHours() it returns a *new* object with the hours added instead of modifying the existing one.

    This is why we had to capture the output of targetTime.AddHours() back into targetTime.

  2. Passing variables by reference
    The widely misunderstood ref keyword is used to pass variables by reference. This means that if we replace the value in a parameter with an entirely new one then the calling function gets the new object – this won’t happen without use of the ref keyword.

    Without using ref the Test() method would not recieve the newly created (since DateTime is immutable) objects from CalculateSchedule().

Solution

Click here to run the code on ideone.com

using System;

public class Launcher
{
 public static void Main()
 {
 Challenge1 challengeObj = new Challenge1();

challengeObj.Test();
 }
}

/// <summary>
/// Solution to Challenge1.
/// </summary>
public class Challenge1
{
 private DateTime ChangeTime(DateTime targetTime, string hhmm)
 {
 int hh = Convert.ToInt32(hhmm.Substring(0, 2));
 int mm = Convert.ToInt32(hhmm.Substring(2, 2));

 targetTime = targetTime.AddHours(targetTime.Hour * -1).AddHours(hh)
 .AddMinutes(targetTime.Minute * -1).AddMinutes(mm);

 return targetTime;
 }

/// RULE#1: Thou shalt not change the return type of this method
 private void CalculateSchedule(ref DateTime wakeupTime, ref DateTime bedTime)
 {
 wakeupTime = ChangeTime(wakeupTime, "0600");
 bedTime = ChangeTime(bedTime, "2230");
 }

 public void Test()
 {
 //Get objects with current date and time
 DateTime bedTime = DateTime.Now;
 DateTime wakeupTime = DateTime.Now;

 //Leave dates the same but change the time part
 CalculateSchedule(ref wakeupTime, ref bedTime);

 /// EXPECTED OUTPUT (if program runs on 12/22/2013):
 /// I hope you woke up at 12/22/2013 6:00:40 AM
 /// Your bedtime is 12/22/2013 10:30:40 PM
 Console.WriteLine("I hope you woke up at " + wakeupTime.ToString());
 Console.WriteLine("Your bedtime is " + bedTime.ToString());
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s