Results 1 to 15 of 15
Thread: [SOLVED] project euler #19
- 12-12-2008, 11:23 PM #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)
My code gives the answer 173, but that is not correct. Here's my code: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)?
And: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"); } }
Do you see any error in the code? I've tried debugging it, but i can't find the error.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; } }
- 12-13-2008, 05:36 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 03:38 PM #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.
- 12-13-2008, 03:44 PM #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
- 12-14-2008, 06:24 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-14-2008, 06:29 AM #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?
- 12-14-2008, 06:31 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 06:33 AM #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
- 12-14-2008, 06:35 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 06:36 AM #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.
- 12-14-2008, 06:41 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 06:42 AM #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"?
- 12-14-2008, 06:45 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 06:51 AM #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.
- 12-14-2008, 06:53 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
Similar Threads
-
Project Help
By XxHEXSORxX in forum AWT / SwingReplies: 4Last Post: 01-28-2009, 10:01 AM -
jsp project help
By rajibmp in forum New To JavaReplies: 4Last Post: 10-10-2008, 12:50 AM -
Could anyone help with project?
By billdara in forum New To JavaReplies: 1Last Post: 03-12-2008, 05:05 PM -
First Project Need Big Help
By earl in forum New To JavaReplies: 1Last Post: 01-18-2008, 06:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks