From df19be7620984a717b68aec6f25a475269412775 Mon Sep 17 00:00:00 2001 From: Ahsan-Siddiqi Date: Fri, 1 Nov 2024 14:44:21 -0400 Subject: [PATCH 1/2] Added Error Handling for Dividing by Zero --- .project | 11 +++++++++++ .../algorithms/mathematics/Division.java | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/.project b/.project index 3cd55b5c..3fb1df01 100644 --- a/.project +++ b/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1730486179290 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/src/com/jwetherell/algorithms/mathematics/Division.java b/src/com/jwetherell/algorithms/mathematics/Division.java index c133f741..4e4397dd 100644 --- a/src/com/jwetherell/algorithms/mathematics/Division.java +++ b/src/com/jwetherell/algorithms/mathematics/Division.java @@ -3,6 +3,9 @@ public class Division { public static final long division(int a, int b) { + if (b == 0) { + throw new IllegalArgumentException("/ by zero"); + } long result = ((long) a) / ((long) b); return result; } @@ -22,6 +25,9 @@ public static final long divisionUsingLoop(int a, int b) { } public static final long divisionUsingRecursion(int a, int b) { + if (b == 0) { + throw new IllegalArgumentException("/ by zero"); + } int absA = Math.abs(a); int absB = Math.abs(b); @@ -38,6 +44,9 @@ public static final long divisionUsingRecursion(int a, int b) { } public static final long divisionUsingMultiplication(int a, int b) { + if (b == 0) { + throw new IllegalArgumentException("/ by zero"); + } int absA = Math.abs(a); int absB = Math.abs(b); @@ -55,6 +64,9 @@ public static final long divisionUsingMultiplication(int a, int b) { } public static final long divisionUsingShift(int a, int b) { + if (b == 0) { + throw new IllegalArgumentException("/ by zero"); + } int absA = Math.abs(a); int absB = Math.abs(b); int tempA, tempB, counter; @@ -76,6 +88,9 @@ public static final long divisionUsingShift(int a, int b) { } public static final long divisionUsingLogs(int a, int b) { + if (b == 0) { + throw new IllegalArgumentException("/ by zero"); + } long absA = Math.abs(a); long absB = Math.abs(b); double logBase10A = Math.log10(absA); From a0a4573d7f93c5b41778f28d7381938e097f278c Mon Sep 17 00:00:00 2001 From: Ahsan-Siddiqi Date: Fri, 1 Nov 2024 14:55:59 -0400 Subject: [PATCH 2/2] Added Error Handling for Dividing by Zero --- .../jwetherell/algorithms/mathematics/Division.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/com/jwetherell/algorithms/mathematics/Division.java b/src/com/jwetherell/algorithms/mathematics/Division.java index 4e4397dd..79f9a605 100644 --- a/src/com/jwetherell/algorithms/mathematics/Division.java +++ b/src/com/jwetherell/algorithms/mathematics/Division.java @@ -3,6 +3,7 @@ public class Division { public static final long division(int a, int b) { + // Error if divide by zero if (b == 0) { throw new IllegalArgumentException("/ by zero"); } @@ -11,6 +12,11 @@ public static final long division(int a, int b) { } public static final long divisionUsingLoop(int a, int b) { + // Error if divide by zero + if (b == 0) { + throw new IllegalArgumentException("/ by zero"); + } + int absA = Math.abs(a); int absB = Math.abs(b); @@ -25,6 +31,7 @@ public static final long divisionUsingLoop(int a, int b) { } public static final long divisionUsingRecursion(int a, int b) { + // Error if divide by zero if (b == 0) { throw new IllegalArgumentException("/ by zero"); } @@ -44,6 +51,7 @@ public static final long divisionUsingRecursion(int a, int b) { } public static final long divisionUsingMultiplication(int a, int b) { + // Error if divide by zero if (b == 0) { throw new IllegalArgumentException("/ by zero"); } @@ -64,6 +72,7 @@ public static final long divisionUsingMultiplication(int a, int b) { } public static final long divisionUsingShift(int a, int b) { + // Error if divide by zero if (b == 0) { throw new IllegalArgumentException("/ by zero"); } @@ -88,6 +97,7 @@ public static final long divisionUsingShift(int a, int b) { } public static final long divisionUsingLogs(int a, int b) { + // Error if divide by zero if (b == 0) { throw new IllegalArgumentException("/ by zero"); }