• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a Calendar in Java using For Loops (Art and Science of Java Chapter 4 Ex. 12)  RSS feed

 
Joshua Friedman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

This is my first post. I am an absolute, total beginner in learning CS and am working through the art and science of Java book to teach myself Java, using a version of Stanford's cs106a class that's on itunes. I'm having a trouble with one of the problems.

I'm supposed to create a very basic calendar that includes the date of the month. On this calendar (stretched from Sunday to Saturday), the month would start on Friday. The number of weeks should also change based upon how many days there are in the month. Instead I get rows of squares, each containing a "0" My code is here:



What ends up happening under these conditions looks like the attached picture. Can anyone here please help me figure out what exactly is going on in these loops and why my dates won't show up properly?

As a related question, what exactly ARE I and J? I imagine the machine as drawing these squares left to right, then moving down, and drawing another row. Using the pause method shows me that's what is happening but as a beginner I'm feeling very, very lost. Any help would be appreciated!


*edit attached a picture of my results
Filename: calendar-results.tiff
File size: 25 Kbytes
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Answering your question regarding "what's i and j"... they act as a counter variable in the "for" loop.



The above prints 0 to 9.

From the code so far, the i and j for loops draw the calendar squares like you expected.

The code after that, the for loop with the k counter... I noticed the if statement is "k=>DAYS_IN_MONTH". Shouldn't this be "<=" because if k=0 and DAYS_IN_MONTH=31 the if statement will be always false, hence your label never gets increment.


If you have trouble doing this with the actual calendar, try doing it without it. Just write simple programs with for loops to print out the numbers in 7 columns, new line when the column counter gets to 7.

Hope this helps.

 
Joshua Friedman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang,

Thanks a lot for your reply. You're right, I DID make a mistake in the sign, but even if I change the sign the number never increments and just puts a number of whatever I assigned "date" to be in the square.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Then I think you should step back a bit and try the calendar output using simple for loop. I did a similar thing but took a while to get the offset right

To start you off:



Output given the "0" before the single digits are spaces


Can you figure out what condition to do the "new line" (line 18 in code)?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!