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

1. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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  Reply With Quote

2. 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  Reply With Quote

3. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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.  Reply With Quote

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

Why not try both and see.  Reply With Quote

5. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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  ;

}  Reply With Quote

6. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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?  Reply With Quote

7. 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.  Reply With Quote

8. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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?  Reply With Quote

9. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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....  Reply With Quote

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

The code you write does the counting sheesh!  Reply With Quote

11. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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?  Reply With Quote

12. 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.  Reply With Quote

13. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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  ;
:'-(  Reply With Quote

14. Member Join Date
Oct 2012
Posts
30
Rep Power
0 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?  Reply With Quote

15. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26 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.  Reply With Quote Posting Permissions

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