# Thread: [SOLVED] project euler #19

1. Member
Join Date
Nov 2008
Posts
67
Rep Power
0

## [SOLVED] project euler #19

Here is the problem (found at Problem 19 - Project Euler)
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:

Java 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:
Java 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.

2. If you debug your code, you must see how the values are change. So why are you cannot find where the error is?

3. Member
Join Date
Nov 2008
Posts
67
Rep Power
0
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.

4. Member
Join Date
Nov 2008
Posts
67
Rep Power
0
I think I just found the error. I was calculating the sundays starting in the year 1900, rather than 1901

5. Originally Posted by matzahboy
By putting in a lot of "system.out.println" statements?
You say that for debugging?

6. Member
Join Date
Nov 2008
Posts
67
Rep Power
0
I print what the different variables are so that i can find where the error occurs. How do you debug?

7. Using a debugger. I'm working on NetBeans and I can put breakpoints anywhere in the code and hold the execution process.

8. Member
Join Date
Nov 2008
Posts
67
Rep Power
0
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

9. 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.

10. Member
Join Date
Nov 2008
Posts
67
Rep Power
0
I don't actually use eclipse, but I think I might start using it, because I've heard good things about it.

11. 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. Member
Join Date
Nov 2008
Posts
67
Rep Power
0
I use JCreator right now.

Either way, what do you mean by "java basis"?

13. 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.

14. Member
Join Date
Nov 2008
Posts
67
Rep Power
0
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.

15. 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. :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•