Question
Provide a JUnit class with test cases for the attached Date class. You should have at least 12 test cases. Remember that a test should
Provide a JUnit class with test cases for the attached Date class. You should have at least 12 test cases. Remember that a test should be small, and cover a single scenario. Your tests should test both "happy" dates to ensure that they are correctly identified as "good," and "bad" dates (like February 30) to ensure that they are correctly identified as "invalid." The Date class specification stated that Spring started on March 21, Summer on June 21, Fall on September 23 and Winter on December 21
public class Month {
private int monthNumber;
private String monthName;
private int monthDays;
public Month(String name, int number, int days) {
setMonthName(name);
setMonthDays(days);
setMonthNumber(number);
}
public boolean isDayValid(int d) {
return d <= monthDays;
}
public String getMonthName() {
return monthName;
}
public void setMonthName(String monthName) {
this.monthName = monthName;
}
public int getMonthDays() {
return monthDays;
}
public void setMonthDays(int monthDays) {
this.monthDays = monthDays;
}
public int getMonthNumber() {
return monthNumber;
}
public void setMonthNumber(int monthNumber) {
this.monthNumber = monthNumber;
}
}
public class Date
{
private static final Month year[] = {
new Month("January", 1, 31),
new Month("February", 2, 29),
new Month("March", 3, 31),
new Month("April", 4, 30),
new Month("May", 5, 31),
new Month("June", 6, 30),
new Month("July", 7, 31),
new Month("August", 8, 31),
new Month("September", 9, 30),
new Month("October", 10, 31),
new Month("November", 11, 30),
new Month("December", 12, 31),
};
private int month;
private int day;
public Date(int theMonth, int theDay)
{
month = theMonth;
day = theDay;
}
public String getMonth()
{
String result = "UNKNOWN";
if (isValidDay()) {
result = year[month - 1].getMonthName();
}
return result;
}
public String getSeason()
{
String result = "UNKNOWN";
if (!isValidDay()) {
return result;
}
if (((month == 12) && day >= 21) || (month == 1 ) || (month == 2) || (month == 3 && day <= 20)) {
result = "Winter";
} else if (((month == 3) && day >= 21) || (month == 4 ) || (month == 5) || (month == 6 && day <= 20)) {
result = "Spring";
} else if (((month == 6) && day >= 21) || (month == 7 ) || (month == 8) || (month == 9 && day <= 22)) {
result = "Summer";
} else {
result = "Fall";
}
return result;
}
public boolean isValidDay()
{
boolean rc = false;
if (isValidMonth() && day > 0 && day <= year[month - 1].getMonthDays()) {
rc = true;
}
return rc;
}
public boolean isValidMonth()
{
return month > 0 && month <= year.length;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started