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

Import algs12. Date) write a program called PrintCalendar that prompts the user

ID: 3836312 • Letter: I

Question

Import algs12. Date) write a program called PrintCalendar that prompts the user for a start date and an end date and then prints all of the dates between them (inclusive), with seven tab-separated dates on each line. Here is an example of a run of the program: Enter a starting date: 1/1/2015 Enter an ending date: 3/1/2015 To help with this, here is (part of) the API for the algs12. Date class: public Date(int month, int day, int year): This constructs a Date object from the values given. public Date (String datestring): This constructs a Date object from a String formatted as an American date, e.g., "5/6/2017" would create a Date object for May 6th, 2017. public Date next (): When called with a Date object, this returns a new Date object representing the next day. public boolean equals (Date that): Returns true if the Date object for which it is called is equal to the that Date object. public boolean is Before (Date that): Returns true if the Date object for which it is called is strictly before the that Date object. Before printing the calendar, your program should check that the first date is not after the second date. If it is, it should print an error message and not print the calendar.

Explanation / Answer

PROGRAM CODE:

PrintCalendar.java

package date;

import java.util.Scanner;
public class PrintCalendar {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       String startDate, endDate;
       System.out.print("Enter the start date: ");
       startDate = input.next();
       System.out.print("Enter the end date: ");
       endDate = input.next();
      
       int startD, startM, startY, endM, endD, endY;
       String start[] = startDate.split("/");
       String end[] = endDate.split("/");
       startM = Integer.valueOf(start[0]);
       startD = Integer.valueOf(start[1]);
       startY = Integer.valueOf(start[2]);
       endM = Integer.valueOf(end[0]);
       endD = Integer.valueOf(end[1]);
       endY = Integer.valueOf(end[2]);
      
       Date StartDate = new Date(startM, startD, startY);
      
       Date EndDate = new Date(endM, endD, endY);
       if(StartDate.isBefore(EndDate))
       {  
           int counter = 0;
           while(!StartDate.equals(EndDate))
           {
               System.out.print(StartDate + " ");
               StartDate = StartDate.next();
               counter++;
               if(counter%7==0)
                   System.out.println();
              
           }
           System.out.println(StartDate);
       }
       else
           System.out.println("Start date is after the end date ");
   }

}

OUTPUT:

Enter the start date: 1/1/2015
Enter the end date: 1/21/2015
1/1/2015   1/2/2015   1/3/2015   1/4/2015   1/5/2015   1/6/2015   1/7/2015  
1/8/2015   1/9/2015   1/10/2015   1/11/2015   1/12/2015   1/13/2015   1/14/2015  
1/15/2015   1/16/2015   1/17/2015   1/18/2015   1/19/2015   1/20/2015   1/21/2015