Skip to content

Commit 9bd9f97

Browse files
committed
test: change data types for division task
- Change method return and parameter types to correctly throw expected exception
1 parent 462fc24 commit 9bd9f97

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ Using the [Java Exception Handling documentation][4] as a guide, implement the m
3939
Create a method that performs division while handling potential arithmetic exceptions:
4040

4141
```java
42-
public Double divide(Double numerator, Double denominator) {
42+
public Integer divide(Integer numerator, Integer denominator) {
4343
// TODO: Implement this method to divide the numerator by the denominator
44-
// - return the result of the division
45-
// - if the denominator is 0, catch the ArithmeticException and return null
4644
// - if either parameter is null, return null
45+
// - catch the ArithmeticException thrown when the denominator is 0 and return null
46+
// - return the result of the division
4747
throw new RuntimeException("Not implemented");
4848
}
4949
```
@@ -111,8 +111,8 @@ public String processData(List<String> data, int index) {
111111
ExceptionExercises exercises = new ExceptionExercises();
112112

113113
// Safe division
114-
Double result1 = exercises.divide(10.0, 2.0); // Returns 5.0
115-
Double result2 = exercises.divide(10.0, 0.0); // Returns null (handles ArithmeticException)
114+
Integer result1 = exercises.divide(10, 2); // Returns 5
115+
Integer result2 = exercises.divide(10, 0); // Returns null (handles ArithmeticException)
116116

117117
// Safe list access
118118
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

exception-handling/src/test/java/com/cbfacademy/ExceptionExercisesTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,29 @@ void setUp() {
2626
@ParameterizedTest
2727
@DisplayName(value = "test the divide method returns the expected result for valid inputs")
2828
@CsvSource({
29-
"10.0, 2.0, 5.0",
30-
"15.0, 3.0, 5.0",
31-
"7.5, 2.5, 3.0",
32-
"100.0, 4.0, 25.0",
33-
"0.0, 5.0, 0.0"
29+
"10, 2, 5",
30+
"15, 3, 5",
31+
"8, 2, 4",
32+
"100, 4, 25",
33+
"0, 5, 0"
3434
})
35-
void testDivideMethodReturnsExpectedResult(Double numerator, Double denominator, Double expectedResult) {
36-
Double result = exceptionExercises.divide(numerator, denominator);
35+
void testDivideMethodReturnsExpectedResult(Integer numerator, Integer denominator, Integer expectedResult) {
36+
Integer result = exceptionExercises.divide(numerator, denominator);
3737
assertEquals(expectedResult, result);
3838
}
3939

4040
@Test
4141
@DisplayName(value = "test the divide method returns null when denominator is 0")
4242
void testDivideMethodReturnsNullForDivisionByZero() {
43-
Double result = exceptionExercises.divide(10.0, 0.0);
43+
Integer result = exceptionExercises.divide(10, 0);
4444
assertNull(result);
4545
}
4646

4747
@Test
4848
@DisplayName(value = "test the divide method returns null when either parameter is null")
4949
void testDivideMethodReturnsNullForNullParameters() {
50-
assertNull(exceptionExercises.divide(null, 5.0));
51-
assertNull(exceptionExercises.divide(10.0, null));
50+
assertNull(exceptionExercises.divide(null, 5));
51+
assertNull(exceptionExercises.divide(10, null));
5252
assertNull(exceptionExercises.divide(null, null));
5353
}
5454

0 commit comments

Comments
 (0)