Question: Need to fill in methods in Java code. Create class Date with the following capabilities: Output the date in multiple formats, such as MM/DD/YYYY June

Need to fill in methods in Java code.

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!