Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Fixing the loop that calculates how many days are between two dates

    I already got the tester class but it keeps printing the wrong value. This is an infinite loop right?
    Java Code:
    // TO DO: write body of this method here
    
    /**
    * Computes and returns the number of days between two dates, counting the
    * end date but not the start date. E.g., the number of days between
    * 11/1/2012 and 11/5/2012 is 4, not 5.
    *
    * Precondition: The start date must occur on or before the end date.
    *
    * @param start the earlier of the two dates
    * @param end the later of the two dates
    *
    * @return the number of days elapsed between the start date and the end
    * date
    */
    
     public int countingTheDays(Date start, Date end)
        {
            
     int count = start.getDay();
      
            
    
            while(start.getMonth()<=end.getMonth() && start.getDay() < end.getDay());
            
           count = count++ ; 
     
     
            return count ; 
    
        }
    }

    Methods:
    d1.getMonth() // returns the month as int 1..12
    d1.getDay() // returns the day of the month, as an int
    d1.getYear() // returns the year

    d1.getMonthName() // returns the month as a String (e.g. “March") d1.getDayOfWeek() // returns the day of the week as a string (e.g.,
    // “Friday”)

    d1.equals(d2) // returns true if d1 is the same date as d2
    // otherwise, returns false

    d1.getShortDate() // returns the date as mm/dd/yyyy
    d1.getMediumDate() // returns the date in the form of
    //“November 2, 2010”
    d1.getLongDate() // returns the date in the form of
    // “Monday, November 2nd, 2010”

    Date d = new Date(1,1,2012) ; // d is January 1, 2012 (a leap year)

    d.next() ; // d is January 2, 2012
    d.previous() ; // d is January 1, 2012
    d.add(31) ; // d is February 1, 2012
    d.subtract(32) ; // d is December 31, 2011
    d.next() ; // d is January 1, 2012

    d = new Date(3,1,2012) ; // d is March 1, 2012
    d.previous() ; // d is now February 29, 2012

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Fixing the loop that calculates how many days are between two dates

    Yes it will be an infinite loop since start day and start month never change. Therefore they will either never be less or they will always be less than end day and month

  3. #3
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    Thanks. Can you help me? I have to print out how many days are between two dates. Should I use the do while or while?
    Last edited by Michael305rodri; 11-06-2012 at 01:21 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Fixing the loop that calculates how many days are between two dates

    Why not try both and see.

  5. #5
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    Woohoo I think I got it!!! Does this make sense to you?

    Java Code:
     {
     
            do
            {
                start.next(); 
            }
            while(start.getMonth()>end.getMonth() && start.getDay() < end.getDay() && 
                   start.getYear()<end.getYear() );
               
     
            return end.getDay() - start.getDay() + 1  ; 
     
        }

  6. #6
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    Okay this is the closest I've gotten I think
    Java Code:
      public int countingTheDays(Date start, Date end)
        {
     
            do
            {
                start.next(); 
            }
            while(start.getMonth()<end.getMonth() && start.getDay() < end.getDay() && 
                   start.getYear()<end.getYear() );
               
     
            return (I don't know what to return here)
     
          
        }
    Can you at least tell me if I'm doing this completely wrong or if I'm almost there?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Fixing the loop that calculates how many days are between two dates

    Based upon the name of the method it would seem that you need to count the number of days between two dates. So you need to actually do some counting and then return the result.

  8. #8
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    I guess I want to return how many start.next() happened. Can you even do this?

  9. #9
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    I need to do the counting?? I don't understand. Isn't that what this program is for? So you wont have to do any counting and the computer can do it for you....

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Fixing the loop that calculates how many days are between two dates

    :headdesk:

    The code you write does the counting sheesh!

  11. #11
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    lol.. I thought you meant me since you said " that YOU need to count the number ". I'm sorry this is my first semester in programming. Like I said I realized the the amount of days between the two dates are equal to the amount of start.next() that happened. So I was thinking about return the amount of start.next()s but can you do that?

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Fixing the loop that calculates how many days are between two dates

    WHAT?

    You need a variable that is zero.
    Inside a loop you increment it.
    You also increment start date.
    And the loop keeps going until start date reaches end date.
    The variable now holds how many days between the two dates.

    This is the algorithm that needs to be used even if you were not writing a computer program. 90% of the exercise in writing a program is working out how to do it. That is why many people suggest working it out on paper first. Then you can convert it to code.

  13. #13
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    I don't know how to increment the start date. My professor never really went over while and much less the do while. The only one he explained thoroughly was the for statement. I thought this would work but it gave me a value of 1...

    Java Code:
    int count = 0 ;
    
            do
            {
                start.next(); 
            }
            
            
            while(start.getMonth()<end.getMonth() && start.getDay() < end.getDay() && 
                   start.getYear()<end.getYear() );
            count ++ ; 
           
     
            return  count  ;
    :'-(

  14. #14
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Fixing the loop that calculates how many days are between two dates

    Never mind I got it with the help of my friend :) Thank you though. Oh, and what's better C++ or java? Why do you think so?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Fixing the loop that calculates how many days are between two dates

    They are different beats with different roles to play.
    C++ (as with C) can code closer to the metal than Java, but I really wouldn't want to write a webapp in it.

    There is a big middle ground.
    I will say I prefer Java for the big apps as I find it easier to maintain, and that's 90% of the work done on an application that has any life to it. C++'s flexibility can be a nightmare on that front if you have some clever arses working on the project who feel pointer manipulation is something that has to be done at every opportunity whether it obfuscates the code or not.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Help with fixing a error
    By LukasHopkins in forum New To Java
    Replies: 1
    Last Post: 09-06-2012, 02:34 AM
  2. Replies: 1
    Last Post: 06-02-2012, 04:57 AM
  3. number of the days (-) days gone past
    By sh4rif in forum New To Java
    Replies: 4
    Last Post: 05-22-2012, 01:17 PM
  4. Best way to find number of days between 2 dates??
    By alinaqvi90 in forum Advanced Java
    Replies: 18
    Last Post: 11-22-2010, 08:49 AM
  5. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 09:06 AM

Posting Permissions

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