In: Computer Science
C#. Build a class that will be called “MyDate”. The class should have 3 properties: month, day and year. Make month, day and year integers. Write the get and set functions, a display function, and constructors, probably two constructors. (No Database access here.)
Application name :WorkingWithDate
Type of Application :Console Application
Language used :C#
MyDate.cs :
//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namespace
namespace WorkingWithDate
{
class MyDate //C# class
{
//fields
private int month;
private int day;
private int year;
//default constructor
public MyDate()
{
month = 0;
year = 0;
day = 0;
}
//constructor with parameter
public MyDate(int d,int m,int y)
{
day = d;
month = m;
year = y;
}
//Properties/getter and setter methods
public int Day
{
set { day = value; }
get { return day; }
}
public int Month
{
set { month = value; }
get { return month; }
}
public int Year
{
set { year = value; }
get { return year; }
}
//method to display date
public String display()
{
//return day , month year in dd-mm-yyyy format
return Day + "-" + month + "-" + year;
}
}
}
**************************************************************
Program.cs :
//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namesapce
namespace WorkingWithDate
{
class Program //C# class
{
//Main() method
static void Main(string[] args)
{
//Object of MyDate
MyDate d = new MyDate();
//asking user Day
Console.Write("Enter day : ");
//reading day
d.Day = int.Parse(Console.ReadLine());
//asking user month
Console.Write("Enter month : ");
//reading month
d.Month = int.Parse(Console.ReadLine());
//asking user year
Console.Write("Enter year : ");
//reading year
d.Year = int.Parse(Console.ReadLine());
//call function and display date
Console.WriteLine("Date in dd-mm-yyy format : "+d.display());
//to hold the screen
Console.ReadKey();
}
}
}
======================================
Output :