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

Create, test, and document a Java class using the adapter pattern to implement t

ID: 3751101 • Letter: C

Question

Create, test, and document a Java class using the adapter pattern to implement the following interface.

/** * DateInterface contains a set of methods to manipulate dates.

Similar to some of * the methods in the class java.util.Date.

Create, test, and document a Java class using the adapter pattern to implement the following interface.

public interface DateInterface {

/** * Returns the year * @return the year stored by the object */

public int getYear();

/** * Sets the year * @param year * @return true */

public boolean setYear(int year);

/** * Returns the month: 0 for January,

1 for February, etc., and 11 for December.

* @return integer corresponding to the month */

public int getMonth();

/** * Sets the month, provided it is between 0 and 11; otherwise no changes are made.

* month must be consistent with the year and day of month. For example, if month is

* 1 (Februray), but year is 2016 and day of month is 30 as stored in the DateInterface

* object, the month stored in the DateInterface object must not be changed. The client

* must do some extra work to effect the change: for example, change the day to 1 and then change * month.

* Anther example is changing month to 3 (April) when day of month is 31. 2 * @param month should be between 0 and 11

* @return returns true if month is valid and a change could be made */

public boolean setMonth(int month);

/** * Returns the day

* @return day of the month */

public int getDayOfMonth();

/** * Sets the day of the month. Should be a valid day or no action is taken.

* For example, if you set 29 for February and the year is 1900 as stored in the

* DateInterface object, the change will not be made. Essentially, the change in dayOfMonth * must not potentially be an inconsistent date. The client

* must do some extra work to effect the change: for example, change the month to 11 and then change

* month. * @param date a valid date for the month and year

* @return true only if the day is valid and a change could be made

public boolean setDayOfMonth(int date);

/** * Returns the hour between 0 and 23

* @return the hour in military time */

public int getHours();

/** * Sets the hour. Should be between 0 and 23 or no action is taken

* @param hours between 0 and 23

* @return true only if hours is valid and a change could be made

*/

public boolean setHours(int hours);

/** * Returns the number of minutes past the hour

* @return number of minutes past the hour */

public int getMinutes();

/** * Sets the minutes of this object; should be between 0 and 59 or no changes are made

* @param minutes 0-59

* @return true only if hours is valid and a change could be made

*/

public boolean setMinutes(int minutes);

/** * Returns the number of seconds past the minute

* @return the number of seconds past the minute */

public int getSeconds();

/** * Sets the seconds of this object; should be between 0 and 59 or no changes are made

* @param seconds 0-59

* @return true only if seconds is valid and a change could be made 3

*/

public boolean setSeconds(int seconds);

/** * Returns true if and only if the year is a leap year

* @return true if and only if the year is a leap year */

public boolean isLeapYear(); }

Your task is to implement the above interface using the Adapter pattern. For this, use the GregorianCalendar class. The implementation must be called DateImpl. When the object is created, it must be set to the current date and time.

Correctness of the Java code and test results: 26 points

• public int getYear(); • public boolean setYear(int year);

• public int getMonth(); • public boolean setMonth(int month);

• public int getDayOfMonth();

• public boolean setDayOfMonth(int date);

• public int getHours();

• public boolean setHours(int hours);

• public int getMinutes();

• public boolean setMinutes(int minutes);

• public int getSeconds();

• public boolean setSeconds(int seconds);

• public boolean isLeapYear(); Program structure: 12 points

• Proper use of methods in super classes

12

• Improper use of features would be penalized Coding standards:

12 points

o Meaningful documentation using /** and */ before the class 4 o Meaningful documentation using /** and */ before all methods: 2 o Proper indentation and line breaks, etc.: 6 o Improperly laid out code will be penalized.

Explanation / Answer

Driver.java

import java.util.GregorianCalendar;

public class Driver {

