Print a Calendar

From CompSciWiki
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Write a program that prints out a calendar month using nested loops.

 

...By Students

"I don't like Calendars. But I really like writing code! This question was a happy medium.

I used to have huge problems printing grids. When I would try and print a bitmap image (one long list of colours one after the other) sometimes they would be drawn sideways, reversed, or upside down. It was all because I used to get confused what loop controlled what axis in my application. Nowadays I instinctively know, but it literally took me years to get to this point.

That said, nested loops are ideal for going over multiple groups of items. If you're dealing with items in a grid, individual servers in multiple cities, or how many blueberries there are in everyone's ice cream cone, you'll need nested loops."

Solution

We'll start this one easy. Make a function that takes two parameters: One that says what day of the week the first day of the month is, the other how many days are in the particular month. Your start code should look like:

 class CalendarPrinter
{
	public static void main(String args[])
	{
		System.out.print("January 2010:\n\n");
		printMonth(6, 31);
	}

	function printMonth(int firstDay, int numDays)
	{

	}
} 

Next your goal is to print a 7 column by 6 row grid to System.out with two loops. your output should look something like the following:

 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 

That's the meat of this application. I used to have huge problems printing boxes in the correct dimensions. Just remember that the inner loop does its thing before the outer loop does, so if we were drawing a picture from left to right, the outer loop would control what row we were drawing, and the inner loop would deal with actually drawing the row.

Now, the next step is to have a counter so our zeros are actually incrementing numbers. Just declare a counter and increment that puppy in the right spot. You'll figure out where to do the incrementing yourself I imagine.

Now the tricky part is when do you start printing numbers? That's where our firstDay variable in our function prototype comes in. Just print a blank space if your current number is less than firstDay. Your output now should look like this:

 0 0 0 0 0 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 

Now, January 2010 didn't start at day six, but we need to keep track of what cell we're on. We could have a second counter, but I'm more of an offset man myself, so I would just subtract firstDay + 1.

So what's left? If you run your code, you'll see we keep counting far past the 31st. You deal with that problem by introducing new tests into one of your loops. You might know that the inner part of your 'for' statement is a Boolean logic test.

 // Run while loop is less than 10
for (int loop = 0; loop < 10; loop++)

// Run while loop is less than 10 and more than b
for (int loop = 0; loop < 10 && loop > b; a++) 

So you really just have to know when to stop printing days. I'll let you figure that out. After that jazz is done, so are you! You just printed the calendar month for January 2010.

Now, you're probably going to hate my solution code to this. There's some stuff in there for output formatting that make it a little convoluted and confusing. Those extras were not the point of this question. What I wanted from you was to have seven numbers drawn per line except the first and last weeks which might have less.

Code

Solution Code

Back to the Program-A-Day homepage