SlideShare a Scribd company logo
1 of 14
Download to read offline
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.
/*
@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.
*
* 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)
{
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
*/
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)
*
* @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;
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
// 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();
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);
*
* @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;
}
/**
* 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;
}
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()) {
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);
}
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;
}
}

More Related Content

Similar to CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf

Modify the Date class that was covered in the lecture which overload.pdf
Modify the Date class that was covered in the lecture which overload.pdfModify the Date class that was covered in the lecture which overload.pdf
Modify the Date class that was covered in the lecture which overload.pdf
saxenaavnish1
 
Answer main.cpp.pdf
Answer main.cpp.pdfAnswer main.cpp.pdf
Answer main.cpp.pdf
vichu19891
 
Pro.docx
Pro.docxPro.docx
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
BANSALANKIT1077
 
Exercise 1 [10 points]Write a static method named num
Exercise 1 [10 points]Write a static method named numExercise 1 [10 points]Write a static method named num
Exercise 1 [10 points]Write a static method named num
mecklenburgstrelitzh
 
@author Haolin Jin To generate weather for locatio.pdf
 @author Haolin Jin To generate weather for locatio.pdf @author Haolin Jin To generate weather for locatio.pdf
@author Haolin Jin To generate weather for locatio.pdf
chennaiallfoodwear
 
Exercise 1 [10 points]Write a static method named numUniq.docx
Exercise 1 [10 points]Write a static method named numUniq.docxExercise 1 [10 points]Write a static method named numUniq.docx
Exercise 1 [10 points]Write a static method named numUniq.docx
rhetttrevannion
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
ashokadyes
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docxSinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
jennifer822
 
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdfimport java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
aquacareser
 
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docxQ2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
amrit47
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 

Similar to CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf (16)

Modify the Date class that was covered in the lecture which overload.pdf
Modify the Date class that was covered in the lecture which overload.pdfModify the Date class that was covered in the lecture which overload.pdf
Modify the Date class that was covered in the lecture which overload.pdf
 
Answer main.cpp.pdf
Answer main.cpp.pdfAnswer main.cpp.pdf
Answer main.cpp.pdf
 
Pro.docx
Pro.docxPro.docx
Pro.docx
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
 
Exercise 1 [10 points]Write a static method named num
Exercise 1 [10 points]Write a static method named numExercise 1 [10 points]Write a static method named num
Exercise 1 [10 points]Write a static method named num
 
@author Haolin Jin To generate weather for locatio.pdf
 @author Haolin Jin To generate weather for locatio.pdf @author Haolin Jin To generate weather for locatio.pdf
@author Haolin Jin To generate weather for locatio.pdf
 
Exercise 1 [10 points]Write a static method named numUniq.docx
Exercise 1 [10 points]Write a static method named numUniq.docxExercise 1 [10 points]Write a static method named numUniq.docx
Exercise 1 [10 points]Write a static method named numUniq.docx
 
Functional C++
Functional C++Functional C++
Functional C++
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docxSinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
 
Java p1
Java p1Java p1
Java p1
 
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdfimport java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
 
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docxQ2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
 

More from deepua8

Balancing ANY chemical equation is done exactly t.pdf
                     Balancing ANY chemical equation is done exactly t.pdf                     Balancing ANY chemical equation is done exactly t.pdf
Balancing ANY chemical equation is done exactly t.pdf
deepua8
 
A) ionic compounds generally formed between the c.pdf
                     A) ionic compounds generally formed between the c.pdf                     A) ionic compounds generally formed between the c.pdf
A) ionic compounds generally formed between the c.pdf
deepua8
 
The fact that a molecule vibrates does not in its.pdf
                     The fact that a molecule vibrates does not in its.pdf                     The fact that a molecule vibrates does not in its.pdf
The fact that a molecule vibrates does not in its.pdf
deepua8
 

More from deepua8 (20)

Balancing ANY chemical equation is done exactly t.pdf
                     Balancing ANY chemical equation is done exactly t.pdf                     Balancing ANY chemical equation is done exactly t.pdf
Balancing ANY chemical equation is done exactly t.pdf
 
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
 
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
 
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
 
1. 252.125Solution1. 252.125.pdf
1. 252.125Solution1. 252.125.pdf1. 252.125Solution1. 252.125.pdf
1. 252.125Solution1. 252.125.pdf
 
(B) 0.815Solution(B) 0.815.pdf
(B) 0.815Solution(B) 0.815.pdf(B) 0.815Solution(B) 0.815.pdf
(B) 0.815Solution(B) 0.815.pdf
 
Suppose AFnSolution Suppose AFn.pdf
 Suppose  AFnSolution Suppose  AFn.pdf Suppose  AFnSolution Suppose  AFn.pdf
Suppose AFnSolution Suppose AFn.pdf
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Definition of Log-Normal DistributionA statistical distr.pdf
  Definition of Log-Normal DistributionA statistical distr.pdf  Definition of Log-Normal DistributionA statistical distr.pdf
Definition of Log-Normal DistributionA statistical distr.pdf
 
Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq) Ste.pdf
                     Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq)  Ste.pdf                     Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq)  Ste.pdf
Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq) Ste.pdf
 
The compounds with low oxidation states (O.S.) be.pdf
                     The compounds with low oxidation states (O.S.) be.pdf                     The compounds with low oxidation states (O.S.) be.pdf
The compounds with low oxidation states (O.S.) be.pdf
 
A) ionic compounds generally formed between the c.pdf
                     A) ionic compounds generally formed between the c.pdf                     A) ionic compounds generally formed between the c.pdf
A) ionic compounds generally formed between the c.pdf
 
A ethers ethers contain R-O-R linkage not carbony.pdf
                     A ethers ethers contain R-O-R linkage not carbony.pdf                     A ethers ethers contain R-O-R linkage not carbony.pdf
A ethers ethers contain R-O-R linkage not carbony.pdf
 
The fact that a molecule vibrates does not in its.pdf
                     The fact that a molecule vibrates does not in its.pdf                     The fact that a molecule vibrates does not in its.pdf
The fact that a molecule vibrates does not in its.pdf
 
The compounds of interest are Na2S and H2SO4. Th.pdf
                     The compounds of interest are Na2S and H2SO4.  Th.pdf                     The compounds of interest are Na2S and H2SO4.  Th.pdf
The compounds of interest are Na2S and H2SO4. Th.pdf
 
sucrose has a formula of C12H22O11while ammonia h.pdf
                     sucrose has a formula of C12H22O11while ammonia h.pdf                     sucrose has a formula of C12H22O11while ammonia h.pdf
sucrose has a formula of C12H22O11while ammonia h.pdf
 
reduction strength Fe Pb As .pdf
                     reduction strength Fe  Pb  As                 .pdf                     reduction strength Fe  Pb  As                 .pdf
reduction strength Fe Pb As .pdf
 
sp3d since it has 1s 3p and 1d orbitals available.pdf
                     sp3d since it has 1s 3p and 1d orbitals available.pdf                     sp3d since it has 1s 3p and 1d orbitals available.pdf
sp3d since it has 1s 3p and 1d orbitals available.pdf
 
No standard potential data given. .pdf
                     No standard potential data given.                .pdf                     No standard potential data given.                .pdf
No standard potential data given. .pdf
 
x2 + 4xSolutionx2 + 4x.pdf
x2 + 4xSolutionx2 + 4x.pdfx2 + 4xSolutionx2 + 4x.pdf
x2 + 4xSolutionx2 + 4x.pdf
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 

Recently uploaded (20)

ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 

CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf

  • 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; } }