Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to write a program in Java that does this: Create class Date with the follo

ID: 3787745 • Letter: N

Question

Need to write a program in Java that does this:
Create class Date with the following capabilities:

a) Output the date in multiple formats, such as:

MM/DD/YYYY
June 14, 2005
DDD YYYY
b) 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. [Hint: To convert the string representation of the month to a numeric value, compare strings using the equals method. For example, it s1 and s2 are strings, the method call s1.equals(s2) returns true if the strings are identical and otherwise returns false.]

* Note please do not use JPlane because it hasn't been covered yet.

Explanation / Answer

// Date.java
public class Date {

private int month;
private int day;
private int year;
private String[] name_Month = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
private int[] days_Month = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public Date()
{
month = 1;
day = 1;
year = 1980;
}

public Date(int inputMonth, int inputDay, int inputYear) {
setMonth(inputMonth);
setDay(inputDay);
setYear(inputYear);
}

public Date(String inputMonth, int inputDay, int inputYear) {
setMonth(determineMonth(inputMonth));
setDay(inputDay);
setYear(inputYear);
}

public Date(int d, int inputYear) {
getdayMonth(d);
setYear(inputYear);
}

public int getMonth() {
return month;
}

public void setMonth(int inputMonth) {
month = (inputMonth > 0 && inputMonth < 13) ? inputMonth : 1;
}

public int getDay() {
return day;
}

public void setDay(int inputDay) {

if (inputDay > 0 && inputDay <= days_Month[month]) {
day = inputDay;
} else if (month == 2 && inputDay == 29 && checkLeapYear()) {
day = inputDay;
} else {
day = 1;
}
}

public int getYear() {
return year;
}

public void setYear(int inputYear) {
year = (inputYear > 1979 && inputYear < 3000) ? inputYear : 2011;
}

public boolean checkLeapYear() {
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
return true;
} else {
return false;
}
}

public String slash() {
return month + "/" + day + "/" + year;
}

public String monthNameDate() {
return monthToName() + " " + day + ", " + year;
}

public String dateDay() {
return dayOfyearcov() + " " + year;
}

public int dayOfyearcov() {
int d = 0;
for (int i = 1; i < month; i++) {
if (checkLeapYear() && i == 2) {
d += 29;
} else {
d += days_Month[i];
}
}
d += day;
return d;
}

public String monthToName() {
return name_Month[month];
}

public int determineMonth(String inputMonth) {

for (int i = 1; i < name_Month.length; i++) {
if (name_Month[i].equalsIgnoreCase(inputMonth)) {
return i;
}
}
return 1;
}

public void getdayMonth(int input) {
if (input > 0) {
month = 1;
while (input > days_Month[month]) {
if (checkLeapYear() && month == 2) {
input -= 29;
} else {
input -= days_Month[month];
}
month++;
}
day = input;
} else {
day = 1;
month = 1;
}
}
}


// DateTest.java

import java.util.Scanner;

public class DateTest {
public static void main(String [] args){

Date date1 = new Date(11,1,1999);
Date date2;   

Scanner sc = new Scanner(System.in);
System.out.println("Enter month: ");
String month = sc.next();
System.out.println("Enter day: ");
int day = sc.nextInt();
System.out.println("Enter year: ");
int year = sc.nextInt();

date2 = new Date(month,day,year);

StringBuffer buffer = new StringBuffer("");
buffer.append(" Date in salsh format: "+date1.slash()+" ");
buffer.append("Date in month name format: " +date1.monthNameDate()+ " ");
buffer.append("Date in day date format: " + date1.dateDay()+" ");
System.out.println(buffer);
buffer = new StringBuffer("");
buffer.append(" Date2 in salsh format: "+date2.slash()+" ");
buffer.append("Date2 in month name format: " +date2.monthNameDate()+ " ");
buffer.append("Date2 in day date format: " + date2.dateDay()+" ");
System.out.println(buffer);

}
}


/*
output:

Enter month:
1
Enter day:
31
Enter year:
1992

Date in salsh format: 11/1/1999
Date in month name format: November 1, 1999
Date in day date format: 305 1999


Date2 in salsh format: 1/31/1992
Date2 in month name format: January 31, 1992
Date2 in day date format: 31 1992


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote