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

A date consists of a month, day and year. Frequently, we represent each of these

ID: 3662281 • Letter: A

Question

A date consists of a month, day and year. Frequently, we represent each of these items as integers(int). For example July 4 1776 is represented as a month 7, day 4 and year 1776.

Note : you have the following Date Class that has only one constructor - the default constructor that will set a new Date object to 1/1/1900 -- for example:

public class Date {
int day, month, year;
public Date()
{
   day =1;
   month = 1;
   year = 1900;
}
}

Write specification (signature) for a method name add_day() that takes ONE parameter of type int greater than zero advances any given date by that number of days and returns the new DATE object. Include a statement of purpose the pre and post conditions including a description of the parameters. Assume that each month has 30 days and there are no leap years.

Write a java implementation for this method. Include comments that will be helpful to someone who will maintain your implementation in the future make sure you include all assumptions. Remember : assume that each month has 30 days and there are no leap years.

Explanation / Answer

Specification (signature) for method name add_day():

public Date add_day(int days)

Pre Condition: Number of days to be increased should be greater than zero
Post Condition:
   1. Number of days should be less than or equal to 30
   2. Number of Months should be less than or equal to 12

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

Java Implementation:

//Program that adds given number of days to date
public class Date
{
   int day, month, year;

   //Constructor that initializes the variables
   public Date()
   {
       day =6;
       month = 1;
       year = 1900;   
   }
  
   //Method that adds number of days based on the condition
   public Date add_day(int days)
   {
       int curr_day = day;
       int new_day = day + days;
       int new_month;
       int no_days, no_months, no_years;

       new_month = new_day / 30;
      
       no_days = new_day % 30;
      
       no_years = new_month / 12;
      
       no_months = new_month % 12;
      
       //Updating new day
       this.day = this.day + no_days - curr_day;
      
       //Updating new month
       this.month = this.month + no_months;
      
       //Updating new year
       this.year = this.year + no_years;
       return this;
   }
  
   //Method that prints the date
   public String print_date()
   {
       return this.day + "-" + this.month + "-" + this.year;
   }
  
   public static void main(String args[])
   {
       //Date object d1 that adds 20 days to date
       Date d1 = new Date();
       System.out.println(" Current Date: " + d1.print_date());
       d1 = d1.add_day(20);
       System.out.println("Increasing days by "+ 20 +" New Date: " + d1.print_date());
      
       //Date object d1 that adds 40 days to date
       Date d2 = new Date();
       System.out.println(" Current Date: " + d2.print_date());
       d2 = d2.add_day(40);
       System.out.println("Increasing days by "+ 40 +" New Date: " + d2.print_date());
      
       //Date object d1 that adds 400 days to date
       Date d3 = new Date();
       System.out.println(" Current Date: " + d3.print_date());
       d3 = d3.add_day(400);
       System.out.println("Increasing days by "+ 400 +" New Date: " + d3.print_date());
   }
}

Sample Output:

F:Java>java Date

Current Date: 6-1-1900
Increasing days by 20 New Date: 26-1-1900

Current Date: 6-1-1900
Increasing days by 40 New Date: 16-2-1900

Current Date: 6-1-1900
Increasing days by 400 New Date: 16-2-1901