Problem: Write a program that prints the calendar for a given year. The user wil
ID: 3813275 • 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.
Get the user’s input
a. Read the input(year)
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.
Print the month and the year
Print “Sunday Monday Tuesday Wednesday Thursday Friday Saturday”.
ii. Print the body for the month
Get the number of the days for each month of the year
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
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
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.
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.
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.
Determine the date for thanksgiving for November.
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.
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
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 “ “.
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.
A program shell has been provided for you.
Here is a sample output
Here is the sample output:
Enter a valid year after 1800: 2006 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: n
Invalid choice
Do you have another year to print the calendar:yes/no-->yes Enter a valid year after 1800: 2006 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: m
*Enter the month of the year1..12-->90
*Enter the month of the year1..12-->56
*Enter the month of the year1..12-->5
May 2006
----------------------------
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 29 30 31
Do you have another year to print the calendar:yes/no-->yes Enter a valid year after 1800: 2017 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: y
January 2017
----------------------------
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
29 30 31
February 2017
----------------------------
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
March 2017
----------------------------
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 29 30 31
April 2017
----------------------------
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 29
30
May 2017
---------------------------- 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 29 30 31
June 2017
---------------------------- 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 29 30
July 2017
---------------------------- 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 29
30 31
August 2017
---------------------------- 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 29 30 31
September 2017
---------------------------- 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 29 30
October 2017
----------------------------
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
29 30 31
November 2017
----------------------------
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 29 30
**Thanksgiving day is on the 23th
December 2017
----------------------------
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 29 30
31
Do you have another year to print the calendar:yes/no-->yes Enter a valid year after 1800: 2018 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: m
*Enter the month of the year1..12-->7
July 2018
----------------------------
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
29 30 31
Do you have another year to print the calendar:yes/no-->yes Enter a valid year after 1800: 2006 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: m
*Enter the month of the year1..12-->2
February 2006
----------------------------
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
Do you have another year to print the calendar:yes/no-->yes Enter a valid year after 1800: 1999 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: y
January 1999
----------------------------
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 29 30
31
February 1999
---------------------------- 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
March 1999
---------------------------- 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 29 30 31
April 1999
---------------------------- 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 29 30
May 1999
---------------------------- 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 29
30 31
June 1999
---------------------------- 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 29 30
July 1999
----------------------------
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 29 30 31
August 1999
---------------------------- 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
29 30 31
September 1999
---------------------------- 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 29 30
October 1999
---------------------------- 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 29 30
31
November 1999
---------------------------- 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 29 30
**Thanks giving day is on the 25th
December 1999
----------------------------
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 29 30 31
Do you have another year to print the calendar: yes/no-->yes Enter a valid year after 1800: 2017 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: m
*Enter the month of the year1..12-->11
November 2017
----------------------------
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 29 30
**Thanks giving day is on the 23th
Do you have another year to print the calendar: yes/no-->no
Good Bye
Enter a valid year after 1800: 2006 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: n
Invalid choice
Do you have another year to print the calendar:yes/no-->yes Enter a valid year after 1800: 2006 Select one of the following options:
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your choice: m
*Enter the month of the year1..12-->90
*Enter the month of the year1..12-->56
*Enter the month of the year1..12-->5
May 2006
----------------------------
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
Explanation / Answer
/**
* The java program PrintCalenderShell that prompts user to enter
* year from 1800 . The ask user to print month or year.
* If user selects month, then prompt for number of month
* then prints the month. If user selects the year,
* then prints the year calendar to console output.
* */
//PrintCalenderShell.java
import java.util.Scanner;
public class PrintCalenderShell
{
public static void main(String[] args)
{
//create a Scanner class object
Scanner scanner = new Scanner(System.in);
String choice;
int year =0;
int month;
boolean repeat = true;
//repeat while loop until repeat is false
while (repeat)
{
//run unitl user enters 1800 or greater
do
{
System.out.println("Enter a valid year after 1800 :");
year=Integer.parseInt(scanner.nextLine());
} while(year <1800);
System.out.println("To get the month calendar press m or M");
System.out.println("To get the calendar for the year enter y or Y");
System.out.println("Enter your chocie:");
choice=scanner.nextLine();
//check if user enters y or Y
if(choice.equals("y")||choice.equals("Y"))
{
for (int i =1; i<=12; i++)
{
printMonth(year, i);
}
}
//check if user enters m or M
else if(choice.equals("m")||choice.equals("M"))
{
//run unitl user enters in a range of 1 and 12
do
{
System.out.println("*Enter the month of the year1..12");
month=Integer.parseInt(scanner.nextLine());
}while(month<=0 || month>12);
//calling method
printMonth(year, month);
}
System.out.println("Do you have another year to print the calender:yes/no");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("no"))
{
System.out.println("Good Bye");
//set false to repeat
repeat = false;
}
}
}
/**
* Method printMonth that takes year and month
* and calls printMonthTitle and printMonthBody */
public static void printMonth(int year, int month)
{
//call the method printTitle with the values year and month
printMonthTitle(year, month);
//call the method printMonthBody with the values year and month
printMonthBody(year, month);
}
/**
* Method :printMonthTitle
* Description : The method printMonthTitle that takes year
* and month and prints the header of the month
* */
public 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");
}
/**
* Method : printMonthBody
* Description:
* The method takes year and month and calls method
* getStartDay that returns the starting day of month
* and calls print method to print the month .
* */
public static void printMonthBody(int year, int month)
{
int startDay = getStartDay(year, month);
print(startDay, year, month);
}
/**
* Method :print
* Description : The method that takes start day, year
* and month and print the month of the given year.
* */
public static void print(int startDay, int year, int month)
{
int start = getStartDay(year, month);
int numOfDaysInMonth = getNumberOfDaysInMonth(year, month);
for(int i=0; i<start; i++)
{
System.out.print(" ");
}
for(int i=1; i<=numOfDaysInMonth; i++)
{
System.out.printf("%4d", i);
//print new line for every new week
if((i+start)%7 ==0)
{
System.out.println();
}
}
System.out.println();
}
/**
* Method : getMonthName
* Description : The method takes month as input
* and returns the name of the month as string.
* */
public static String getMonthName(int month)
{
String monthName = "";
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 = "Aubuest"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
}
return monthName;
}
//this method returns the start day of the month
public static int getStartDay(int year, int month)
{
//call the method getTotalNumberOfDays and store it in a variable called start
int start = getTotalNumberOfDays(year, month);
start= start+3;
//add 3 to the variable start
return start%7;
}
//returns the total number of the days up to the given month in the given year
public static int getTotalNumberOfDays(int year, int month)
{
int sum = 0;
//for loop goes from 1800 up to the year
for(int i=1800; i<=year; i++)
{
if(isLeapYear(i))
{
sum = sum+366;
}else{
sum = sum+365;
}
}
//add number of days in the year to sum, call the method leapyear
for(int i=1; i<month; i++)
{
sum = sum+getNumberOfDaysInMonth(year, i);
}
return sum;
}
/**
* Method : getNumberOfDaysInMonth
* Description : The method takes year an month and
* returns the number of days in the year
* */
public static int getNumberOfDaysInMonth(int year, int month)
{
int days = 0;
switch(month)
{
case 1: days = 30;
break;
case 2: if(isLeapYear(year)){
days = 29;
}else{
days = 28;
}break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
}
return days;
}
/**
* Method :isLeapYear
* Description : The method that takes year
* and returns true if year is leap year
* otherwise returs false.
* */
public static boolean isLeapYear(int year)
{
return(year %400==0 || year%4==0 && year%100!=0);
}
}
-------------------------------------------------------------------------------------
Sample Output:
Enter a valid year after 1800 :
2017
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your chocie:
m
*Enter the month of the year1..12
1
January 2017
----------------------------
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 29 30
Do you have another year to print the calender:yes/no
yes
Enter a valid year after 1800 :
2017
To get the month calendar press m or M
To get the calendar for the year enter y or Y
Enter your chocie:
y
January 2017
----------------------------
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 29 30
February 2017
----------------------------
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
March 2017
----------------------------
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 29 30 31
April 2017
----------------------------
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 29
30
May 2017
----------------------------
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 29 30 31
June 2017
----------------------------
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 29 30
July 2017
----------------------------
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 29
30 31
Aubuest 2017
----------------------------
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 29 30 31
September 2017
----------------------------
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 29 30
October 2017
----------------------------
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
29 30 31
November 2017
----------------------------
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 29 30
December 2017
----------------------------
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 29 30
31
Do you have another year to print the calender:yes/no
no
Good Bye
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.