Skip to content

Commit 1c2372e

Browse files
committed
Implemented int AbundantNumber calculator.
1 parent f9bc2a4 commit 1c2372e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package stringbasic.exercises;
2+
3+
public class AbundantNumber {
4+
5+
private int number;
6+
7+
public AbundantNumber(int number) {
8+
this.number = number;
9+
}
10+
11+
public int abundance() {
12+
int result = 0;
13+
for (int i = this.number-1; i>0; i--) {
14+
if (this.number % i == 0)
15+
result += i;
16+
}
17+
18+
return result;
19+
}
20+
21+
public boolean isAbundant() {
22+
return this.abundance() > this.number;
23+
}
24+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package stringbasic.exercises;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.Parameterized;
8+
import org.junit.runners.Parameterized.Parameters;
9+
10+
import java.util.Collection;
11+
12+
@RunWith(Parameterized.class)
13+
public class AbundantNumberTest {
14+
15+
private int number;
16+
private int abundance;
17+
private boolean isAbundant;
18+
19+
@Parameters
20+
public static Collection<Object[]> abundantNumbers () {
21+
return ImmutableList.of(
22+
new Object[] {10, 8, false},
23+
new Object[] {12, 16, true},
24+
new Object[] {18, 21, true},
25+
new Object[] {20, 22, true},
26+
new Object[] {21, 11, false},
27+
new Object[] {24, 36, true},
28+
new Object[] {30, 42, true}
29+
);
30+
}
31+
32+
public AbundantNumberTest(int number, int abundance, boolean isAbundant) {
33+
this.number = number;
34+
this.abundance = abundance;
35+
this.isAbundant = isAbundant;
36+
}
37+
38+
@Test
39+
public void testKnownCases() {
40+
AbundantNumber sut = new AbundantNumber(this.number);
41+
Assert.assertEquals(this.abundance, sut.abundance());
42+
}
43+
}

0 commit comments

Comments
 (0)