Create a Date class with integer data members for year month
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 the 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 test the Date class.
Solution
DATE CLASS:
public class Date {
private int day;
private int month;
private int year;
private String monthName;
public Date(int day, int month, int year, String monthName) {
super();
this.day = day;
this.month = month;
this.year = year;
this.monthName = monthName;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getMonthName() {
return monthName;
}
public void setMonthName(String monthName) {
this.monthName = monthName;
}
public String dateWithName() {
return monthName+\" \"+day+\",\"+year;
}
@Override
public String toString() {
return month + \"/\"+day+\"/\" + year;
}
}
TEST CLASS:
public class TestDate {
public static void main(String[] args) {
Date d=new Date(31, 10, 2016, \"October\");
System.out.println(d);
System.out.println(d.dateWithName());
}
}
OUTPUT:
10/31/2016
October 31,2016

