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

start day method 1. Write a program that prompts the user to enter a month (1-12

ID: 3890244 • Letter: S

Question

start day method

1. Write a program that prompts the user to enter a month (1-12) and a year (e.g., 2012) and then displays a calendar for that month and year as illustrated below: Januay 2012 Sun Mon Tue Ved Thu Fri Sat 8 9 10 11 12 13 14 15 16 17 18 19 20 1 22 23 24 2S 2E 27 20 9 30 31 Your program must use the following methods: void printMonthCalendar int month, int year) Displays a calendar like the one above fora specified month and year. void printMonthHeader( int month, int year Displays the header information ( month, year line separator, 3-character day names) for a void printMonthBody( int month, int year) String getMonthName( int month) int getStartDay( int month, int year) Displays the days in the calendar associated with thec Returns the name of the month for a specified month number (e.g., returns March for m 3) Returns the day of week number (1-Monday, ..7- Sunday) of the first day of month/year int getNumDayslnMonthl int month, int year) Returns the number of days in a specified month and year. Le Returns true if the specified year is a leap year, and returns false otherwise. boolean isLeapYear( int year) Code for the getStartDay() method can be downloaded from the course website. You must write the code for the remaining methods. Be sure to clearly document your code and your methods.

Explanation / Answer

//Please see the code below:

import java.util.Scanner;

public class displayCalendar
{
/**Main method*/
public static void main(String[] args)
{
    // The user enters year and month
   Scanner in = new Scanner(System.in);
    System.out.print("Enter full year: ");
    int year = in.nextInt();
    System.out.print("Enter month in number between 1 and 12: ");
    int month = in.nextInt();

    // Print calendar for the month of the year
    printMonthCalendar(year, month);
}

/**Print the calendar for a month in a year*/
static void printMonthCalendar(int year, int month)
{
    // Get start day of the week for the first date in the month
    int startDay = getStartDay(year, month);

    // Get number of days in the month
    int numOfDaysInMonth = getNumOfDaysInMonth(year, month);

    // Print headings
    printMonthHeader(year, month);

    // Print body
    printMonthBody(startDay, numOfDaysInMonth);
}

/**Get the start day of the first day in a month*/
static int getStartDay(int year, int month)
{
    // Get total number of days since 1/1/1800
    int startDay1800 = 3;
    long totalNumOfDays = getTotalNumOfDays(year, month);

    // Return the start day
    return (int)((totalNumOfDays + startDay1800) % 7);
}

/**Get the total number of days since Jan 1, 1800*/
static long getTotalNumOfDays(int year, int month)
{
    long total = 0;

    // Get the total days from 1800 to year -1
    for (int i = 1800; i < year; i++)
    if (isLeapYear(i))
      total = total + 366;
    else
      total = total + 365;

    // Add days from Jan to the month prior to the calendar month
    for (int i = 1; i < month; i++)
      total = total + getNumOfDaysInMonth(year, i);

    return total;
}

/**Get the number of days in a month*/
static int getNumOfDaysInMonth(int year, int month)
{
    if (month == 1 || month==3 || month == 5 || month == 7 ||
      month == 8 || month == 10 || month == 12)
      return 31;

    if (month == 4 || month == 6 || month == 9 || month == 11)
      return 30;

    if (month == 2)
      if (isLeapYear(year))
        return 29;
      else
        return 28;

    return 0; // If month is incorrect.
}

/**Determine if it is a leap year*/
static boolean isLeapYear(int year)
{
    if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
      return true;

    return false;
}

/**Print month body*/
static void printMonthBody(int startDay, int numOfDaysInMonth)
{
    // Pad space before the first day of the month
    int i = 0;
    for (i = 0; i < startDay; i++)
      System.out.print("    ");

    for (i = 1; i <= numOfDaysInMonth; i++)
    {
      if (i < 10)
        System.out.print("   " + i);
      else
        System.out.print(" " + i);

      if ((i + startDay) % 7 == 0)
        System.out.println();
    }

    System.out.println();
}

/**Print the month title, i.e. May, 1999*/
static void printMonthHeader(int year, int month)
{
    System.out.println("         "+getMonthName(month)+", "+year);
    System.out.println("-----------------------------");
    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}

/**Get the English name for the month*/
static String getMonthName(int month)
{
    String monthName = null;
    switch (month)
    {
      case 1: monthName = "January"; break;
      case 2: monthName = "February"; break;
      case 3: monthName = "March"; break;
      case 4: monthName = "April"; break;
      case 5: monthName = "May"; break;
      case 6: monthName = "June"; break;
      case 7: monthName = "July"; break;
      case 8: monthName = "August"; break;
      case 9: monthName = "September"; break;
      case 10: monthName = "October"; break;
      case 11: monthName = "November"; break;
      case 12: monthName = "December";
    }

    return monthName;
}
}

OUTPUT:

Enter full year: 2014
Enter month in number between 1 and 12: 2
         February, 2014
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
                           1
   2   3   4   5   6   7   8
   9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28