Complete the printMonth(int numDays, int startDay) method below to print a calen
ID: 3542630 • Letter: C
Question
Complete the printMonth(int numDays, int startDay) method below to print a calendar month. The
parameters numDays and startDay indicate how many days are in the month, and which day of the week
the month starts on (Sunday==0, Monday==1, etc.). Make sure that the printout lines up vertically. For
example, calling printMonth(31, 3) should result in printing:
Su Mo Tu We Th Fr Sa
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
public static void printMonth(int numDays, int startDays)
{
}
Explanation / Answer
public class printer
{
public static void printMonth(int numDays, int startDays)
{
System.out.printf("Su Mo Tu We Th Fr Sa ");
for(int i=0; i<startDays; i++)
System.out.printf(" ");
for(int i=1; i<=numDays; i++)
{
System.out.printf("%2d ",i);
if((i+startDays)%7==0)System.out.println();
}
}
public static void main(String[] args)
{
printMonth(31,3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.