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

SUBMIT Date.java (with no imports, no main(), no System calls). 1. Do not use an

ID: 3727065 • Letter: S

Question

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.

9. Reuse: Avoid copy/paste of code (certainly no plagiarism) by using one method to call others in your Class, like only one section of "daysTo" algorithm, one general constructor called by the default, and one date verification algorithm that is used by many methods to keep the date valid.

Below is some test code, that represents only a portion of what I might test:

public class Assign4 {

   // Part of the main method I'll use to test your class

   // NO imports allowed from the JAVA API

   public static void main(String[] a) {

       Date Date(1582,10,15);   // start of Gregorian

       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)); // -158184 days (negative)

       System.out.println(one.getDay());   // day is now the 25th

       System.out.println(one.getMonth());   // 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

      

       try {

           Date three = new Date(12,34,1956); // obviously illegal

           Date four = new Date(2013,2,29); // illegal leap year

           three.setDay(31);       // fixes that day of month, OK

           four.setMonth(3);       // fixes the month, year still wrong

           four.setYear(1929);       // fixes the year, code not reached

       } catch (IllegalArgumentException e) {

           System.out.println("Illegal day attempted");

       }

      

       // 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()); // January 1, 1970

      

       // Finally, let's understand what static methods are most commonly used for:

       System.out.println(Date.daysTo(one, two));   // still 158184 days (positive)

   }

}

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

public class Assign4 {

   // Part of the main method I'll use to test your class

   // NO imports allowed from the JAVA API

   public static void main(String[] a) {

       Date Date(1582, 10, 15); // start of Gregorian

       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)); // -158184 days (negative)

       System.out.println(one.getDay()); // day is now the 25th

       System.out.println(one.getMonth()); // 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

       try {

           Date three = new Date(12, 34, 1956); // obviously illegal

           Date four = new Date(2013, 2, 29); // illegal leap year

           three.setDay(31); // fixes that day of month, OK

           four.setMonth(3); // fixes the month, year still wrong

           four.setYear(1929); // fixes the year, code not reached

       } catch (IllegalArgumentException e) {

           System.out.println("Illegal day attempted");

       }

       // 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()); // January 1, 1970

       // Finally, let's understand what static methods are most commonly used for:

       //System.out.println(Date.daysTo(one, two)); // still 158184 days (positive)

   }

}


public class Date {
   private int day;
   private int month;
   private int year;
   int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   public Date() {
       this.day = 1;
       this.month = 1;
       this.year = 1970;
   }

   public Date(int year, int month, int day) {
       this.day = day;
       this.month = month;
       if (!isLeapYear() && day > 28) {
           throw new IllegalArgumentException();
       }
       this.year = year;
   }

   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;
   }

   // method to check leap year
   public boolean isLeapYear() {
       boolean leap = false;

       if (year % 4 == 0) {
           if (year % 100 == 0) {
               // year is divisible by 400, hence the year is a leap year
               if (year % 400 == 0)
                   leap = true;
               else
                   leap = false;
           } else
               leap = true;
       } else
           leap = false;
       return leap;
   }

   int maxNumberOfDaysInMonth() {
       int md = daysInMonth[month - 1];
       return md;
   }

   void addDays(int n) {
       int d = this.day += n;
       int m = this.month;
       int y = this.year;
       while (d > maxNumberOfDaysInMonth()) {
           d = d - maxNumberOfDaysInMonth();
           m++;
           if (m > 12) {
               y++;
               m = 1;
           }
       }
       setDay(d);
       setMonth(m);
       setYear(y);
   }

   @Override
   public String toString() {
       return year + "/" + month + "/" + day;
   }

   public void addWeeks(int numberOfWeeks) {
       // call to add days
       addDays(numberOfWeeks*7+1);

   }

   public String longDate() {
       String[] months = new String[13];
       months[0] = null;
       months[1] = "January";
       months[2] = "February";
       months[3] = "March";
       months[4] = "April";
       months[5] = "May";
       months[6] = "June";
       months[7] = "July";
       months[8] = "August";
       months[9] = "September";
       months[10] = "October";
       months[11] = "November";
       months[12] = "December";
       return months[month] + " " + day + " , " + year;
   }

}

output:

25
12
1582
false
1582/12/25
January 1 , 1970

//i could not understand your daysTo method .please complete it yourself.