Java class failing a lot of JUnit tests, not sure why
I have a class that I need to modify so that it passes a bunch of JUnit
test cases given to us by the professor. I am not sure why they are
failing. I have moved everything I can into the constructor and I have
switched around the order of the functions to no luck.
Here is my class that I must modify
public class DayOfWeek1 {
int myMonth, myDayOfMonth, myYear, myAdjustment, numericDayOfWeek,
remainderSeven;
/*
* @param what the date was
*/
public DayOfWeek1(int month, int dayOfMonth, int year){
myMonth = month;
myDayOfMonth = dayOfMonth;
myYear = year;
remainderSeven = 0;
if(myMonth==1){
myAdjustment = 1;
if(myYear%4==0){
myAdjustment=0;
}
}
if(myMonth==2){
myAdjustment = 4;
if(myYear%4==0){
myAdjustment=3;
}
}
if(myMonth==3){
myAdjustment = 4;
}
if(myMonth==4){
myAdjustment = 0;
}
if(myMonth==5){
myAdjustment = 2;
}
if(myMonth==6){
myAdjustment = 5;
}
if(myMonth==7){
myAdjustment = 0;
}
if(myMonth==8){
myAdjustment = 3;
}
if(myMonth==9){
myAdjustment = 6;
}
if(myMonth==10){
myAdjustment = 1;
}
if(myMonth==11){
myAdjustment = 4;
}
if(myMonth==12){
myAdjustment = 6;
}
int fourDivides = myYear / 4;
numericDayOfWeek = myAdjustment + myDayOfMonth + (myYear-1900) +
fourDivides;
remainderSeven = numericDayOfWeek % 7;
}
/*
* @return the month in a string
*/
public String getMonthString(){
if(myMonth==1){
return "January";
}
if(myMonth==2){
return "February";
}
if(myMonth==3){
return "March";
}
if(myMonth==4){
return "April";
}
if(myMonth==5){
return "May";
}
if(myMonth==6){
return "June";
}
if(myMonth==7){
return "July";
}
if(myMonth==8){
return "August";
}
if(myMonth==9){
return "September";
}
if(myMonth==10){
return "October";
}
if(myMonth==11){
return "November";
}
if(myMonth==12){
return "December";
}
else{
return "NO_VALUE";
}
}
public int getDayOfMonth(){
return myDayOfMonth;
}
/*
* @return what year it was
*/
public int getYear(){
return myYear;
}
public int getNumericDayOfWeek(){
return remainderSeven;
}
public String getDayOfWeek(){
//numericDayOfWeek = this.getNumericDayOfWeek();
if(numericDayOfWeek==0){
return "Saturday";
}
if(numericDayOfWeek==1){
return "Sunday";
}
if(numericDayOfWeek==2){
return "Monday";
}
if(numericDayOfWeek==3){
return "Tuesday";
}
if(numericDayOfWeek==4){
return "Wednesday";
}
if(numericDayOfWeek==5){
return "Thursday";
}
if(numericDayOfWeek==6){
return "Friday";
}
else{
return "NO_VALUE";
}
}
/*
* @return the numeric day of the week
*/
/*
* returns what day of the week it was
*/
/*
* @return the month in an int
*/
public int getMonth(){
return myMonth;
}
/*
* @return what day of the month it was
*/
}
Here is one of the classes given to us by the professor, I think it acts
as a passer between my class and the test cases
package dayofweek;
/**
* Require API to conform to is:
*
* public DayOfWeek(int month, int dayOfMonth, int year) constructor
* public int getNumericDayOfWeek() (where Saturday = 0, É, Friday = 6,
NO_VALUE = -1)
* public String getDayOfWeek() (where "Saturday", "Sunday", É, "Friday",
null)
* public int getMonth() (where January=1, December=12)
* public String getMonthString()
* public int getDayOfMonth()
* public int getYear()
*
*/
public class DayOfWeek
{
public final static int NO_VALUE = -1;
private int myDOWnum;
private String myDOWstring;
public DayOfWeek(int month, int dayOfMonth, int year)
{
if (month == 1 && dayOfMonth == 1 && year == 1900)
{
myDOWnum = 2;
myDOWstring = "Monday";
}
else
{
myDOWnum = NO_VALUE;
myDOWstring = null;
}
}
public int getNumericDayOfWeek()
{
return myDOWnum;
}
public String getDayOfWeek()
{
return myDOWstring;
}
public int getMonth()
{
return NO_VALUE;
}
public String getMonthString()
{
return null;
}
public int getDayOfMonth()
{
return NO_VALUE;
}
public int getYear()
{
return NO_VALUE;
}
}
and here is a JUnit class that fails every test case
package dayofweektesting;
import dayofweek.DayOfWeek;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Test cases for "Day of week" lab assuming requirements set
* at top of DayOfWeek class
*
* Test valid leap years
*
*
*/
public class TestValidLeapYears {
/**
* Test valid 2/29/XXXX dates
*/
@Test
public void firstValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1904);
assertTrue(dow.getDayOfWeek().compareTo("Monday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 2);
}
@Test
public void secondValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1908);
assertTrue(dow.getDayOfWeek().compareTo("Saturday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 0);
}
@Test
public void thirdValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1912);
assertTrue(dow.getDayOfWeek().compareTo("Thursday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 5);
}
@Test
public void fourthValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1916);
assertTrue(dow.getDayOfWeek().compareTo("Tuesday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 3);
}
/**
* Test boundary dates of2/28/XXXX and 3/1/XXXX for valid leap years
*/
@Test
public void firstValidLeapYearBoundaries() {
DayOfWeek dow = new DayOfWeek(2, 28, 1904);
assertTrue(dow.getDayOfWeek().compareTo("Sunday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 1);
dow = new DayOfWeek(3, 1, 1904);
assertTrue(dow.getDayOfWeek().compareTo("Tuesday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 3);
}
@Test
public void secondValidLeapYearBoundaries() {
DayOfWeek dow = new DayOfWeek(2, 28, 1908);
assertTrue(dow.getDayOfWeek().compareTo("Friday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 6);
dow = new DayOfWeek(3, 1, 1908);
assertTrue(dow.getDayOfWeek().compareTo("Sunday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 1);
}
}
No comments:
Post a Comment