San Jose State University | CS 151 - OO Design Section 5 | Fall 2016

Homework 3

Put all work into a directory hw3 of your Git project and push it once a day. Don't put any code into packages. Don't put any code into a src subdirectory.

Implement a class DayWithTime that represents a day and hours/minutes. I suggest you start with day2/Day.java from the book code. Support these methods:

public class DayWithTime
{
   /**
      Constructs a day with a given year, month, and day
      of the Julian/Gregorian calendar. The Julian calendar
      is used for all days before October 15, 1582
      @param aYear a year != 0
      @param aMonth a month between 1 and 12
      @param aDayOfMonth a day of the month between 1 and 31
      @param hours the hours between 0 and 23
      @param minutes the minutes between 0 and 59
   */
   public DayWithTime(int aYear, int aMonth, int aDayOfMonth, int hours, int minutes)
   {
      ...
   }

   /**
      Returns the year of this day and time
      @return the year
   */
   public int getYear()
   {
      ...
   }

   /**
      Returns the month of this day and time
      @return the month
   */
   public int getMonthValue()
   {
      ...
   }

   /**
      Returns the day of the month of this day and time
      @return the day of the month
   */
   public int getDayOfMonth()
   {
      ...
   }

   /**
      Returns the hours of this day and time
      @return the hours
   */
   public int getHours()
   {
      ...
   }
   
   /**
      Returns the minutes of this day and time
      @return the minutes
   */
   public int getMinutes()
   {
      ...
   }

   public String toString()
   {
      return String.format("%04d-%02d-%02d %02d:%2d", ...);
   }
   

   /**
      Returns a day with time that is a certain number of days away from
      this day with time
      @param n the number of days, can be negative
      @return a day that is n days away from this one
   */
   public DayWithTime plusDays(int n)
   {
      ...
   }

   /**
      Returns a day with time that is a certain number of days away from
      this day with time
      @param n the number of minutes, can be negative
      @return a day that is n days away from this one
   */
   public DayWithTime plusMinutes(long n)
   {
      ...
   }

   /**
      Returns the number of minutes between this day with time and 
      another day with time.
      @param other the other day with time
      @return the number of minutes that this day with time is away from 
      the other
   */
   public long minutesFrom(DayWithTime other)
   {
       ...
   }

   /**
      Returns the number of days between this day with time and 
      another day with time, rounded to the closest value. (Round 
      any difference >= 12 hours to the next day.)
      @param other the other day with time
      @return the number of days that this day is away from 
      the other (>0 if this day comes later)
   */
   public int daysFrom(DayWithTime other)
   {
       ...
   }

   // Private stuff
   ...
}

Here is a JUnit test class.

Implement a class MutableDay that stores just a day, not a time, but whose plusDays method mutates the day instead of returning a new day. Also provide setters for dayOfMonth, monthValue, and year. In the lecture, I said that these mutators aren't a good idea. You'll make them less bad by allowing the mutation to happen, but checking before all other methods that the date is not in an inconsistent state (such as February 31). If it is, throw an IllegalStateException.

I suggest you start with day1/Day from the textbook.

Here is the public interface:

public class MutableDay
{
   /**
      Constructs a day with a given year, month, and day
      of the Julian/Gregorian calendar. The Julian calendar
      is used for all days before October 15, 1582
      @param aYear a year != 0
      @param aMonth a month between 1 and 12
      @param aDayOfMonth a day of the month between 1 and 31
   */
   public MutableDay(int aYear, int aMonth, int aDayOfMonth)
   {
      ...
   }

   /**
      Returns the year of this day
      @return the year
   */
   public int getYear()
   {
      // Check that the date is valid...
      ...
   }

   /**
      Returns the month of this day
      @return the month
   */
   public int getMonthValue()
   {
      // Check that the date is valid...
      ...
   }

   /**
      Returns the day of the month of this day
      @return the day of the month
   */
   public int getDayOfMonth()
   {
      // Check that the date is valid...
      ...
   }

   public void setYear(int year) 
   {
      ...
   }
   
   public void setMonthValue(int month) 
   {
      ...
   }
   
   public void setDayOfMonth(int date) 
   {
      ...
   }
   
   public String toString()
   {
      // Check that the date is valid...
	    return String.format("%04d-%02d-%02d", ...);
   }
   
   /**
      Advances this day (or moves it backwards) by a given number of days.
      @param n the number of days, can be negative
   */
   public void plusDays(int n)
   {
      ...
   }

   /**
      Returns the number of days between this day and another
      day
      @param other the other day
      @return the number of days that this day is away from 
      the other (>0 if this day comes later)
   */
   public int daysFrom(MutableDay other)
   {
      ...
   }
   // Private implementation
   ...
}

Here is a JUnit test class.