This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Printing a Calendar in Java  RSS feed

 
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this problem, you will be creating two methods: printMonth and printYear.

For printMonth(int year, int month, int janFirst, boolean leapYear), the int year represents the year in question, month will be a number from 1-12 representing the month (January-December).  janFirst will be a number from 0-6 (Sunday-Saturday) representing the day of the week of January 1st, and leapYear will be true if the year is a leap year.

printMonth(2016, 2, 5, true) should print:

    February 2016                                                              
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

printYear(int year, int janFirst, boolean leapYear) is similar.  However, it will indicate special holidays with certain characters:

President’s Day   *p Third Monday in Feb
Veteran’s Day     *v November 11
Thanksgiving     *t 4th Thursday in November
Memorial Day   *m Last Monday in May
Christmas           *c December 25
New Years           *y January 1st
4th of July           *4 July 4th
Labor Day           *L First Monday in September
Tax Day           *x April 15th (16th if Sunday, 17th if Saturday)

So, printYear(2013, 2, false) should print:
                             
                                              2013

       January                         February                        March
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
          *y   2   3  4   5                            1  2                              1  2
  6   7   8   9 10 11 12    3    4  5    6   7  8  9    3     4  5    6  7   8  9
13 14  15 16 17 18 19  10 11 12 13 14 15 16   10 11 12 13 14 15 16
20 21 22 23 24 25 26   17 *p 19 20 21 22 23   17 18 19 20 21 22 23
27 28 29 30 31            24 25 26 27 28             24 25 26 27 28 29 30
                                                                     31
        April                                May                              June
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1   2   3   4  5   6                    1  2  3  4                                  1
  7   8   9 10 11 12 13    5    6  7   8  9 10 11      2   3   4   5   6  7   8
14 *x 16 17 18 19 20   12 13 14 15 16 17 18      9 10 11 12 13 14 15
21 22 23 24 25 26 27   19 20 21 22 23 24 25    16 17 18 19 20 21 22
28 29 30                      26*m 28 29 30 31        23 24 25 26 27 28 29
                                                                     30
        July                                August                     September
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1   2   3 *4  5  6                         1  2   3     1  *L  3   4   5  6   7
7  8  9 10 11 12 13       4   5   6    7  8  9  10     8  9 10 11 12 13 14
14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
28 29 30 31                 25 26 27 28 29 30 31   29 30

       October                         November                    December
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
            1  2   3   4  5                             1  2     1   2   3   4   5   6  7
  6   7   8  9 10 11 12     3   4   5   6   7   8  9     8   9 10 11 12 13 14
13 14 15 16 17 18 19   10 *v 12 13 14 15 16    15 16 17 18 19 20 21
20 21 22 23 24 25 26   17 18 19 20 21 22 23    22 23 24 *c 26 27 28
27 28 29 30 31            24 25 26 27 *t  29 30    29 30 31

 
author & internet detective
Sheriff
Posts: 38564
654
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victoria,
CodeRanch is Not a Code Mill While we are happy to answer specific questions, nobody is going to write your project for you. So what more specific question do you have?
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have attempted doing:


I don't know exactly how to display all the dates starting from the beginning date of each month. Also, the method I am using to find the first day of each month doesn't seem to work. Is anyone able to help?
 
Marshal
Posts: 60116
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got all your class members static?
Have you been through the Java™ Tutorials, because I think you will find lots of useful information there?
 
Campbell Ritchie
Marshal
Posts: 60116
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write == true or == false. Both are poor style and error‑prone; you might write = instead of == by mistake.
Never
if (b == true) ...
always
if (b) ...
Never
if (b == false)...
always
if (!b)...
Note that expressions are easier to read if you can avoid the bang sign ! for not.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why have you got all your class members static?
Have you been through the Java™ Tutorials, because I think you will find lots of useful information there?



All my classes/methods are static because the main method needs to be able to access them. Without the static, the main doesn't recognize them.
 
Campbell Ritchie
Marshal
Posts: 60116
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is bad design, I am afraid. It means you have too much code in the main() method.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is bad design, I am afraid. It means you have too much code in the main() method.



I don't see how that is. All the main method is doing is executing the two methods printMonth() and printYear().
 
