Question: Refactor the drawMonth ( int month ) and drawRow ( int week, int startDay ) methods to make use of the multi - dimensional array
Refactor the drawMonthint month and drawRowint week, int startDay methods to make use of the multidimensional array that represents the calendar month. Prefill the array with day numbers before calling the methods that draw the calendar.
Replace the repeated use of day calculations in the drawMonth and drawRow methods with values retrieved from the arrays.
New Method:
Implement a new method called populateCalendarArrayint calendar that takes a D array as input and populates it with the appropriate day numbers. Use a nested loop to fill the array, ensuring each row contains days.
public class Calendartwo
Function to display month and draw calendar row by row
public static void drawMonthint month
Systemout.println month
System.out.println;
ifmonth
System.out.println month ;
else
System.out.println month ;
System.out.println;
forint i; i ; i
drawRowi;
System.out.println;
Function to display calendar row
public static void drawRowint row
Calculate the start of the row by multiplying the row number by based index for weeks
int rowStart row;
Calculate the end of the row by adding days to rowStart.
int rowEnd rowStart;
Outer loop to print each of the three lines for the row cells.
forint i; i; i
Scanner loop to iterate over each day in the current row week
forint jrowStart; j
Print an empty cell with padding if the day is beyond
System.out.print;
else
Print the top or bottom line for each cell empty cell with padding
ifi i
System.out.print;
else
For the middle line, print the day number with padding after it
System.out.printj;
Add an extra space for singledigit day numbers to maintain alignment.
ifj
System.out.print;
Add an extra space for days between and on the top and bottom lines.
ifj && j && i i
System.out.print;
Print the end of the row with a vertical bar and move to the next line.
System.out.println;
Function to display provided month and day
public static void displayDateint month, int day
System.out.printlnMonth: month;
System.out.printlnDay: day;
Function to get month from the provided date
public static int monthFromDateString date
int index date.indexOf;
int month Integer.parseIntdatesubstringindex;
return month;
Function to get day from the provided date
public static int dayFromDateString date
int index date.indexOf;
int day Integer.parseIntdatesubstringindex;
return day;
public static void mainString args
System.out.println;
System.out.println;
System.out.println;
System.out.println;
System.out.println;
System.out.println;
System.out.println;
System.out.println;
Create a Scanner object to read input
Scanner scanner new ScannerSystemin;
