1

I wrote this code above to get number of sundays appears in first day of the month. I don't get a correct result and dont have idea why? any suggestion?

import java.util.Calendar;

    public class Test{
          public static void main(String args[]) {
            int counter = 0;
            Calendar c = Calendar.getInstance();

            for (int i = 1900; i<=2000; i++){
                for (int j = 0; j <=11; j++){
                    c.set(i, j, 1);
                    int day_of_week = c.get(Calendar.DAY_OF_WEEK);
                    if(day_of_week==1){
                        counter++;
                    }
                }
            }
            System.out.println(counter);
          }   
    }
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Pulkownik
  • 33
  • 6
  • 3
    What result do you get? And what is the correct result? – MAV Feb 27 '14 at 17:18
  • Over 101 years, there have been 173 months where the first day of the month has been on a Sunday (is the result I'm getting from the above question) – Chris Forrence Feb 27 '14 at 17:22
  • 1
    For starters, change `day_of_week==1` to `day_of_week==Calendar.DAY_OF_WEEK.SUNDAY` . It may be clear to you that 1 represents Sunday, but it is not immediately clear to everyone reading your code. If you use the enumerated type, you are clearly referring to Sunday. – Rainbolt Feb 27 '14 at 17:22
  • Why `day_of_week==1`? Where did you get that 1 is Sunday? You should always use the constant `Calendar.SUNDAY`. – m0skit0 Feb 27 '14 at 17:24
  • 4
    Something to keep in mind (if this is coming from [Project Euler #19](http://projecteuler.net/problem=19)), the 20th century does not include 1900. Is the correct answer 171? – Chris Forrence Feb 27 '14 at 17:25
  • What result do you expect to get? – PearsonArtPhoto Feb 27 '14 at 17:25
  • @m0skit0 The Javadoc has a list of the values for the various enumerated types inside of Calendar. http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.util.Calendar.SUNDAY – Rainbolt Feb 27 '14 at 17:26
  • Correct answer is 171. And i forgot about one thing. The 1900.1.1 day is a Monday, the calandar.day_of_week for that date return 5, so i assume that sunday is a 4 and i paste wrong code if(day_of_week==1) here should be ==4. But it doesnt matter because the result still is not correct. – Pulkownik Feb 27 '14 at 17:26
  • java.util.Calendar: public static final int SUNDAY = 1 – Leos Literak Feb 27 '14 at 17:27
  • Somebody else had [nearly the same homework assignment](http://stackoverflow.com/q/21896589/642706). Most importantly, you'll learn to avoid using java.util.Date & .Calendar for serious work. Instead use [Joda-Time](http://www.joda.org/joda-time/) (or java.time in Java 8) as shown in [my answer to that other question](http://stackoverflow.com/a/21901080/642706). – Basil Bourque Feb 28 '14 at 03:19

2 Answers2

4

The 20th century includes years from 1901 to 2000. By amending your for-loop...

for (int i = 1901; i <= 2000; i++){

You will get 171 months.


In addition, using hard-coded values in code is generally a bad thing; look at some of PearsonArtPhoto's suggestions

Community
  • 1
  • 1
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
3

First of all, there's several things to make the code clearer that could be done. But I suspect the crux of the question is that you should start from the year 1901, as that's technically the first day of the century. As far as code clean up, here's a few tips:

  1. Use variable names that mean something, like year and month.
  2. Use constants, instead of just enumerating the value (Calendar.DAY_OF_WEEK.SUNDAY)

Put everything together, and you've got:

public class Test{
      public static void main(String args[]) {
        int counter = 0;
        Calendar c = Calendar.getInstance();
        int Day_Of_Month=1; //This makes it easier in case you want to tweak the test, to say, 

        for (int year = 1901; year<=2000; year++){
            for (int month = 0; month <12; month++){
                c.set(year, month, 1);
                int day_of_week = c.get(Calendar.DAY_OF_WEEK);
                if(day_of_week==Calendar.DAY_OF_WEEK.SUNDAY){
                    counter++;
                }
            }
        }
        System.out.println(counter);
      }   
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142