Rancher
Posts: 1878
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem as per my understanding is your return statement is out side the loop, so it will print only when the loop terminates.  You either need some sort of recursive function or the print statement should be inside the loop itself.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:One problem as per my understanding is your return statement is out side the loop, so it will print only when the loop terminates.  You either need some sort of recursive function or the print statement should be inside the loop itself.



I changed the return statement and added a bunch of System.out.print() statements. This is what I have now:



Although the code works for February 2016, it does not seem to do so for most other dates. How am I able to revise this?
 
Swastik Dey
Rancher
Posts: 1878
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is happening in the above line?
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:

What is happening in the above line?



I was trying to find the first date of the month, and I think that this is the part of the code that needs work.
 
Swastik Dey
Rancher
Posts: 1878
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason behind starting from January?  Why not start from the 1st of the given month and year itself.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:What is the reason behind starting from January?  Why not start from the 1st of the given month and year itself.



According to my assignment, I'm required to have a user input the day that January 1st is on and figure out the other first days based on January 1st.
 
Swastik Dey
Rancher
Posts: 1878
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok..then even things are getting complicated.  You can use Calendar, class it would be much easier.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:Ok..then even things are getting complicated.  You can use Calendar, class it would be much easier.



Yeah, it would probably be easier to do that, but imports besides java.util.scanner aren't allowed (you can probably feel my pain). I just tried this:

       

In this piece of code, I tried to add the days of each month to 'd'. I then modded it to find the day that the month should start at. However, this still doesn't yield the right days, so I'm unsure how to fix this.
 
Swastik Dey
Rancher
Posts: 1878
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What value janFirst contain.   Is this fixed value.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:What value janFirst contain.   Is this fixed value.



No, it depends on what year it is. The user will have to input that value. For example, 2016's janFirst is 5, or Thursday.
 
Bartender
Posts: 620
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that it is not necessary for the caller to specify what day January 1 is (not only that, it is just wrong to do so - the caller could easily enter the incorrect day).
Knowing that January 1, 2018 was a Monday, it is not too difficult to deduce what day January 1 is for any specified year.

It is also not necessary to include a parameter that specifies whether it is a leap year (that also allow the caller to make an error).
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:Note that it is not necessary for the caller to specify what day January 1 is (not only that, it is just wrong to do so - the caller could easily enter the incorrect day).
Knowing that January 1, 2018 was a Monday, it is not too difficult to deduce what day January 1 is for any specified year.

It is also not necessary to include a parameter that specifies whether it is a leap year (that also allow the caller to make an error).



Sorry, it would be easier to have an automatic calendar that is not based on user input, but that is a requirement of the assignment.
 
Campbell Ritchie
Marshal
Posts: 60116
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Victoria Li wrote:. . . . that is a requirement of the assignment.

Many of us believe that one of the things one should never do is write one's own date and time classes. But you are stuck with it.
 
Swastik Dey
Rancher
Posts: 1878
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your requirement is , considering the given value for Jan you have to display the calendar of given month?
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:So your requirement is , considering the given value for Jan you have to display the calendar of given month?



Yep! I just can't get the first day for each month right.
 
Rancher
Posts: 2828
96
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple way without calculations is to have a 2D array month[][], where the first index is the month, and the second index the number of days in that month. So month[0] = new int[31], month[1] = new int[28/29], et cetera. Then loop through all the months and days, starting with day janFirst.
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:A simple way without calculations is to have a 2D array month[][], where the first index is the month, and the second index the number of days in that month. So month[0] = new int[31], month[1] = new int[28/29], et cetera. Then loop through all the months and days, starting with day janFirst.



Oh! I completely forgot about ragged arrays! I'll try that. Thanks
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've got this code now:



It works for me, but now I need to print out the entire calendar:

printYear(int year, int janFirst, boolean leapYear) is similar.  However, it will indicate special holidays with certain characters:


So, printYear(2013, 2, false) should print:


The special characters (i.e. *p, *v, etc.) should be in red.

Any idea how to add in the special characters using the shortest amount of characters?
 
Victoria Li
Greenhorn
Posts: 22
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, calling printMonth() in printYear() will print out the months, but in a vertical line. Any idea how to make it print in a 4*3 formation (4 down, 3 across)
 
Piet Souris
Rancher
Posts: 2828
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about printing a month to a String[], that later can be printed on screen or paper? You just calculate how many lines maximum it would take to print a month (including headers) and how long the longest line is. Once you have that, you can print the months any way you like by combining the strings.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!