Question
JAVA: Create class Date with the following capabilities: Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY Use overloaded constructors
JAVA: Create class Date with the following capabilities:
Output the date in multiple formats, such as
MM/DD/YYYY
June 14, 1992
DDD YYYY
Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year.
Please provide the code in the appropriate methods.
public class Date {
private static final String[] monthNames = {"January", "February",
"March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
private static final int[] monthDays = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
private int day; // day of the month
private int month; // month in the year
private int year; // year
// no-argument constructor
public Date() {
}
// constructor for format MM/DD/YYYY
public Date(int month, int day, int year) {
}
// constructor for format MonthName dd, yyyy
public Date(String month, int day, int year) {
}
// constructor for format DDD YYYY
public Date(int ddd, int year) {
}
// Set the day
public void setDay(int day) {
}
// Set the month
public void setMonth(int mm) {
}
// Set the year
public void setYear(int year) {
}
// return Date in format: mm/dd/yyyy
public String toString() {
}
// return Date in format: MonthName dd, yyyy
public String toMonthNameDateString() {
}
// return Date in format DDD YYYY
public String toDayDateString() {
}
// Return the number of days in the month
private static int daysInMonth(int month, int year) {
}
// test for a leap year
private static boolean leapYear(int year) {
}
// convert mm and dd to ddd
private int convertToDayOfYear() {
int ddd = 0;
for (int m = 1; m < month; ++m) {
if (leapYear(year) && m == 2) {
ddd += 29;
}
else {
ddd += monthDays[m -1];
}
}
ddd += day;
return ddd;
}
// convert from month name to month number
private static int convertFromMonthName(String monthName) {
for (int subscript = 0; subscript < 12; subscript++) {
if (monthName.equals(monthNames[subscript])) {
return subscript + 1;
}
}
throw new IllegalArgumentException("Invalid month name");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started