Write a program that prints the calendar for a given year. The user will be promoted tochoose
Question:
Write a program that prints the calendar for a given year. The user will be promoted tochoose the calendar for the month or the year. If the year is selected, then the calendar for thegiven year will be displayed. If the month is selected then the user will be prompted to enter themonth, 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 achievefor 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 leapyear if it is divisible by 400 or if the year is divisible by 4 butnot 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 theweek assuming that Sunday is the zero day of the week. Tofigure out which day is the first day of the month you need todo the following:
a. Calculate the total number of days since 1800 up to themonth of the year that you are trying to print thecalendar
i. Calculate the total number of days since 1800up to one year prior to the given year. Notethat you also need to check if the year is a leapyear or not. For example, if the user enters 2014, we need to calculate how many days from 1800to 2013. Need a for loop and in the for loop youneed to check if the year is a leap year.
ii. Add the total days from the beginning of thegiven year up to the month. For example, if weare trying to print the calendar for the March2014, then we need to add the total number ofdays in Jan and Feb to whatever we calculatedin i.
iii. Whatever you get from ii add 3 to it (January1800 was on a Wednesday)
iv. Calculate the remainder of iii divided by 7, thisresult will tell you that first days of month isSun 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 andthe day for the first day of the month you can print thecalendar
3. Now you can write a method that accepts the number of the days in the month and thefirst 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 6thday 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 calendarfor the year or the month. Based on the user’s option display the result.
you can use this procedure
mport java.util.Scanner;
public class PrintCalenderShell
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
//prompt the user to enter the year
boolean repeat = true;
while (repeat)//repeat as long as the user wants to
{
//your code
}
}
/*this method prints the body of the calender for the given month*/
public static void printMonth(int year, int month)
{
//call the method printTitle with the values year and month
//call the method printMonthBody with the values year and month
}
/*this method prints the title of the days in each week(sunday Mon Tues Wed Thur Fir Sat)*/
public static void printMonthTitle(int year, int month)
{
//output the title for each month of the year
}
/*this method outputs the calender for each month of the year*/
public static void printMonthBody(int year, int month)
{
//call the method getStartDay to get the start day for each month of the year
//call the method print to print the calender for the month and pass the first day of the month as the parameter
}
/*prints the calendar for the given month*/
public static void print(int startDay, int year, int month)
{
// call the method getNumberOfdaysInMonth to finde out the number of the days in the month
//use a for loop to print spaces up to the start day for each month
//use another for loop to print the rest of the days in the calender
//if the month is November calculate the thanksgiving day and output }
}
/*this method calculates the date for thanksgiving, you need to use
a switch statment. Thanksgiving is always on fourth Thursday of November.
depending on the first day of the month a constant needs to be added to 3 * 7
since we have three full weeks. if the first day of the month is on a
Sunday then for that month thanksgiving will be 5 + 3 *7. */
public static int thanks(int startDay)
{
return 0;
}
/*you can use this method from you previous HW */
public static String getMonthName(int month)
{
return "";
}
/*this method returns the atrt day of the month*/
public static int getStartDay(int year, int month)
{
//call the method getTotalNumberOfDays and store it in a variable called start
//add 3 to the variable start
//return start % 7
return 0;
}
/*returns the total number of the days up to the given month in the given year */
public static int getTotalNumberOfDays(int year, int month)
{
return 0;
}
/*this method return the number of the days in the given month of the
given year. take leap year into consideration*/
public static int getNumberOfDaysInMonth(int year, int month)
{
return 0;
}
/*returns true if the given year is leap, false otherwise*/
public static boolean isLeapYear(int year)
{
//refer to the HW description
return false;
}
}
Database Systems A Practical Approach to Design Implementation and Management
ISBN: 978-0132943260
6th Edition Global
Authors: Thomas Connolly, Carolyn Begg