Skip to content

handle when divisor b is zero in all division methods in Division.Java #249

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

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

Successfully merging this pull request may close these issues.

1 participant