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

• 11-06-2012, 02:10 AM
Michael305rodri
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?
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
• 11-06-2012, 02:15 AM
Junky
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
• 11-06-2012, 02:19 AM
Michael305rodri
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?
• 11-06-2012, 02:24 AM
Junky
Re: Fixing the loop that calculates how many days are between two dates
Why not try both and see.
• 11-06-2012, 02:30 AM
Michael305rodri
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?

Code:

```  {           do         {             start.next();         }         while(start.getMonth()>end.getMonth() && start.getDay() < end.getDay() &&               start.getYear()<end.getYear() );                     return end.getDay() - start.getDay() + 1  ;       }```
• 11-06-2012, 02:42 AM
Michael305rodri
Re: Fixing the loop that calculates how many days are between two dates
Okay this is the closest I've gotten I think
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?
• 11-06-2012, 02:46 AM
Junky
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.
• 11-06-2012, 02:46 AM
Michael305rodri
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?
• 11-06-2012, 02:50 AM
Michael305rodri
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....
• 11-06-2012, 02:53 AM
Junky
Re: Fixing the loop that calculates how many days are between two dates

The code you write does the counting sheesh!
• 11-06-2012, 02:58 AM
Michael305rodri
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?
• 11-06-2012, 03:14 AM
Junky
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.
• 11-06-2012, 03:46 AM
Michael305rodri
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...

Code:

```int count = 0 ;         do         {             start.next();         }                         while(start.getMonth()<end.getMonth() && start.getDay() < end.getDay() &&               start.getYear()<end.getYear() );         count ++ ;                 return  count  ;```
:'-(
• 11-06-2012, 04:46 AM
Michael305rodri
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?
• 11-06-2012, 10:56 AM
Tolls
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.