   /**
   * Main method that runs program and test it
   * @param args
   */
   public static void main(String[] args) {
       DateImpl current = new DateImpl();
        GregorianCalendar test = new GregorianCalendar();
      
        /* Test getter methods */
   
        System.out.printf("year: %d month: %d day: %d hour: %d minutes: %d seconds: %d ",
               current.getYear(), current.getMonth(), current.getDayOfMonth(), current.getHours(),
               current.getMinutes(), current.getSeconds());
        assert (current.getYear() == test.get(GregorianCalendar.YEAR));
        assert (current.getMonth() == test.get(GregorianCalendar.MONTH));
        assert (current.getDayOfMonth() == test.get(GregorianCalendar.DAY_OF_MONTH));
        assert (current.getHours() == test.get(GregorianCalendar.HOUR_OF_DAY));
        assert (current.getMinutes() == test.get(GregorianCalendar.MINUTE));
        assert (current.getSeconds() == test.get(GregorianCalendar.SECOND));
      
      
        /* Test setter methods */
        System.out.println("Set year to 2018: ");
        current.setYear(2018);
        assert(current.getYear() == 2018);
        System.out.printf("year: %d ", current.getYear());
      
        System.out.println("Set month to 8: ");
        current.setMonth(8);
        assert(current.setMonth(8) && current.getMonth() == 8);
        System.out.printf("month: %d ", current.getMonth());
      
        System.out.println("Set month to 20: ");
        current.setMonth(20);
        assert(!current.setMonth(20) | current.getMonth() == 20);
        System.out.printf("month: %d ", current.getMonth());
      
        System.out.println("Set day to 12: ");
        current.setDayOfMonth(12);
        assert(current.setDayOfMonth(12) && current.getDayOfMonth() == 12);
        System.out.printf("day: %d ", current.getDayOfMonth());
      
        System.out.println("Set day to 40: ");
        current.setDayOfMonth(40);
        assert(!current.setDayOfMonth(40) | current.getDayOfMonth() == 40);
        System.out.printf("day: %d ", current.getDayOfMonth());
      
        System.out.println("Set hour to 0: ");
        current.setHours(0);
        assert(current.setHours(23) && current.getHours() == 0);
        System.out.printf("hour: %d ", current.getHours());
      
        System.out.println("Set hour to -1: ");
        current.setHours(-1);
        assert(!current.setHours(-1) | current.getHours() == -1);
        System.out.printf("hour: %d ", current.getHours());
      
        System.out.println("Set minutes to 30: ");
        current.setMinutes(30);
        assert(current.setMinutes(30) && current.getMinutes() == 30);
        System.out.printf("minute: %d ", current.getMinutes());
      
        System.out.println("Set minutes to 70: ");
        current.setMinutes(70);
        assert(!current.setMinutes(70) | current.getMinutes() == 70);
        System.out.printf("minute: %d ", current.getMinutes());
      
        System.out.println("Set seconds to 13: ");
        current.setSeconds(13);
        assert(current.setSeconds(13) && current.getSeconds() == 13);
        System.out.printf("seconds: %d ",current.getSeconds());
      
        System.out.println("Set seconds to 90: ");
        current.setSeconds(90);
        assert(!current.setSeconds(90) | current.getSeconds() == 90);
        System.out.printf("seconds: %d ",current.getSeconds());
      
        current.setYear(2016);
        assert(current.isLeapYear());
        System.out.printf("Is %d a leap year: " + current.isLeapYear() + " ", current.getYear());
      
        current.setYear(2017);
        assert(current.isLeapYear());
        System.out.printf("Is %d a leap year: " + current.isLeapYear() + " ", current.getYear());
        System.out.println("End of tests");
   }

}


DateImpl.java

