Question: /** * Tests the method that returns the sum of the even * numbers between 0 and the specified value inclusive. */ @Test public void
/**
* Tests the method that returns the sum of the even
* numbers between 0 and the specified value inclusive.
*/
@Test
public void testSumOfEvens() {
int sumOfEvens = math.sumOfEvens(0);
assertEquals(0, sumOfEvens);
}
/**
* Tests the method that returns the sum of the even
* numbers between 0 and the specified value inclusive.
*/
@Test
public void testSumOfEvens2() {
int sumOfEvens = math.sumOfEvens(10);
assertEquals(30, sumOfEvens);
}
/**
* Tests the method that returns the sum of the even
* numbers between 0 and the specified value inclusive.
*/
@Test
public void testSumOfEvens3() {
int sumOfEvens = math.sumOfEvens(-10);
assertEquals(-30, sumOfEvens);
}
I'm keep getting this part wrong. Please explain me what I did wrong and the corrections.
Part above and below are in separate classes.
/**
* Returns sum of the even numbers between 0 and the specified value,
* inclusive.
* @param number upper bound
* @return sum of the even numbers between 0 and number
*/
public int sumOfEvens(int number) {
int count = 0;
int sum = 0;
while(count >= 0 && count <= number) {
count ++;
if(number % 2 == 0) {
sum += number;
}
}
while(count <= 0 && count >= number) {
if(number % 2 == -0) {
sum += number;
}
return sum;
}
return count;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
