Results 1 to 15 of 15
  1. #1
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default [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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

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

  3. #3
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    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. #4
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I think I just found the error. I was calculating the sundays starting in the year 1900, rather than 1901

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

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

  6. #6
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I print what the different variables are so that i can find where the error occurs. How do you debug?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

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

  8. #8
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    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. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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. #10
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I don't actually use eclipse, but I think I might start using it, because I've heard good things about it.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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. #12
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I use JCreator right now.

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

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

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

    Default

    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. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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

  1. Project Help
    By XxHEXSORxX in forum AWT / Swing
    Replies: 4
    Last Post: 01-28-2009, 10:01 AM
  2. jsp project help
    By rajibmp in forum New To Java
    Replies: 4
    Last Post: 10-10-2008, 12:50 AM
  3. Could anyone help with project?
    By billdara in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 05:05 PM
  4. First Project Need Big Help
    By earl in forum New To Java
    Replies: 1
    Last Post: 01-18-2008, 06:12 PM

Posting Permissions

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