The document contains code for testing a Counter class with various methods like increase(), decrease(), etc. It also contains code for a Date class that can return the season for a given month and day. The Date class uses a Month class to store details of each month like name, number of days, etc. The tests provided need the Counter and Date classes to be implemented to pass the tests.
1. CounterTest.java:
import static org.junit.Assert.*;
import org.junit.Test;
public class CounterTest {
// FIRST
// Write a class Counter such that the following test works
@Test
public void testZero() {
Counter cnt = new Counter(0);
assertEquals("initial value of (0) failed", 0, cnt.getCount());
cnt.increase();
assertEquals("increased value of (0) failed", 0, cnt.getCount());
cnt.decrease();
assertEquals("decreased value of (0) failed", 0, cnt.getCount());
}
// SECOND
// Uncomment the following method,
// modify your Counter class so that this test works.
/*
@Test
public void testIncrease() {
Counter cnt = new Counter(7);
assertEquals("initial value of (7) failed", 0, cnt.getCount());
cnt.increase();
assertEquals("increased once value of (7) failed", 7, cnt.getCount());
cnt.increase();
assertEquals("twice increased once value of (7) failed", 14,
cnt.getCount());
}
*/
// THIRD
// Uncomment the following method,
// modify your Counter class so that this test works.
/*
2. @Test
public void testDecrease() {
Counter cnt = new Counter(11);
cnt.decrease();
assertEquals("decreased value of (11) failed", -11, cnt.getCount());
cnt.decrease();
assertEquals("twice decreased value of (11) failed", -22,
cnt.getCount());
}
*/
// FOURTH
// Uncomment the following method,
// modify your Counter class so that this test works.
/*
@Test
public void testNegative() {
Counter cnt = new Counter(-1);
cnt.decrease();
assertEquals("decreased value of (-1) failed", 1, cnt.getCount());
cnt.increase();
assertEquals("decreased/increased value of (-1) failed", 0,
cnt.getCount());
cnt.increase();
assertEquals("decreased/increased/increased value of (-1) failed", -1,
cnt.getCount());
}
*/
}
Date.java:
/**
*
* This class represents a date given the month and the day of the month. For the
* given date, this class provides a method for determining the season in the northern hemisphere
for the date.
*
3. * For example, the given code fragment the output to the console should be WINTER.
*
* Date jan1 = new Date(1, 1);
* String season = jan1.getSeason();
* System.out.println(season);
*
* @author parks
*
*/
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;
/**
* Constructs a new Date object. The month should be a value
* from 1 -12 and the day from 1 - 31.
*
* @param theMonth the month
* @param theDay the day
*/
public Date(int theMonth, int theDay)
4. {
month = theMonth;
day = theDay;
}
/**
* This method returns the String representation for the month. For example:
* "January" for month == 1, "February" for month == 2, etc
*
* @return string representation of the month
*/
public String getMonth()
{
String result = "UNKNOWN";
if (isValidDay()) {
result = year[month - 1].getMonthName();
}
return result;
}
/**
* Returns the season that this days falls in based on the following chart:
*
* SEASON RETURNED FROM TO
* SPRING March 21 June 20
* SUMMER June 21 September 22
* FALL September 23 December 20
* WINTER December 21 March 20
*
* If the day does not represent a valid day it will return UNKNOWN
*
* @return the string representation for the season:
* WINTER | SPRING | SUMMER | FALL | UNKNOWN
*/
5. 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;
}
/**
*
* Returns true if the day is a valid day of the month. For example if the
* day were 31 and the month was 11 (November) this method would return
* false since November only has 30 days. Here is a (shortened version)
* of a Mother Goose rhyme for the number of days in each month:
*
* Thirty days hath September,
* April, June, and November.
* All the rest have thirty-one,
* Excepting February (which we will say has 29 days)
*
6. * @return true if the day is a valid day of the month otherwise false
*/
public boolean isValidDay()
{
boolean rc = false;
if (isValidMonth() && day > 0 && day <= year[month - 1].getMonthDays()) {
rc = true;
}
return rc;
}
/**
* Returns true if the day is a valid month. Valid months have values from 1 - 12 (inclusive)
*
* @return true if the day is a valid day of the month otherwise false
*/
public boolean isValidMonth()
{
return month > 0 && month <= year.length;
}
}
Month.java:
/**
*
* Simple store for a month's number (1-origin), name
* and number of days in the month.
*
* @author parks
*
*/
public class Month {
private int monthNumber;
private String monthName;
private int monthDays;
7. 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;
}
}
Solution
CounterTest.java:
import static org.junit.Assert.*;
import org.junit.Test;
public class CounterTest {
// FIRST
8. // Write a class Counter such that the following test works
@Test
public void testZero() {
Counter cnt = new Counter(0);
assertEquals("initial value of (0) failed", 0, cnt.getCount());
cnt.increase();
assertEquals("increased value of (0) failed", 0, cnt.getCount());
cnt.decrease();
assertEquals("decreased value of (0) failed", 0, cnt.getCount());
}
// SECOND
// Uncomment the following method,
// modify your Counter class so that this test works.
/*
@Test
public void testIncrease() {
Counter cnt = new Counter(7);
assertEquals("initial value of (7) failed", 0, cnt.getCount());
cnt.increase();
assertEquals("increased once value of (7) failed", 7, cnt.getCount());
cnt.increase();
assertEquals("twice increased once value of (7) failed", 14,
cnt.getCount());
}
*/
// THIRD
// Uncomment the following method,
// modify your Counter class so that this test works.
/*
@Test
public void testDecrease() {
Counter cnt = new Counter(11);
cnt.decrease();
assertEquals("decreased value of (11) failed", -11, cnt.getCount());
cnt.decrease();
9. assertEquals("twice decreased value of (11) failed", -22,
cnt.getCount());
}
*/
// FOURTH
// Uncomment the following method,
// modify your Counter class so that this test works.
/*
@Test
public void testNegative() {
Counter cnt = new Counter(-1);
cnt.decrease();
assertEquals("decreased value of (-1) failed", 1, cnt.getCount());
cnt.increase();
assertEquals("decreased/increased value of (-1) failed", 0,
cnt.getCount());
cnt.increase();
assertEquals("decreased/increased/increased value of (-1) failed", -1,
cnt.getCount());
}
*/
}
Date.java:
/**
*
* This class represents a date given the month and the day of the month. For the
* given date, this class provides a method for determining the season in the northern hemisphere
for the date.
*
* For example, the given code fragment the output to the console should be WINTER.
*
* Date jan1 = new Date(1, 1);
* String season = jan1.getSeason();
* System.out.println(season);
*
10. * @author parks
*
*/
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;
/**
* Constructs a new Date object. The month should be a value
* from 1 -12 and the day from 1 - 31.
*
* @param theMonth the month
* @param theDay the day
*/
public Date(int theMonth, int theDay)
{
month = theMonth;
day = theDay;
}
11. /**
* This method returns the String representation for the month. For example:
* "January" for month == 1, "February" for month == 2, etc
*
* @return string representation of the month
*/
public String getMonth()
{
String result = "UNKNOWN";
if (isValidDay()) {
result = year[month - 1].getMonthName();
}
return result;
}
/**
* Returns the season that this days falls in based on the following chart:
*
* SEASON RETURNED FROM TO
* SPRING March 21 June 20
* SUMMER June 21 September 22
* FALL September 23 December 20
* WINTER December 21 March 20
*
* If the day does not represent a valid day it will return UNKNOWN
*
* @return the string representation for the season:
* WINTER | SPRING | SUMMER | FALL | UNKNOWN
*/
public String getSeason()
{
String result = "UNKNOWN";
if (!isValidDay()) {
return result;
12. }
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;
}
/**
*
* Returns true if the day is a valid day of the month. For example if the
* day were 31 and the month was 11 (November) this method would return
* false since November only has 30 days. Here is a (shortened version)
* of a Mother Goose rhyme for the number of days in each month:
*
* Thirty days hath September,
* April, June, and November.
* All the rest have thirty-one,
* Excepting February (which we will say has 29 days)
*
* @return true if the day is a valid day of the month otherwise false
*/
public boolean isValidDay()
{
boolean rc = false;
if (isValidMonth() && day > 0 && day <= year[month - 1].getMonthDays()) {
13. rc = true;
}
return rc;
}
/**
* Returns true if the day is a valid month. Valid months have values from 1 - 12 (inclusive)
*
* @return true if the day is a valid day of the month otherwise false
*/
public boolean isValidMonth()
{
return month > 0 && month <= year.length;
}
}
Month.java:
/**
*
* Simple store for a month's number (1-origin), name
* and number of days in the month.
*
* @author parks
*
*/
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);
}
14. 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;
}
}