Question: Refactor the drawMonth ( int month ) and drawRow ( int week, int startDay ) methods to make use of the multi - dimensional array

Refactor the drawMonth(int month) and drawRow(int week, int startDay) methods to make use of the multi-dimensional array that represents the calendar month. Pre-fill 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 populateCalendarArray(int[][] calendar) that takes a 2D array as input and populates it with the appropriate day numbers. Use a nested loop to fill the array, ensuring each row contains 7 days.
public class Calendartwo {
// Function to display month and draw calendar row by row
public static void drawMonth(int month){
//System.out.println(""+ month +"")
System.out.println("===================================================================================================");
if(month<10){
System.out.println("--------------------------------------------------"+ month +"------------------------------------------------");
} else {
System.out.println("--------------------------------------------------"+ month +"-----------------------------------------------");
}
System.out.println("===================================================================================================");
for(int i=0; i <5; i++){
drawRow(i);
System.out.println("===================================================================================================");
}
}
// Function to display calendar row
public static void drawRow(int row){
// Calculate the start of the row by multiplying the row number by 7(0-based index for weeks).
int rowStart = row*7;
// Calculate the end of the row by adding 7 days to rowStart.
int rowEnd = rowStart+7;
// Outer loop to print each of the three lines for the row cells.
for(int i=0; i<3; i++){
// Scanner loop to iterate over each day in the current row (week).
for(int j=rowStart; j 31){
// Print an empty cell with padding if the day is beyond 31.
System.out.print("|");
} else {
// Print the top or bottom line for each cell (empty cell with padding).
if(i==0|| i==2){
System.out.print("|");
} else {
// For the middle line, print the day number with padding after it.
System.out.print("|"+(j+1)+"");
}
}
// Add an extra space for single-digit day numbers to maintain alignment.
if((j+1)<10){
System.out.print("");
}
// Add an extra space for days between 10 and 31 on the top and bottom lines.
if((j+1)>=10 && (j+1)<=31 && (i==0|| i==2)){
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 displayDate(int month, int day){
System.out.println("Month: "+ month);
System.out.println("Day: "+ day);
}
// Function to get month from the provided date
public static int monthFromDate(String date){
int index = date.indexOf('/');
int month = Integer.parseInt(date.substring(0,index));
return month;
}
// Function to get day from the provided date
public static int dayFromDate(String date){
int index = date.indexOf('/');
int day = Integer.parseInt(date.substring(index+1));
return day;
}
public static void main(String[] 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 Scanner(System.in);
/

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 Programming Questions!