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 methods. Create the To_String() 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. Make UML Diagram Also
using System;
using System.Collections.Generic;
using System.Globalization;
namespace App
{
class Date
{
private int day, month, year;
private int[] days_per_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private string[] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
public Date(int month, int day, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
/**
* if the year number isn't divisible by 4, it is a common year;
*
* otherwise, if the year number isn't divisible by 100, it is a leap year;
*
* otherwise, if the year number isn't divisible by 400, it is a common year;
*
* otherwise, it is a leap year.
*
* @param year
* @return
*/
bool isLeapYear(int year)
{
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
public string To_String()
{
int days = days_per_month[this.month - 1];
if (this.day > days)
{
return "Invalid number of days";
}
if (isLeapYear(year))
{
if (this.month == 2)
{
days = 29;
if (this.day > days)
{
return "Invalid number of days";
}
}
}
return monthNames[this.month - 1] + "/" + day + "/" + year;
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter year: ");
int year = int.Parse(Console.ReadLine());
Console.Write("Enter month: ");
int month = int.Parse(Console.ReadLine());
Console.Write("Enter day: ");
int day = int.Parse(Console.ReadLine());
Date d = new Date(month, day, year);
Console.Write(d.To_String());
Console.ReadLine();
}
}
}
Comments
Leave a comment