Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Starting code is given for a class whose objects represent the current date as i

ID: 3604518 • Letter: S

Question

Starting code is given for a class whose objects represent the current date as int month, int day, int year. Complete the instance method public boolean isValid() which should return true if the current fields represent a valid date. This method is used in the GPSDate constructor to check that the initializing date is valid.

Work on completing this by doing test-driven development, creating JUnit test methods within the JUnit test case GPSDateTest. First write a test method (preface header with @Test) that fails (try writing one that expects the date 0/1/2017 to be valid), then complete just enough code so the test succeeds (i.e. add a check for month <= 0). Then write another test that fails for the date 13/1/2017, and so forth.

Also continue to add to your JUnit test case GPSDateTest, striving to test all possible code paths in your method. As above, put all test code within a test source folder in Eclipse. Be sure you handle (and test for the correct handling of): invalid months, invalid days for the given month, including both leap and non-leap years, and invalid years (those part of dates before Oct 15, 1582).

Explanation / Answer

----------------------------Date.java----------------------------

public class Date{

int month;

int day;

int year;

public boolean isLeapYear(int year){

if(year>0){

if(year%400==0 || (year%4==0 && year%100!=0)){return true;}

}

return false;

}

public boolean isValid(){

if(month<=0 || month>12){

return false;

}

if(day<=0){

return false;

}

if(year<=0){

return false;

}

switch(month){

//For months with 31 Days

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

if(day>31){

return false;

}

break;

//For months with 30 Days

case 4:

case 6:

case 9:

case 11:

if(day>30){

return false;

}

break;

//Special check for february

case 2:

if(isLeapYear(year)){

if(day>29){

return false;

}

}else{

if(day>28){

return false;

}

}

break;

}

//Invalidating dates before 15 Oct, 1582

if(year<1582 || (year==1582 && month<11) || (year==1582 && month==11 && day<15)){

return false;

}

return true;

}

}

----------------------------GPSDateTest.java----------------------------

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class GPSDateTest{

public GPSDateTest(){
}

@Before
public void setUp(){
}

@After
public void tearDown()
{
}

@Test(timeout=1000)
public void testDays() {
Date d = new Date();
d.day = 0;
d.month = 12;
d.year = 2000;
assertFalse(d.isValid());
d.day = 12;
assertTrue(d.isValid());
d.day = 31;
assertTrue(d.isValid());
d.day = 32;
assertFalse(d.isValid());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote