Question: using c# Create a new class Time2 based on Fig. 10.5 but representing the time internally as the number of seconds since midnight rather than
using c#
Create a new class Time2 based on Fig. 10.5 but representing the time internally as the number of seconds since midnight rather than three integer values for hour, minutes and seconds and test it with Time2Test.cs
// Fig. 10.5: Time2.cs // Time2 class declaration with overloaded constructors. using System; // for class ArgumentOutOfRangeException
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")}"; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//this is the Time2Test.cs c sharp file
// Fig. 10.6: Time2Test.cs // Overloaded constructors used to initialize Time2 objects. using System;
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: "); Console.WriteLine("t1: all arguments defaulted"); Console.WriteLine($" {t1.ToUniversalString()}"); // 00:00:00 Console.WriteLine($" {t1.ToString()} "); // 12:00:00 AM
Console.WriteLine( "t2: hour specified; minute and second defaulted"); Console.WriteLine($" {t2.ToUniversalString()}"); // 02:00:00 Console.WriteLine($" {t2.ToString()} "); // 2:00:00 AM
Console.WriteLine( "t3: hour and minute specified; second defaulted"); Console.WriteLine($" {t3.ToUniversalString()}"); // 21:34:00 Console.WriteLine($" {t3.ToString()} "); // 9:34:00 PM
Console.WriteLine("t4: hour, minute and second specified"); Console.WriteLine($" {t4.ToUniversalString()}"); // 12:25:42 Console.WriteLine($" {t4.ToString()} "); // 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(" Exception while initializing t6:"); Console.WriteLine(ex.Message); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
