-2

so I am trying to do a test class, the instruction is

Create a class called TestDate whose constructor creates fifteen Date objects and displays the day of the week for the dates by calling each Date’s getDayOfTheWeek() method. The dates are specifically these ones, in this precise order:

but when I run it, I keep getting errors! I think it's my constructor crashing or somewhere along that line.

Below is my TestDate class code

public class TestDate
{
private Date date;
/*
 * Main Constructor
 * @param fifteen specific date, test to your heart's content
 */
public TestDate()
{
    Date d1 = new Date(1970, 11, 15); 
    System.out.println(date.getDayOfTheWeek());
    Date d2 = new Date(1887, 7, 31);         
    Date d3 = new Date(1966, 5, 2);          
    Date d4 = new Date(1980, 8, 19);         
    Date d5 = new Date(2001, 9, 11);         
    Date d6 = new Date(1900, 6, 26);         
    Date d7 = new Date(1940, 2, 28);        
    Date d8 = new Date(1974, 10, 30);       
    Date d9 = new Date(1914, 1, 15);        
    Date d10 = new Date(1840, 10, 1);        
    Date d11 = new Date(1999, 12, 31);       
    Date d12 = new Date(1988, 5, 20);       
    Date d13= new Date(2012, 3, 10);         
    Date d14 = new Date(2006, 4, 1);        
    Date d15 = new Date(1992, 2, 29);       


}
}

And here is my Date Class constructor and method getDayofTheWeek, I think that's all the information related to my problem.

public class Date
{
public Date(int year, int month, int day)
{
    setYear(year);
    setMonth(month);
    setDay(day);
}   

public String getDayOfTheWeek()   
{
    int step0 = year%100;
    int step1 = step0 / 12;
    int step2 = step0 % 12;
    int step3 = step2 / 4;
    int step4 = day;    
    int step5 = 0;            
    if(month == JANUARY || month == OCTOBER)
    {
        step5 = JAN_CODE;
    }
    else if(month == FEBURARY || month == MARCH || month == NOVEMBER)
    {
        step5 = FEB_CODE;
    }
    else if(month == APRIL || month == JULY)
    {
        step5 = APR_CODE;
    }
    else if(month == MAY)
    {
        step5 = MAY_CODE;
    }
    else if(month == JUNE)
    {
        step5 = JUN_CODE;
    }
    else if(month == AUGUST)
    {
        step5 = AUG_CODE;
    }
    else if(month == SEPTEMBER || month == DECEMBER)
    {
        step5 = SEP_CODE;
    }
    else
    {
        step5 = 0;
    }  
    if(year/100 ==16 || year/100 == 20)
    {
        step5 = step5 + SPECIAL_CODE_SIX;
    }
    else if(year/100 == 17 || year/100 == 21)
    {
        step5 = step5 + SPECIAL_CODE_FOUR;
    }
    else if(year/100 == 18)
    {
        step5 = step5 + SPECIAL_CODE_TWO;
    }
    int step6 = (step1 + step2 + step3 +step4 + step5)%MOD_CODE;     
    if(step6 == SAT_CODE)
    {
        return "Saturday";
    }
    else if(step6 == SUN_CODE)
    {
        return "Sunday";
    }
    else if(step6 == MON_CODE)
    {
        return "Monday";
    }
    else if(step6 == TUE_CODE)
    {
        return "Tuesday";
    }
    else if(step6 == WED_CODE)
    {
        return "Wednesday";
    }
    else if(step6 == THU_CODE)
    {
        return "Thursday";
    }
    else if(step6 == FRI_CODE)
    {
        return "Friday";
    }
    else
    {
        return null;
    }
} 
}  

and I passed the parameter in the mutator method

public void setYear(int year)
    {
    if(year <= CURRENT_YEAR && year >= YEAR_ZERO)
    {
        this.year = year;
    }        
}

    public void setMonth(int month)
{
    if(month <= DECEMBER && month >= JANUARY)
    {
        this.month = month;
    }
}

    public void setDay(int day)
{
    if(month == JANUARY || month == MARCH || month == MAY || month == JULY || month == AUGUST
    || month == OCTOBER || month == DECEMBER)
    {
        if(day <= DAY_THIRTY_ONE && day >= DAY_ONE)
        {
            this.day = day;
        }
        else 
        {
            year = CURRENT_YEAR;
            month = JANUARY;
            day = DAY_ONE;
        }
    }    
    else if(month == APRIL || month == JUNE || month == SEPTEMBER || month == NOVEMBER)
    {
        if(day <= DAY_THIRTY && day >= DAY_ONE)
        {
            this.day = day;
        }
        else
        {
            year = CURRENT_YEAR;
            month = JANUARY;
            day = DAY_ONE;
        }
    }
    else if(month == FEBURARY)
    {
        if((isLeapYear() == true) && day <= DAY_FEB_LEAP && day >= DAY_ONE)
        {
            this.day = day;
        }      
        else if((isLeapYear() == false) && day <= DAY_FEB_NORMAL && day >= DAY_ONE)
        {
            this.day = day;
        }  
        else 
        {
            year = CURRENT_YEAR;
            month = JANUARY;
            day = DAY_ONE;
        }
    }        
}
Sorry for the long list of code, I don't really know which ones are useless.
Puneet
  • 615
  • 2
  • 7
  • 17

1 Answers1

1

In the second line of your constructor you are calling

System.out.println(date.getDayOfTheWeek());

on the variable date which is null at this point as it has not been initialized yet. This leads to a NullPointerException. Read here for more information: What is a NullPointerException, and how do I fix it?

What I am (assuming) you wanted to do there is:

System.out.println(d1.getDayOfTheWeek());


Things besides that that I noticed:

  1. You use the @param field in a javadoc comment without there being a parameter. Do not do this. If your constructor does not take arguments then there should not be a @param.

  2. You are creating 15 instances of Date in your constructor whose scope end as soon as the constructor is finished. You could just delete them and nothing would change. I am assuming you want this class to be a "test" whether your dates are initialized correctly. I would recommend doing this in a main method (for a quick test while coding) and with an actual test framework like JUnit for 'real' testing. You can read this tutorial if you are interested. It also goes into detail why you actually want to test.

Ben
  • 1,665
  • 1
  • 11
  • 22
  • Thank you so much! That was exactly what I was looking for, I am going to read the link to learn more. – James Smith Mar 08 '18 at 07:37
  • @JamesSmith no problem. I updated my answer a bit with some general 'best practices' you are not using currently. Maybe have a look at those, too. – Ben Mar 08 '18 at 07:42