Forums Register Login

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

+Pie Number of slices to send: Send
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
Description:
File size: 24 Kbytes
[Download calendar-results.tiff] Download Attachment
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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)?
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 7050 times.
Similar Threads
Exception in thread "main" java.lang.NullPointerException
Problem with saving a state in calendar program.
For Loops making me loopy
The Art & Science of Java Chapter 4 Exercise 14 Checkers
Checking Date
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:09:47.