Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a problem lining up the numbers in calendar.... Instruction. In order to

ID: 3645583 • Letter: I

Question

I have a problem lining up the numbers in calendar....



Instruction.
In order to print out a calendar for one month, you need to know two things: the first day of the month and the number of days in the month. Given this information, you can print out the calendar for that month. For example, if the month has 30 days and starts on Wednesday, then the calendar output should be:


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



STATE
the number of days in the month (an integer between 28-31)
the first day of the month (an integer between 0-6, Sun=0, Mon=1, Tues=2, Wed=3, Thurs=4, Fri=5, Sat=6)

BEHAVIOR
A standard constructor with two parameters to set the state of the Month directly.
A method named getFirstDayOfNextMonth that returns the day that the NEXT month should start on.
A method named printMonth that prints out the calendar for that month using a loop. That is, you should not just have 28 different sets of println() statements that print the calendar out directly.




//Month.java
public class Month {
public int daysPerMonth;
private int firstDay;
int i;
static String [] day = {"Sun","Mon","Tues","Wed","Thurs","Fri","Sat"};
int nextDay;
public Month(int daysPerMonth, int firstDay)
{
this.daysPerMonth = daysPerMonth;
this.firstDay = firstDay;
}
public int getFirstDayOfNextMonth() {
int nextDay = firstDay;
for(int i = 0; i < daysPerMonth; i++) {
if(nextDay + 1 > day.length - 1) nextDay = 0;
else nextDay++;
}
return nextDay;
}
public void printMonth() {
System.out.print("Su Mo Tu We Th Fr Sa");
System.out.println(" " + "---------------------------");
for(int i=0;i<firstDay;i++) //we're printing number of spaces using tab( )

{
System.out.print(" ");
}
/*the dates will be printed from value 1 corresponding to the starting day of the month*/
for(int i=0;i<=(6-firstDay);i++)
{
System.out.print(i+1);
}
i++;
System.out.println();//moving to the next line after reaching the last day of the week
for(int j=i;j<=daysPerMonth;j++)
{
//printing the rest of the days in the same manner

if(j==daysPerMonth)
break;
int rec=(6-firstDay);
if(j%7==rec+1)
System.out.println();
else
{
System.out.print(j);

}

}

}
}










//Calendar.java
import java.util.Scanner;

public class Calendar
{
static final int FEBRUARY = 2;
static final int MONTHS_PER_YEAR = 12;

static int [] daysPerMonth =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

static String [] months =
{"", " Januray", "February", " March", " April",
" May", " June", " July", " August",
"September", " October", "November", "December"};

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the year? (yyyy): ");
int year = in.nextInt();

int firstDay = firstDayOfYear(year);

// February: Check for leap year
daysPerMonth [FEBRUARY] = isLeapYear(year) ? 29 : 28;

for (int i=1; i <= MONTHS_PER_YEAR; i++) {
Month month = new Month(daysPerMonth[i], firstDay);
System.out.println(" "+months[i]+" "+year);
month.printMonth();
firstDay = month.getFirstDayOfNextMonth();
}
}

private static boolean isLeapYear(int year) {
return ((year%4 == 0) && (year%100 != 0)) || (year%400 == 0);
}

private static int firstDayOfYear(int year) {
return (2 * (13) + (3 * (13+1)/5) + (year-1) + ((year-1)/4)
- ((year-1)/100) + ((year-1)/400) + 2) % 7;
}
}
Report Abuse

Explanation / Answer

So, print(" ") doesn't work. You should have in your loop an if statement that adds one space or two spaces depending on if you're working with a single digit or a two digit number. There is a fancier way of doing it with printf, but I haven't used it in a while so I don't know if I can help you as much.

That being said, there are a couple other problems with your code:
1. Because you have a "int i" in your for loop in the printMonth() function, your "i" value is getting erased. (Hence, when your code runs, you actually get the first week twice, at least for 2012).

Your code should look like this for this section:
/*the dates will be printed from value 1 corresponding to the starting day of the month*/

int i;
for (i = 0; i <= (6 - firstDay); i++) { System.out.print(i + 1 + " "); }
i++;
System.out.println();//moving to the next line after reaching the last day of the week
for (int j = i; j <= daysPerMonth; j++) { //printing the rest of the days in the same manner

If you define i instead the for loop, then its value will not carry over to the next for loop where you need j to be what i is.

2. After the first week, your subsequent weeks do not loop around (again, for 2012). This is because in 2012, firstDay = 0 and rec = 6, whih means that you're waiting for when j % 7 == rec + 1 = 7. This will never happen because 7 % 7 = 0. Simple way to fix that is to have

if (j % 7 == (rec + 1) % 7) instead.

3. You're currently not printing out the dates for Saturday.

You need to be doing System.out.println(j) instead of System.out.println();

4. You have an extra println that you don't need between when you finish first week and when you start the other weeks. That first if statement should give you that println you need.

5. With the rest of the Saturdays being printed, you only need to print out all the days before Saturday for the first week. Otherwise you'll get double Saturdays on the first week.

6. You don't need the j == daysPerMonth. the for loop should take care of that. The way you have it now, it never prints the last day.

Sorry, this is probably more than you were asking for. Hope it helps.

Here's the final code for printMonth()

public void printMonth() {

System.out.print("Su Mo Tu We Th Fr Sa");

System.out.println(" " + "---------------------------");

for (int i = 0; i < firstDay; i++) { System.out.print(" "); }

int i;

for (i = 0; i <= (6 - firstDay) - 1; i++) { System.out.print(i + 1 + " "); }    

for (int j = i + 1; j <= daysPerMonth; j++) {

int rec = (6 - firstDay);

if (j % 7 == (rec + 1) % 7) { System.out.println(j); }

else {

if (j < 10) System.out.print(j + " ");

else System.out.print(j + " "); }

}

}