Write a method named enough time for lunch that accepts four integers hour 1, mi
ID: 3934042 • Letter: W
Question
Write a method named enough time for lunch that accepts four integers hour 1, minute 2, hour2, and minute2 as parameters. Each pair of parameters represents a time on the 24-hour clock (for example, 1:3 6 PM would be represented as 13 and 36). The method should return true if the gap between the two times is long enough to eat lunch: that is, if the second time is at least 45 minutes after the first time. Otherwise the method should return false. You may assume that all parameter values are valid: the hours are both between 0 and 23, and the minute parameters are between 0 and 59. You may also assume that both times represent times in the same day, e.g. the first time won't represent a time today while the second time represents a time tomorrow. Note that the second time might be earlier than the first time; in such a case, your method should return false. Here are some example calls to your method and their expected return results:Explanation / Answer
bool enoughTimeForLunch(int h1,int m1,int h2,int m2)
{
int t1 = h1*60 + m1; // calculating the first time in terms of minutes
int t2 = h2 * 60 + m2; // calculating the second time in terms of minutes
int time = t2 - t1; // Calculating the difference between the 2 time periods in minutes
if ( time >= 45 ) // Checking whether the minimum time difference is 45 minutes or more
return(true); // Return true as required by question
else // If difference between is less than 45 minutes
return(false); // Return false as required by question
}
For Example :
bool enoughTimeForLunch(11,00,11,59)
{
int t1 = h1*60 + m1; // t1 = 660
int t2 = h2 * 60 + m2; // t2 = 660 + 59
int time = t2 - t1; // time = 660 - (660 + 59) => time = 59
if ( time >= 45 ) // if (59 > 45)
return(true); // Return true
else // Skipped by defaut
return(false); // Skipped by defaut
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.