Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C# code Instructions: 10.11 (Extension Methods for Class Time2) Use an extension method to provide for class Time2 (Fig. 10.5) a Tick method

In C# code

 

Instructions:

10.11 (Extension Methods for Class Time2) Use an extension method to provide for class Time2 (Fig. 10.5) a Tick method that increments the time by one second. The Time2 object should always remain in a consistent state, so be sure to account for and test the following cases:

 

Fig 10.5 below

 

using System;

/*
* 10.11 (Extension Methods for Class Time2) Use an extension
* method to provide for class Time2 (Fig. 10.5)
* a Tick method that increments the time by one second.
* The Time2 object should always remain in a consistent state,
* so be sure to account for and test the following cases:
*/


namespace Exercise_10_11
{
   public class Time2
   {
       private int hour; // 0 - 23
       private int minute; // 0 - 59
       private int second; // 0 - 59

       // constructor can be called with zero, one, two or three arguments
       public Time2(int hour = 0, int minute = 0, int second = 0)
       {
           SetTime(hour, minute, second); // invoke SetTime to validate time
       }

       // Time2 constructor: another Time2 object supplied as an argument
       public Time2(Time2 time)
          : this(time.Hour, time.Minute, time.Second) { }

       // set a new time value using universal time; invalid values
       // cause the properties' set accessors to throw exceptions
       public void SetTime(int hour, int minute, int second)
       {
           Hour = hour; // set the Hour property
           Minute = minute; // set the Minute property
           Second = second; // set the Second property
       }

       // property that gets and sets the hour
       public int Hour
       {
           get
           {
               return hour;
           }
           set
           {
               if (value < 0 || value > 23)
               {
                   throw new ArgumentOutOfRangeException(nameof(value),
                      value, $"{nameof(Hour)} must be 0-23");
               }

               hour = value;
           }
       }

       // property that gets and sets the minute
       public int Minute
       {
           get
           {
               return minute;
           }
           set
           {
               if (value < 0 || value > 59)
               {
                   throw new ArgumentOutOfRangeException(nameof(value),
                      value, $"{nameof(Minute)} must be 0-59");
               }

               minute = value;
           }
       }

       // property that gets and sets the second
       public int Second
       {
           get
           {
               return second;
           }
           set
           {
               if (value < 0 || value > 59)
               {
                   throw new ArgumentOutOfRangeException(nameof(value),
                      value, $"{nameof(Second)} must be 0-59");
               }

               second = value;
           }
       }

       // convert to string in universal-time format (HH:MM:SS)
       public string ToUniversalString() =>
          $"{Hour:D2}:{Minute:D2}:{Second:D2}";

       // convert to string in standard-time format (H:MM:SS AM or PM)
       public override string ToString() =>
          $"{((Hour == 0 || Hour == 12) ? 12 : Hour % 12)}:" +
          $"{Minute:D2}:{Second:D2} {(Hour < 12 ? "AM" : "PM")}";
   }
}
 

 

//Use code below to test:

 

using System;

namespace Exercise_10_11
{
   public class Time2Test
   {
       static void Main()
       {
           var t1 = new Time2(); // 00:00:00            
           var t2 = new Time2(2); // 02:00:00        
           var t3 = new Time2(21, 34); // 21:34:00    
           var t4 = new Time2(12, 25, 42); // 12:25:42
           var t5 = new Time2(t4); // 12:25:42        

           Console.WriteLine("Constructed with:\n");
           Console.WriteLine("t1: all arguments defaulted");
           Console.WriteLine($"   {t1.ToUniversalString()}"); // 00:00:00
           Console.WriteLine($"   {t1.ToString()}\n"); // 12:00:00 AM

           Console.WriteLine(
              "t2: hour specified; minute and second defaulted");
           Console.WriteLine($"   {t2.ToUniversalString()}"); // 02:00:00
           Console.WriteLine($"   {t2.ToString()}\n"); // 2:00:00 AM

           Console.WriteLine(
              "t3: hour and minute specified; second defaulted");
           Console.WriteLine($"   {t3.ToUniversalString()}"); // 21:34:00
           Console.WriteLine($"   {t3.ToString()}\n"); // 9:34:00 PM

           Console.WriteLine("t4: hour, minute and second specified");
           Console.WriteLine($"   {t4.ToUniversalString()}"); // 12:25:42
           Console.WriteLine($"   {t4.ToString()}\n"); // 12:25:42 PM

           Console.WriteLine("t5: Time2 object t4 specified");
           Console.WriteLine($"   {t5.ToUniversalString()}"); // 12:25:42
           Console.WriteLine($"   {t5.ToString()}"); // 12:25:42 PM

           // attempt to initialize t6 with invalid values
           try
           {
               var t6 = new Time2(27, 74, 99); // invalid values
           }
           catch (ArgumentOutOfRangeException ex)
           {
               Console.WriteLine("\nException while initializing t6:");
               Console.WriteLine(ex.Message);
           }
       }
   }
}

 




Step by Step Solution

There are 3 Steps involved in it

Step: 1

To create an extension method for the Time2 class in C that increments the time by ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions