Solution: Implementation of interfaces
Completion requirements

Class DateTime implements interfaces IDate and ITime
- using System;
- namespace DateTime
- {
- interface IDate
- {
- int getDay();
- int getMonth();
- int getYear();
- }
- interface ITime
- {
- int getHour();
- int getMinute();
- int getSecond();
- }
- class DateTime : IDate, ITime // 2 interfaces are implemented
- {
- private int day, month, year, hour, minute, second;
- public DateTime()
- {
- System.DateTime now = System.DateTime.Now;
- day = now.Day;
- month = now.Month;
- year = now.Year;
- hour = now.Hour;
- minute = now.Minute;
- second = now.Second;
- }
- public int getDay() // these methods have to be defined in class
- {
- return day;
- }
- public int getMonth()
- {
- return month;
- }
- public int getYear()
- {
- return year;
- }
- public int getHour()
- {
- return hour;
- }
- public int getMinute()
- {
- return minute;
- }
- public int getSecond()
- {
- return second;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- DateTime dt = new DateTime();
- Console.WriteLine("Date: {0}.{1}.{2}\tTime: {3}:{4}:{5}"
- , dt.getDay().ToString("D2"), dt.getMonth().ToString("D2"), dt.getYear().ToString("D4")
- , dt.getHour().ToString("D2"), dt.getMinute().ToString("D2"), dt.getSecond().ToString("D2"));
- Console.ReadKey();
- }
- }
- }
Last modified: Wednesday, 16 October 2019, 10:51 AM