Question: C# 2010 for programmers fourth edition (Modifying the Internal Data Representation of a Class) It would be perfectly reasonable for the Time2 class of figure

C# 2010 for programmers fourth edition (Modifying the Internal Data Representation of a Class) It would be perfectly reasonable for the Time2 class of figure 10.7 to represent the time internally as the number of seconds since midnight rather than three integer values for hour, minute, and second. Clients could use the same public methods and properties to get the same results. Modify the Time2 class of figure 10.7 to implement the Time2 as the number of seconds since midnight, and show that no change is visible to the clients of the class by using the same test app from figure 10.8.
Note: the Time2 class must use only seconds for representing the time internally. The methods of the class will do whatever work is necessary to convert the public time of hours, minutes and seconds into seconds, and to convert the private seconds of the class into the public time of hours, minutes and seconds. The user should see no difference at all. This project demonstrates that the internal workings of a class are hidden from the user, hiding the data and methods, also known as encapsulation.
Below is the original code, the instructions above tell us what we need to do to modify it, we need to somehow only display time since midnight in seconds instead of hours and minutes and seconds. I do t have a screenshot of what the code is supposed to be but on Chegg there is the same assignment in Java but I don't know how to convert it to c#.
This is the same assignment in Java-
http://www.chegg.com/homework-help/modifying-internal-data-representation-class-would-perfectly-chapter-8-problem-5e-solution-9780132961349-exc?a_r=1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Internal_Data_Representation_of_a_Class
{
class Program
{
static void Main(string[] args)
{
Time2 t1 = new Time2(); // 00:00:00
Time2 t2 = new Time2(2); // 02:00:00
Time2 t3 = new Time2(21, 34); // 21:34:00
Time2 t4 = new Time2(12, 25, 42); // 12:25:42
Time2 t5 = new Time2(27, 74, 99); // 00:00:00
Time2 t6 = new Time2(t4); // 12:25:42
Console.WriteLine("Constructed with: ");
Console.WriteLine("t1: all arguments defaulted");
Console.WriteLine(" {0}", t1.ToUniversalString()); // 00:00:00
Console.WriteLine(" {0} ", t1.ToString()); // 12:00:00 AM
Console.WriteLine(
"t2: hour specified; minute and second defaulted");
Console.WriteLine(" {0}", t2.ToUniversalString()); // 02:00:00
Console.WriteLine(" {0} ", t2.ToString()); // 2:00:00 AM
Console.WriteLine(
"t3: hour and minute specified; second defaulted");
Console.WriteLine(" {0}", t3.ToUniversalString()); // 21:34:00
Console.WriteLine(" {0} ", t3.ToString()); // 9:34:00 PM
Console.WriteLine("t4: hour, minute and second specified");
Console.WriteLine(" {0}", t4.ToUniversalString()); // 12:25:42
Console.WriteLine(" {0} ", t4.ToString()); // 12:25:42 PM
Console.WriteLine("t5: all invalid values specified");
Console.WriteLine(" {0}", t5.ToUniversalString()); // 00:00:00
Console.WriteLine(" {0} ", t5.ToString()); // 12:00:00 AM
Console.WriteLine("t6: Time2 object t4 specified");
Console.WriteLine(" {0}", t6.ToUniversalString()); // 12:25:42
Console.WriteLine(" {0}", t6.ToString()); // 12:25:42 PM
Console.WriteLine("Hit Any key to exit...");
Console.ReadKey();
}
public class Time2
{
public int hour;
public int minute;
public int second;
public Time2() : this(0, 0, 0) {}
public Time2(int h) : this(h, 0, 0) { }
public Time2(int h, int m) : this(h, m, 0) { }
public Time2(int h, int m, int s)
{
SetTime(h, m, s);
}
public Time2(Time2 time): this(time.Hour, time.Minute, time.Second){ }
public void SetTime(int h, int m, int s)
{
Hour = h;
Minute = m;
Second = s;
}
public int Hour
{
get
{
return hour;
}
private set
{
hour = ((value >= 0 && value < 24) ? value : 0);
}
}
public int Minute
{
get
{
return minute;
}
private set
{
minute = ((value >= 0 && value < 60) ? value : 0);
}
}
public int Second
{
get
{
return second;
}
private set
{
second = ((value >= 0 && value < 60) ? value : 0);
}
}
public string ToUniversalString()
{
return string.Format("{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second);
}
public override string ToString()
{
return string.Format("{0}:{1:D2}:{2:D2} {3}",((Hour == 0 || Hour == 12) ? 12 : Hour % 12), Minute, Second, (Hour < 12 ? "AM" : "PM"));
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!