import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateImpl implements DateInterface {
   private GregorianCalendar calendar;
  

   /**
   * A no argument constructor that initializes calendar
   */
   public DateImpl() {
       calendar = new GregorianCalendar();
      
   }

   @Override
   public int getYear() {
       return calendar.get(Calendar.YEAR);
   }


   @Override
   public boolean setYear(int year) {
       boolean flag = false;
       if (year > -1) {
           calendar.set(Calendar.YEAR, year);
           flag = true;
       }
       return flag;
   }

   @Override
   public int getMonth() {
       return calendar.get(Calendar.MONTH);
   }


   @Override
   public boolean setMonth(int month) {
       boolean flag = false;
       if (month >= 0 && month <= 11) {
           if ( (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                   || month == 10 || month == 12) && getDayOfMonth() <= 31) {
               calendar.set(Calendar.MONTH, month);
               flag = true;
           }
           else if ((month == 4 || month == 6 || month == 9 || month == 11)
                   && getDayOfMonth() <= 30) {
               calendar.set(Calendar.MONTH, month);
               flag = true;
           }
           else if (month == 2 && ((getDayOfMonth() <= 28 && !isLeapYear()) || (getDayOfMonth() <= 29 && isLeapYear()))) {
               calendar.set(Calendar.MONTH, month);
                flag = true;
           }
       }
       if (!flag) {
           System.out.println("Not able to change month.");
       }
       return flag;
   }


   @Override
   public int getDayOfMonth() {
       return calendar.get(Calendar.DAY_OF_MONTH);
   }


   @Override
   public boolean setDayOfMonth(int date) {
       boolean flag = false;
       if (date > 0 && date <= 31) {
           if ( (getMonth() == 1 || getMonth() == 3 || getMonth() == 5 || getMonth() == 7 || getMonth() == 8
                   || getMonth() == 10 || getMonth() == 12) && getDayOfMonth() <= 31) {
               calendar.set(Calendar.DAY_OF_MONTH, date);
               flag = true;
           }
           else if ((getMonth() == 4 || getMonth() == 6 || getMonth() == 9 || getMonth() == 11)
                   && getDayOfMonth() <= 30) {
               calendar.set(Calendar.DAY_OF_MONTH, date);
               flag = true;
           }
           else if (getMonth() == 2 && ((getDayOfMonth() <= 28 && !isLeapYear()) || (getDayOfMonth() <= 29 && isLeapYear()))) {
               calendar.set(Calendar.DAY_OF_MONTH, date);
                flag = true;
           }
       }
       if (!flag) {
           System.out.println("Not a valid day.");
       }
       return flag;
   }


   @Override
   public int getHours() {
       return calendar.get(Calendar.HOUR_OF_DAY);
   }


   @Override
   public boolean setHours(int hours) {
       boolean flag = false;
       if (hours >=0 && hours < 24) {
           calendar.set(Calendar.HOUR_OF_DAY, hours);
           flag = true;
       }
       return flag;
   }


   @Override
   public int getMinutes() {
       return calendar.get(Calendar.MINUTE);
   }


   @Override
   public boolean setMinutes(int minutes) {
       boolean flag = false;
       if (minutes >= 0 && minutes <= 60) {
           calendar.set(Calendar.MINUTE, minutes);
            return true;
        }
       return flag;
   }


   @Override
   public int getSeconds() {
       return calendar.get(Calendar.SECOND);
   }


   @Override
   public boolean setSeconds(int seconds) {
       boolean flag = false;
       if (seconds >= 0 && seconds <= 60) {
           calendar.set(Calendar.SECOND, seconds);
            return true;
        }
       return flag;
   }

   @Override
   public boolean isLeapYear() {
       boolean flag = false;
       if (calendar.isLeapYear(getYear())) {
           flag = true;
       }
       return flag;
   }
  
}

DateInterface.java

public interface DateInterface {
  
   /**
   * Returns the year
   * @return the year stored by the object
   */
   public int getYear();
  
   /**
   * Sets the year
   * @param year
   * @return true
   */
   public boolean setYear(int year);
  
   /**
   * Returns the month: 0 for January, 1 for February, etc., and 11 for December.
   * @return integer corresponding to the month
   */
   public int getMonth();
  
   public boolean setMonth(int month);
  
   /**
   * Returns the day
   * @return day of the month
   */
   public int getDayOfMonth();
  
   public boolean setDayOfMonth(int date);
  
   /**
   * Returns the hour between 0 and 23
   * @return the hour in military time
   */
   public int getHours();
  
   /**
   * Sets the hour. Should be between 0 and 23 or no action is taken
   * @param hours between 0 and 23
   * @return true only if hours is valid and a change could be made
   */
   public boolean setHours(int hours);
  
   /**
   * Returns the number of minutes past the hour
   * @return number of minutes past the hour
   */
   public int getMinutes();
  
   /**
   * Sets the minutes of this object; should be between 0 and 59 or no changes are made
   * @param minutes 0-59
   * @return true only if hours is valid and a change could be made
   */
   public boolean setMinutes(int minutes);
  
   /**
   * Returns the number of seconds past the minute
   * @return the number of seconds past the minute
   */
   public int getSeconds();
  
   /**
   * Sets the seconds of this object; should be between 0 and 59 or no changes are made
   * @param seconds 0-59
   * @return true only if seconds is valid and a change could be made 3
   */
   public boolean setSeconds(int seconds);
  
   /**
      * Returns true if and only if the year is a leap year
      * @return true if and only if the year is a leap year
      */
   public boolean isLeapYear();
}

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