# [SOLVED] project euler #19

• 12-13-2008, 12:23 AM
matzahboy
[SOLVED] project euler #19
Here is the problem (found at Problem 19 - Project Euler)
Quote:

You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
My code gives the answer 173, but that is not correct. Here's my code:

Code:

```public class Problem_19 {         public static void main(String[] args) {                         Date d = new Date();             int sundays=0; //the total sundays that fall on the first of the month             while(d.stillRoom()) {                     d.incrementWeek();                     if(d.getDay()==1) {                             sundays++;                             //System.out.println(d.getMonth() + "-" + d.getDay() + "," + d.getYear());                     }             }             System.out.println("There are " + sundays + " sundays");     } }```
And:
Code:

```public class Date {                 private int day;         private int month;         private int year;                 public Date() {                 day=7;                 month=1;                 year=1900;         }                 public int getYear() {                 return year;         }                 public int getMonth() {                 return month;         }                 public int getDay() {                 return day;         }                 //If the there is still another sunday left in the year 2000         public boolean stillRoom() {                 if((year<2000)||(month<12)||(day+7<=daysInMonth()))                         return true;                 else                         return false;         }                 public void incrementWeek() {                 day+=7;                 if(day>daysInMonth()) {                         day = day-daysInMonth();                         incrementMonth();                 }         }                 private void incrementMonth() {                 month++;                 if(month>12) {                         month=month-12;                         year++;                 }         }                 private int daysInMonth() {                 if(month==4|month==6|month==9|month==11)                         return 30;                 if(month==2) {                         if(isLeapYear())                                 return 29;                         else                                 return 28;                 }                 return 31;         }                 private boolean isLeapYear() {                 if(year%4==0) {                         if((year%100==0)&&(year%400!=0))                                 return false;                         return true;                 } else                         return false;         } }```
Do you see any error in the code? I've tried debugging it, but i can't find the error.
• 12-13-2008, 06:36 AM
Eranga
If you debug your code, you must see how the values are change. So why are you cannot find where the error is?
• 12-13-2008, 04:38 PM
matzahboy
By putting in a lot of "system.out.println" statements?

Anyway, I even plugged in some of the sundays into a date to day calculator, and they were all sundays. I just can't figure out which part of my program contains the error.
• 12-13-2008, 04:44 PM
matzahboy
I think I just found the error. I was calculating the sundays starting in the year 1900, rather than 1901
• 12-14-2008, 07:24 AM
Eranga
Quote:

Originally Posted by matzahboy
By putting in a lot of "system.out.println" statements?

You say that for debugging?
• 12-14-2008, 07:29 AM
matzahboy
I print what the different variables are so that i can find where the error occurs. How do you debug?
• 12-14-2008, 07:31 AM
Eranga
Using a debugger. I'm working on NetBeans and I can put breakpoints anywhere in the code and hold the execution process.
• 12-14-2008, 07:33 AM
matzahboy
Does eclipse have that as well? My program (JCreator LITE) does not. I remember that feature when I used to program in VB.net... very useful
• 12-14-2008, 07:35 AM
Eranga
Check on tools something like Debud, Breakpoint and so on. I'm not Eclipse user, so I don't know the exact way to start on. But that facility must be there.

Did you workout on any Eclipse tutorial? Check on the Eclipse help page as well.
• 12-14-2008, 07:36 AM
matzahboy
I don't actually use eclipse, but I think I might start using it, because I've heard good things about it.
• 12-14-2008, 07:41 AM
Eranga
If you are still work on with Notepad + Command prompt, my advice is don't move to any IDE until you get much knowledge on Java basis. That combination helps a lot to learn Java quickly, in my experience.
• 12-14-2008, 07:42 AM
matzahboy
I use JCreator right now.

Either way, what do you mean by "java basis"?
• 12-14-2008, 07:45 AM
Eranga
It's all about Java. I think you are newbie to Java, is it? If so you may have to learn more fundamental on Java.
• 12-14-2008, 07:51 AM
matzahboy
I took a class on java last year in my school, but my teacher wasn't any good. I've been brushing up on my skills with project euler since then.
• 12-14-2008, 07:53 AM
Eranga
Fine, if you can workout the complete tutorial on Suns' web site you can learn a lot yourself in quick period of time. Get much clear idea about Java fundamentals first and later think about IDEs. At the time use the way you have practiced upto now. :)