Skip to content

Conversation

UmForgotMyName
Copy link

This PR modifies all methods in Division.java to handle when the divisor, b is zero. This prevents potential runtime errors and improves the robustness of the code.

@UmForgotMyName
Copy link
Author

after reviewing the code, each method in Division.java has a new if statement to throw an illegal argument error if the divisor b is zero.

@@ -3,11 +3,17 @@
public class Division {

public static final long division(int a, int b) {
if (b == 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catches if divisor b is 0

long result = ((long) a) / ((long) b);
return result;
}

public static final long divisionUsingLoop(int a, int b) {
if (b == 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catches if divisor b is 0

@@ -22,6 +28,9 @@ public static final long divisionUsingLoop(int a, int b) {
}

public static final long divisionUsingRecursion(int a, int b) {
if (b == 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catches if divisor b is 0

@@ -38,6 +47,9 @@ public static final long divisionUsingRecursion(int a, int b) {
}

public static final long divisionUsingMultiplication(int a, int b) {
if (b == 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catches if divisor b is 0

@@ -55,6 +67,9 @@ public static final long divisionUsingMultiplication(int a, int b) {
}

public static final long divisionUsingShift(int a, int b) {
if (b == 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catches if divisor b is 0

@@ -76,6 +91,9 @@ public static final long divisionUsingShift(int a, int b) {
}

public static final long divisionUsingLogs(int a, int b) {
if (b == 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catches if divisor b is 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant