This repository was archived by the owner on Feb 10, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +119
-0
lines changed
Expand file tree Collapse file tree 6 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 1212 <module >math-functions</module >
1313 <module >account-analytics</module >
1414 <module >account-data</module >
15+ <module >sum-of-squares</module >
1516 </modules >
1617
1718 <properties >
Original file line number Diff line number Diff line change 1+ # <img src =" https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png " height =50/ >Sum of squares exercise :muscle :
2+ Improve your functional programming skills
3+ ### Task
4+ ` SumOfSquares ` is a class that allows you to calculate a sum of squares in a provided range. It is implemented using
5+ OO approach. Your job is to ** refactor the todo section using functional approach.** So the ** implementation will not use
6+ mutable variables** , and all test will pass.
7+
8+ ### Pre-conditions :heavy_exclamation_mark :
9+ You're supposed to be familiar with Java 8
10+
11+ ### How to start :question :
12+ * Just clone the repository and start working on the ** todo** section, verify your changes by running tests
13+ * If you don't have enough knowledge about this domain, check out the [ links below] ( #related-materials-information_source )
14+ * Don't worry if you got stuck, checkout the branch ** exercise/completed** and see the final implementation
15+
16+ ### Related materials :information_source :
17+ * [ Functional programming tutorial] ( https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/functional-programming-basics ) <img src =" https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png " height =20/ >
18+
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <parent >
6+ <artifactId >java-functional-features-exercises</artifactId >
7+ <groupId >com.bobocode</groupId >
8+ <version >1.0-SNAPSHOT</version >
9+ </parent >
10+ <modelVersion >4.0.0</modelVersion >
11+
12+ <artifactId >sum-of-squares</artifactId >
13+
14+
15+ </project >
Original file line number Diff line number Diff line change 1+ package com .bobocode ;
2+
3+
4+ import com .bobocode .exception .InvalidRangeException ;
5+
6+
7+ /**
8+ * This class allow to calculate a sum of squares of integer number in a certain range. It was implemented using
9+ * OO approach. Your job is to refactor it using functional approach. E.g. avoid using mutable variables
10+ */
11+ public class SumOfSquares {
12+ public static void main (String [] args ) {
13+ System .out .println ("Sum of squares from 5 to 10 is " + calculateSumOfSquaresInRange (5 , 10 ));
14+ }
15+
16+ /**
17+ * This method calculates the sum of squares of integer in the range
18+ *
19+ * @param startInclusive first element in range
20+ * @param endInclusive last element in range
21+ * @return the sum of squares of each element in the range
22+ */
23+ static int calculateSumOfSquaresInRange (int startInclusive , int endInclusive ) {
24+ if (endInclusive < startInclusive ) {
25+ throw new InvalidRangeException ();
26+ }
27+
28+ // todo: refactor using functional approach
29+ int sumOfSquares = 0 ;
30+ for (int i = startInclusive ; i <= endInclusive ; i ++) {
31+ sumOfSquares += i * i ;
32+ }
33+ return sumOfSquares ;
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package com .bobocode .exception ;
2+
3+ public class InvalidRangeException extends RuntimeException {
4+ }
Original file line number Diff line number Diff line change 1+ package com .bobocode ;
2+
3+ import com .bobocode .exception .InvalidRangeException ;
4+ import org .junit .Test ;
5+ import org .junit .runner .RunWith ;
6+ import org .junit .runners .JUnit4 ;
7+
8+ import static org .junit .Assert .assertEquals ;
9+
10+ @ RunWith (JUnit4 .class )
11+ public class SumOfSquareTest {
12+
13+ @ Test
14+ public void testCalculateSumOfSquaresOfZero () {
15+ int sumOfSquares = SumOfSquares .calculateSumOfSquaresInRange (0 , 0 );
16+
17+ assertEquals (0 , sumOfSquares );
18+ }
19+
20+ @ Test
21+ public void testCalculateSumOfSquaresOfOne () {
22+ int sumOfSquares = SumOfSquares .calculateSumOfSquaresInRange (0 , 1 );
23+
24+ assertEquals (1 , sumOfSquares );
25+ }
26+
27+ @ Test
28+ public void testCalculateSumOfSquares () {
29+ int sumOfSquares = SumOfSquares .calculateSumOfSquaresInRange (1 , 5 ); // 1*1 + 2*2 + 3*3 + 4*4 + 5*5 = 55
30+
31+ assertEquals (55 , sumOfSquares );
32+ }
33+
34+ @ Test
35+ public void testCalculateSumOfSquaresOnNegative () {
36+ int sumOfSquares = SumOfSquares .calculateSumOfSquaresInRange (-4 , -2 ); // -4*(-4) + -3*(-3) + -2*(-2) = 29
37+
38+ assertEquals (29 , sumOfSquares );
39+ }
40+
41+ @ Test (expected = InvalidRangeException .class )
42+ public void testWithInvalidRange () {
43+ SumOfSquares .calculateSumOfSquaresInRange (4 , 1 );
44+ }
45+
46+ }
You can’t perform that action at this time.
0 commit comments