Create a Date Class with integer data members f for year, month, and day. Also i
ID: 3776572 • Letter: C
Question
Create a Date Class with integer data members f for year, month, and day. Also include a string data
member for the name of the month. Include a method that returns the month name (as a stringl as part
of the date. Separate the day from the year with a comma in that method. Include appropriate
constructors, properties, and methods. Override the ToString () method to display the date formatted
with slashes (/) separating the month, day, and year. Create a second class that instantiates and test
the Date class.
note: do it in c# console application
Explanation / Answer
using System;
class Date
{
//data members
private int year;
private int monthint;
private int day;
private string monthstr;
//constructors
public Date() //default constructor
{
}
public Date(int year,int monthint,int day,string monthstr) //parameterized constructor
{
this.year = year;
this.monthint = monthint;
this.day = day;
this.monthstr = monthstr;
}
//properties for variables
public int Year
{
get
{
return year;
}
set
{
year = value;
}
}
public int Monthint
{
get
{
return monthint;
}
set
{
monthint = value;
}
}
public int Day
{
get
{
return day;
}
set
{
day = value;
}
}
public string Monthstring
{
get
{
return monthstr;
}
set
{
monthstr = value;
}
}
public string displayMonthName()
{
return ""+monthstr+" "+day+","+year;
}
public override string ToString()
{
return ""+monthint+"/"+day+"/"+year;
}
}
public class Test
{
public static void Main()
{
Date d = new Date(1977,12,1,"december");
Console.WriteLine(d.ToString());
Console.WriteLine(d.displayMonthName());
Date d1 = new Date();
d1.Year = 2016;
d1.Day = 15;
d1.Monthint = 6;
d1.Monthstring = "june";
Console.WriteLine(d1.ToString());
Console.WriteLine(d1.displayMonthName());
}
}
output: