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

Problem: Write a program that prints the calendar for a given year. The user wil

ID: 3803510 • Letter: P

Question

Problem: Write a program that prints the calendar for a given year. The user will be promoted to
choose the calendar for the month or the year. If the year is selected, then the calendar for the
given year will be displayed. If the month is selected then the user will be prompted to enter the
month, then the calendar for the given month will be displayed.
Top down design: Break down the problem into sub problems and state what you will achieve
for each of the sub problems First we need to break down into two sub problems.
1. Get the user’s input
a. Read the input(year)
2. Print the calendar
Print the calendar for the year
a. Print the calendar for each month of the year
i. Print the title for each month in the year.
1. Print the month and the year
2. Print “Sunday Monday Tuesday Wednesday Thursday Friday
Saturday”.
ii. Print the body for the month
1. Get the number of the days for each month of the year
2. Need to know if the year is a leap year or not. A year is a leap
year if it is divisible by 400 or if the year is divisible by 4 but
not divisible by 100
3. Get the number for the first day in the month. For example,
November 1st was on a Saturday which is the 6th day of the
week assuming that Sunday is the zero day of the week. To
figure out which day is the first day of the month you need to
do the following:
a. Calculate the total number of days since 1800 up to the
month of the year that you are trying to print the
calendar
i. Calculate the total number of days since 1800
up to one year prior to the given year. Note
that you also need to check if the year is a leap
year or not. For example, if the user enters 2014,
we need to calculate how many days from 1800
to 2013. Need a for loop and in the for loop you
need to check if the year is a leap year.
ii. Add the total days from the beginning of the
given year up to the month. For example, if we
are trying to print the calendar for the March
2014, then we need to add the total number of
days in Jan and Feb to whatever we calculated
in i.
iii. Whatever you get from ii add 3 to it (January
1800 was on a Wednesday)
iv. Calculate the remainder of iii divided by 7, this
result will tell you that first days of month is
Sun or Mon or Tue or Wed or Thu or Fri or Sat.
4. Determine the date for thanksgiving for November.
5. Now that you know the number of the days in the month and
the day for the first day of the month you can print the
calendar.
3. Now you can write a method that accepts the number of the days in the month and the
first day of the month, then print the body
a. 1. If the first day of the month is for example on a Saturday which is the 6th
day of the week then you need to output 5 blanks “ “.
b. You start printing from 1 to the last day of the month.
Add appropriate codes in the main method to give the user the option of displaying the calendar
for the year or the month. Based on the user’s option display the result.

you can use this procedure

Explanation / Answer

Here is the code for you:

import java.util.Scanner;
public class PrintCalendar
{
public static void main(String[] args)
{
// Prompt the user to enter year
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter year
System.out.print("Enter full year (e.g., 2016): ");
int year = scanner.nextInt();
// Prompt the user to enter month
//System.out.print("Enter month in number between 1 and 12: ");
//int month = scanner.nextInt();
// Print calendar for the month of the year
for(int i = 1; i <= 12; i++)
printMonth(year, i);
}
  
/** Print the calendar for a month in a year */
static void printMonth(int year, int month)
{
// Print the headings of the calendar
printMonthTitle(year, month);
// Print the body of the calendar
printMonthBody(year, month);
}
  
/** Print the month title, e.g., May, 1999 */
static void printMonthTitle(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;
}
  
/** Print month body */
static void printMonthBody(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 numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
// 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 <= numberOfDaysInMonth; i++)
{
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
  
/** 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;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
// Return the start day
return (totalNumberOfDays + startDay1800) % 7;
}
  
/** Get the total number of days since January 1, 1800 */
static int getTotalNumberOfDays(int year, int month)
{
int 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 + getNumberOfDaysInMonth(year, i);
return total;
}
  
/** Get the number of days in a month */
static int getNumberOfDaysInMonth(int year, int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;
else if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
else if (month == 2)
return isLeapYear(year) ? 29 : 28;
return 0; // If month is incorrect
}
  
/** Determine if it is a leap year */
static boolean isLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
}

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