Write a method before that takes as parameters two month/day combinations and th
ID: 3689245 • Letter: W
Question
Write a method before that takes as parameters two month/day combinations and that returns whether or not the first date comes before the second date (true if the first month/day comes before the second month/day, false if it does not). The method will take four integers as parameters that represent the two month/day combinations.
The first integer in each pair represents the month and will be a value between 1 and 12 (1 for January, 2 for February, etc, up to 12 for December). The second integer in each pair represents the day of the month (a value between 1 and 31). One date is considered to come before another if it comes earlier in the year.
Below are sample calls on the method.
Method Call
Return
Explanation
before(6, 3, 9, 20)
true
June 3 comes before Sep 20
before(10, 1, 2, 25)
false
Oct 1 does not come before Feb 25
before(8, 15, 8, 15)
false
Aug 15 does not come before Aug 15
before(8, 15, 8, 16)
true
Aug 15 comes before Aug 16
Method problem: For this problem, you are supposed to write a Java method as described. You should notwrite a complete Java class; just write the method(s) described in the problem statement.
Method Call
Return
Explanation
before(6, 3, 9, 20)
true
June 3 comes before Sep 20
before(10, 1, 2, 25)
false
Oct 1 does not come before Feb 25
before(8, 15, 8, 15)
false
Aug 15 does not come before Aug 15
before(8, 15, 8, 16)
true
Aug 15 comes before Aug 16
Explanation / Answer
//Java method before(int m1,int d1,int m2,d2)
/**
* The method before that takes four integer values month1, day1,
* month2 and day2 and returns true if the month1/day1 is comes before/earlier
* month2/day2 otherwise returns false.
* */
private static boolean before(int month1, int day1, int month2, int day2) {
boolean valid=false;
//Check whether month1 is less than month2
if(month1<month2)
//Set valid=true
valid=true;
//otherwise check if months months are equal
//Then check if day1 is less than day2
else if(month1==month2)
{
//Check if day1 is less than day2
if(day1<day2)
//Set valid=true
valid=true;
else
//otherwise set valid=false
valid=false;
}
//return valid boolean value
return valid;
}//end of the method before
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.