Return Method Tests
Each example assumes the students file name is Example#.java replacing # with the actual example number. You can call the methods based on the student filename of your choice.

Example 1 - Add Five

Students write an int method named addFive(int number) that takes an integer as a parameter and returns an int equal to 5 plus the parameter value.

Grading Tests:

1
import org.junit.Test;
2
import static org.junit.Assert.*;
3
​
4
public class CodingRoomsUnitTests {
5
@Test
6
public void testDefaultCase() {
7
// You may rename this method to better suit the purpose of your test case
8
// Your test case logic here
9
assertEquals(10, Example1.addFive(5));
10
assertEquals(5, Example1.addFive(0));
11
assertEquals(11, Example1.addFive(6));
12
}
13
}
Copied!

Sample Solution:

Example1.java
1
public class Example1
2
{
3
public static int addFive(int number)
4
{
5
number += 5;
6
return number;
7
}
8
}
Copied!

Example 2 - Average

Students write a method that takes 5 int values as parameters and returns the average value of the 5 ints as a double. The method must be named average() and it must have 5 int parameters. The method must return a double.
Example: Calling average(1, 5, 7, 4, 10) would return 5.4.
Note: Assertions with doubles must have delta value (tolerance when comparing values).

Grading Tests:

1
import org.junit.Test;
2
import static org.junit.Assert.*;
3
​
4
public class CodingRoomsUnitTests {
5
@Test
6
public void testDefaultCase() {
7
// You may rename this method to better suit the purpose of your test case
8
// Your test case logic here
9
assertEquals(5.4, Example2.average(1, 5, 7, 4, 10), 0.001);
10
assertEquals(9.6, Example2.average(5, 9, 24, 6, 4), 0.001);
11
}
12
}
Copied!

Sample Solution:

Example2.java
1
public class Example2
2
{
3
public static double average(int a, int b, int c, int d, int e)
4
{
5
//Calculate sum of 5 numbers
6
int s = a + b + c + d + e;
7
//cast s to double and divide by 5
8
return (double) s / 5;
9
}
10
}
Copied!

Example 3 - Repeats

Students write a method that takes a String parameter. If the String has a double letter (i.e. contains the same letter twice in a row) then it should return true. Otherwise, it should return false.
This method must be named hasRepeat(String str) and have a String parameter. This method must return a boolean.

Grading Tests:

1
import org.junit.Test;
2
import static org.junit.Assert.*;
3
​
4
public class CodingRoomsUnitTests {
5
@Test
6
public void testDefaultCase() {
7
// You may rename this method to better suit the purpose of your test case
8
// Your test case logic here
9
assertTrue(Example3.hasRepeat("mississippi"));
10
assertFalse(Example3.hasRepeat("capsized"));
11
assertTrue(Example3.hasRepeat("mazzone"));
12
assertFalse(Example3.hasRepeat("this"));
13
}
14
}
Copied!

Sample Solution:

Example3.java
1
public class Example3
2
{
3
public static boolean hasRepeat(String str)
4
{
5
for(int i = 0; i < str.length()-1; i++)
6
{
7
if(str.substring(i, i+1).equals(str.substring(i+1, i+2)))
8
{
9
return true;
10
}
11
}
12
return false;
13
}
14
}
Copied!
Last modified 8mo ago