Question: I submitted this code for a project we're doing for Java where we need to print out a calendar for the month and year asked

I submitted this code for a project we're doing for Java where we need to print out a calendar for the month and year asked by the user of the program. My professor wrote back this "Good job. Please don't use static methods. Please correct an resubmit. Also, I don't think you need to test for leap year if you use the methods suggested in the assignment. It is not wrong to do so but just not necessary.," I am struggling with what he is saying here and need help. import java.util.Scanner; public class PrintCalendar { private static int SIZE = 3; private static char HORIZONTAL = '-'; private static char VERTICAL = ' '; private static String[] monthNames = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; private static String days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" }; // check if a year is leap year or not public static boolean isLeapYear(int year) { return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); } public static void printCharacter(char c, int count) { while (count-- > 0) { System.out.print(c); } } public static void drawHorizontalLine() { int width = SIZE * 7 + 1; printCharacter(HORIZONTAL, width); } public static void drawMonth(int year, int month, int daysToSkip, int maxDays) { int center = SIZE * 2; printCharacter(' ', center); System.out.print(monthNames[month] + " "); for (int i = 0; i < days.length; i++) { System.out.printf("%2s ", days[i]); } System.out.println(); int start = 1; while (start <= maxDays) { if (start == 1) { drawRow(start, maxDays, daysToSkip); start += 7 - daysToSkip; } else { drawRow(start, maxDays, 0); start += 7; } } System.out.println(); } public static void drawRow(int start, int maxDaysInMonth, int skip) { String str = ""; int day = start; for (int cell = 1; cell <= 7; cell++) { str = ""; if (cell > skip && day <= maxDaysInMonth) { str += day++; } while (str.length() < SIZE - 1) str = " " + str; str += VERTICAL; System.out.print(str); } int height = SIZE / 2; for (int h = 2; h <= height; h++) { System.out.print(" |"); for (int cell = 1; cell <= 7; cell++) { str = ""; while (str.length() < SIZE - 1) str = " " + str; str += VERTICAL; System.out.print(str); } } System.out.print(" "); } public static void drawCalendar(int year, int month) { int startingDay = getStartingDay(month, year); int maxDays = getMaximumDays(month, year); drawMonth(year, month, startingDay, maxDays); } public static int getStartingDay(int month, int year) { if (month == 1) { month = 13; year--; } if (month == 2) { month = 14; year--; } int q = 1; int m = month; int k = year % 100; int j = year / 100; int h = q + 13 * (m + 1) / 5 + k + k / 4 + j / 4 + 5 * j; h = (h - 1) % 7; return h; } public static int getMaximumDays(int month, int year) { int days = 31; switch (month) { case 2: days = 28; if (year % 4 == 0) { days = 29; if (year % 100 == 0 && year % 400 != 0) { days = 28; } } break; case 4: case 6: case 9: case 11: days = 30; default: break; } return days; } public static void main(String[] args) { System.out.println("Enter the year: "); Scanner in = new Scanner(System.in); int year = in.nextInt(); System.out.println("Enter the month: "); int month = in.nextInt(); drawCalendar(year, month); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!