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

Programming Project #2, page 585, PLUS specifications listed below SUBMIT Date.j

ID: 3723395 • Letter: P

Question

Programming Project #2, page 585, PLUS specifications listed below   

SUBMIT Date.java (with no imports, no main(), no System calls).

----------------------------------------------------------------------------------------------------------------

1. Do not use any imports from the JAVA API, nor any package statements, nor System.

2. I also insist the fields of your Class must remain private access. Only three fields allowed in your Date Class (year, month, day).

3. Add a default constructor that initializes the default Date to January 1, 1970

4. Whenever an illegal date is attempted, your code throws an IllegalArgumentException.

5. The addDays and addWeeks methods can actually subtract days or weeks for a negative parameter passed. Test these for thousands of days too.

6. Add mutator methods (setYear, setMonth, setDay) and be certain they create valid dates, else throw the IllegalArgumentException

7. Add a longDate() method that returns a String with the format as shown below.

8. Create a static version of daysTo, used as shown in test code below. And this is the ONLY place that static appears in your Date Class.

ellexuCollege CS210 * W.P· ANersen lnstructor 4 from Building Java Programs text 5 by Reges and Stepp (Pearson pub.) 7 * Programming Project #2, Chapter 8, p. 585 8 also note text examples for output format 0 public class Assign4 // Part of the main method I'1l use to test your class // NO imports allowed from the JAVA API 3 public static void main(String] a) { Date Date(1582,10,15); // start of gcegocian Date two = new Date(2016, 1,28); // 2016 was a leap year one.addDays (1); /advance one day (negative subtracts days) one.addweeks (10); // advance one week (negative allowed, yes) System.out.println (two.daysTo(one)); I-158184 days (negative) System.out.println (one.getDay) // day is now the 25th System.out.println (one.getMonth(O); // returns 12, January is 1 System.out.println (one.getYear)) / still 1582 from start System.out.println (one.isLeapYear) // false for 1582 System.out.println(one.tostring)); // style is 1582/12/25 2 4 5 6 try Date three = new Date (12,34, 1956); // obviously illegal Date four new Date(2013,2,29); // illegal leap year three.setDay (31); four.setMonth(3); four.setYear (1929) fixes the year, code not reached // fixes that day of month, oK // fixes the month, year still wrong catch (IllegalArgumentException e) [ System.out.println("Illegal day attempted") 4 // Use UNIX zero of 01/01/70 for default, and create "longDate" output // I thought a long date was dinner with a person you don't like? Date five new Date(); System.out.println(five.longDate)) I/ January 1, 197 8 9 // Finally, let's understand what static methods are most commonly used for: System.out.println (Date.daysTo (one, two) // still 158184 days (positive) 5

Explanation / Answer

Answered:
1
2
3
4

Bonus answer:
6
7

/**

*
* @author sambh
*/
public class Date {
private int year;
private int month;
private int date;

private int[] maxDays= {31,28,31,30,31,30,31,31,30,31,30,31};
  
private boolean isLeap() {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
public Date(int year, int month, int date) {
this.year = year;
this.month = month;
this.date = date;
  
if (!isVald())
throw new IllegalArgumentException();
}

public Date() {
this.year = 1970;
this.month = 1;
this.date = 1;
}
  
  
public String longDate() {
String dateString = "";
switch (month) {
case 1:
dateString = "January";
break;
case 2:
dateString = "February";
break;
case 3:
dateString = "March";
break;
case 4:
dateString = "April";
break;
case 5:
dateString = "May";
break;
case 6:
dateString = "June";
break;
case 7:
dateString = "July";
break;
case 8:
dateString = "August";
break;
case 9:
dateString = "September";
break;
case 10:
dateString = "October";
break;
case 11:
dateString = "November";
break;
case 12:
dateString = "December";
break;
}
dateString = dateString + " " + date + ", " + year;
return dateString;
}

private boolean isVald() {
  
if (isLeap())
maxDays[1] = 29;
else
maxDays[1] = 28;
if (month < 1 && month > 12)
return false;
if (date < 1 || date > maxDays[month - 1])
return false;
return true;
}
  
public void setYear(int year) {
this.year = year;
if (!isVald())
throw new IllegalArgumentException();
}

public void setMonth(int month) {
this.month = month;
if (!isVald())
throw new IllegalArgumentException();
}

public void setDate(int date) {
this.date = date;
if (!isVald())
throw new IllegalArgumentException();
}
}