Create a Date class with integer data members 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 string) 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 (7) separating the month, day, and year. Create a second class that instantiates and test the Date class.
using System;
using System.Collections.Generic;
using System.Globalization;
namespace App
{
class Date
{
private int year;
public int Year
{
get { return year; }
set { year = value; }
}
private int month;
public int Month
{
get { return month; }
set { month = value; }
}
private int day;
public int Day
{
get { return day; }
set { day = value; }
}
public Date() { }
public Date(int year, int month, int day)
{
this.year = year;
this.month = month;
this.day = day;
}
public override string ToString()
{
return month.ToString() + "/" + day.ToString() + "/" + year.ToString();
}
public string getNameOfMonth()
{
string monthName = new DateTime(year, month, day).ToString("MMM", CultureInfo.InvariantCulture);
return day.ToString() + "," + year.ToString() + "," + monthName;
}
}
class Program
{
static void Main(string[] args)
{
Date newDate = new Date(2020,5,4);
Console.WriteLine(newDate.ToString());
Console.WriteLine(newDate.getNameOfMonth());
Console.ReadLine();
}
}
}
Comments
Leave a comment