-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from FridaTveit/SumOfMultiplesUseInstanceMethod
sum-of-multiples: use instance method instead of static method [Fix #355]
- Loading branch information
Showing
2 changed files
with
90 additions
and
97 deletions.
There are no files selected for viewing
26 changes: 17 additions & 9 deletions
26
exercises/sum-of-multiples/src/example/java/SumOfMultiples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
public class SumOfMultiples { | ||
|
||
|
||
public static int sum(int number, int[] set) { | ||
|
||
private final int sum; | ||
|
||
public SumOfMultiples(int number, int[] set) { | ||
sum = calculateSum(number, set); | ||
} | ||
|
||
public int getSum() { | ||
return sum; | ||
} | ||
|
||
private int calculateSum(int number, int[] set) { | ||
|
||
int sum = 0; | ||
int count = 0; | ||
|
||
for (int i = 1; i < number; i++) { | ||
|
||
for (int j = 0; j < set.length; j++) { | ||
if (i % set[j] == 0) { | ||
count++; | ||
} | ||
} | ||
|
||
if (count > 0) { | ||
sum = sum + i; | ||
count = 0; | ||
} | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
} |
161 changes: 73 additions & 88 deletions
161
exercises/sum-of-multiples/src/test/java/SumOfMultiplesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,175 +1,160 @@ | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class SumOfMultiplesTest { | ||
|
||
|
||
|
||
@Test | ||
public void testSumOfMultiplesOf3and4UpToOne() { | ||
|
||
int[] set = { | ||
3, | ||
5 | ||
3, | ||
5 | ||
}; | ||
int output = SumOfMultiples.sum(1, set); | ||
int output = new SumOfMultiples(1, set).getSum(); | ||
assertEquals(0, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf3and5UpToFour() { | ||
|
||
int[] set = { | ||
3, | ||
5 | ||
3, | ||
5 | ||
}; | ||
int output = SumOfMultiples.sum(4, set); | ||
int output = new SumOfMultiples(4, set).getSum(); | ||
assertEquals(3, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf3and5UpToTen() { | ||
|
||
int[] set = { | ||
3, | ||
5 | ||
3, | ||
5 | ||
}; | ||
int output = SumOfMultiples.sum(10, set); | ||
int output = new SumOfMultiples(10, set).getSum(); | ||
assertEquals(23, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf3and5UpToOneHundred() { | ||
|
||
int[] set = { | ||
3, | ||
5 | ||
3, | ||
5 | ||
}; | ||
int output = SumOfMultiples.sum(100, set); | ||
int output = new SumOfMultiples(100, set).getSum(); | ||
assertEquals(2318, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf3and5UpToOneThousand() { | ||
|
||
int[] set = { | ||
3, | ||
5 | ||
3, | ||
5 | ||
}; | ||
int output = SumOfMultiples.sum(1000, set); | ||
int output = new SumOfMultiples(1000, set).getSum(); | ||
assertEquals(233168, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf7and13and17UpToTwenty() { | ||
|
||
int[] set = { | ||
7, | ||
13, | ||
17 | ||
7, | ||
13, | ||
17 | ||
}; | ||
int output = SumOfMultiples.sum(20, set); | ||
int output = new SumOfMultiples(20, set).getSum(); | ||
assertEquals(51, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf4and6UpToFifteen() { | ||
|
||
int[] set = { | ||
4, | ||
6 | ||
4, | ||
6 | ||
}; | ||
int output = SumOfMultiples.sum(15, set); | ||
int output = new SumOfMultiples(15, set).getSum(); | ||
assertEquals(30, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf5and6and8UpToOneHundredFifty() { | ||
|
||
int[] set = { | ||
5, | ||
6, | ||
8 | ||
5, | ||
6, | ||
8 | ||
}; | ||
int output = SumOfMultiples.sum(150, set); | ||
int output = new SumOfMultiples(150, set).getSum(); | ||
assertEquals(4419, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf5and25UpToTwoHundredSeventyFive() { | ||
|
||
int[] set = { | ||
5, | ||
25 | ||
5, | ||
25 | ||
}; | ||
int output = SumOfMultiples.sum(51, set); | ||
int output = new SumOfMultiples(51, set).getSum(); | ||
assertEquals(275, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOf43and47UpToTenThousand() { | ||
|
||
int[] set = { | ||
43, | ||
47 | ||
43, | ||
47 | ||
}; | ||
int output = SumOfMultiples.sum(10000, set); | ||
int output = new SumOfMultiples(10000, set).getSum(); | ||
assertEquals(2203160, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOfOneUpToOneHundred() { | ||
|
||
int[] set = { | ||
1 | ||
1 | ||
}; | ||
int output = SumOfMultiples.sum(100, set); | ||
int output = new SumOfMultiples(100, set).getSum(); | ||
assertEquals(4950, output); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
@Ignore | ||
public void testSumOfMultiplesOfNoneUpToTenThousand() { | ||
|
||
int[] set = {}; | ||
int output = SumOfMultiples.sum(10000, set); | ||
int output = new SumOfMultiples(10000, set).getSum(); | ||
assertEquals(0, output); | ||
|
||
} | ||
|
||
|
||
|
||
} |