In: Computer Science
Given the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as October 1, 2020. Please do the following:
public class Date
{
private int dMonth; //variable to store the month
private int dDay; //variable to store the day
private int dYear; //variable to store the year
//Default constructor
//Data members dMonth, dDay, and dYear are set to
//the default values
//Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
public Date()
{
dMonth = 1;
dDay = 1;
dYear = 1900;
}
//Constructor to set the date
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public Date(int month, int day, int year)
{
setDate(month, day, year);
}
//Method to set the date
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public void setDate(int month, int day, int year)
{
if (year >= 1)
dYear = year;
else
dYear = 1900;
if (1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
switch (dMonth)
{
case 1: case 3: case 5: case 7:
case 8: case 10: case 12: if (1 <= day && day <=
31)
dDay = day;
else
dDay = 1;
break;
case 4: case 6:
case 9: case 11: if (1 <= day && day <= 30)
dDay = day;
else
dDay = 1;
break;
case 2: if (isLeapYear())
{
if (1 <= day && day <= 29)
dDay = day;
else
dDay = 1;
}
else
{
if (1 <= day && day <= 28)
dDay = day;
else
dDay = 1;
}
}
}
//Method to return the month
//Postcondition: The value of dMonth is returned
public int getMonth()
{
return dMonth;
}
//Method to return the day
//Postcondition: The value of dDay is returned
public int getDay()
{
return dDay;
}
//Method to return the year
//Postcondition: The value of dYear is returned
public int getYear()
{
return dYear;
}
//Method to return the date in the form mm-dd-yyyy
public String toString()
{
return (dMonth + "-" + dDay + "-" + dYear);
}
public boolean isLeapYear()
{
if ((dYear % 4 == 0 && dYear % 100 != 0) || (dYear % 400 ==
0))
return true;
else
return false;
}
public void setMonth(int m)
{
dMonth = m;
}
public void setDay(int d)
{
dDay = d;
}
public void setYear(int y)
{
dYear = y;
}
public int getDaysInMonth()
{
int noOfDays = 0;
switch (dMonth)
{
case 1: case 3: case 5:
case 7: case 8: case 10: case 12: noOfDays = 31;
break;
case 4: case 6: case 9: case 11: noOfDays = 30;
break;
case 2: if (isLeapYear())
noOfDays = 29;
else
noOfDays = 28;
}
return noOfDays;
}
public int numberOfDaysPassed()
{
int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int sumDays = 0;
int i;
for (i = 1; i < dMonth; i++)
sumDays = sumDays + monthArr[i];
if (isLeapYear() && dMonth > 2)
sumDays = sumDays + dDay + 1;
else
sumDays = sumDays + dDay;
return sumDays;
}
int numberOfDaysLeft()
{
if (isLeapYear())
return 366 - numberOfDaysPassed();
else
return 365 - numberOfDaysPassed();
}
public void incrementDate(int nDays)
{
int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int daysLeftInMonth;
daysLeftInMonth = monthArr[dMonth] - dDay;
if (daysLeftInMonth >= nDays)
dDay = dDay + nDays;
else
{
dDay = 1;
dMonth++;
nDays = nDays - (daysLeftInMonth + 1);
while (nDays > 0)
if (nDays >= monthArr[dMonth])
{
nDays = nDays - monthArr[dMonth];
if ((dMonth == 2) && isLeapYear())
nDays--;
dMonth++;
if (dMonth > 12)
{
dMonth = 1;
dYear++;
}
}
else
{
dDay = dDay+nDays;
nDays = 0;
}
}
}
public void makeCopy(Date otherDate)
{
dMonth = otherDate.dMonth;
dDay = otherDate.dDay;
dDay = otherDate.dDay;
}
public Date getCopy()
{
Date temp = new Date();
temp.dMonth = dMonth;
temp.dDay = dDay;
temp.dYear = dYear;
return temp;
}
}
Short Summary:
Source Code:
ExtDate.java:
/**
*
* ExtDate is the child class extends from the Date class
*
*/
public class ExtDate extends Date {
// Data member of the ExtDate to store the month
name
public String month_string;
// Default constructor
public ExtDate() {
super();
}
// Parameterized constructor
public ExtDate(int month, int day, int year) {
super(month, day, year);
}
// function that finds the month name
public void assign_string_month() {
// checking for the month name and
storing it in a variable
switch (super.getMonth()) {
case 1:
month_string = "January";
break;
case 2:
month_string = "February";
break;
case 3:
month_string = "March";
break;
case 4:
month_string = "April";
break;
case 5:
month_string = "May";
break;
case 6:
month_string = "June";
break;
case 7:
month_string = "July";
break;
case 8:
month_string = "August";
break;
case 9:
month_string = "September";
break;
case 10:
month_string = "October";
break;
case 11:
month_string = "November";
break;
case 12:
month_string = "December";
break;
}
}
@Override
public String toString() {
// return the month name with the
year
return month_string + " " +
super.getYear();
}
}
DateDemo.java:
/**
*
* Main function
*
*/
public class DateDemo {
public static void main(String[] args) {
// creating the instance for child
class
ExtDate extDate = new
ExtDate(3,11,2005);
// calling the method of child
class
extDate.assign_string_month();
// printing the result
System.out.print(extDate.toString());
}
}
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
***************************************************************************************