Java Integer Divideunsigned Method

JavaJavaBeginner
Practice Now

Introduction

The divideUnsigned() method is one of the methods of the Integer class in Java. The method is used to return the quotient (unsigned) obtained by dividing the first argument (dividend) with the second argument (divisor). This lab provides a step-by-step guide on how to use the divideUnsigned() method in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/classes_objects -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/class_methods -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/modifiers -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/oop -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/packages_api -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/wrapper_classes -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/identifier -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/arrays -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/comments -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/data_types -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/for_loop -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/if_else -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/operators -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/output -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/strings -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/variables -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} java/system_methods -.-> lab-117704{{"`Java Integer Divideunsigned Method`"}} end

Import the Integer class

In this step, import the java.lang.Integer class into the DivideUnsignedDemo class.

import java.lang.Integer;

Define the main method

In this step, define the main() method within the DivideUnsignedDemo class.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
      // code here
    }
}

Call the divideUnsigned() method

In this step, define two integer variables - dividend and divisor - and then use them as arguments to call the divideUnsigned() method. The method returns the unsigned quotient obtained by dividing the dividend with the divisor.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int dividend = 100;
        int divisor = 5;
        int quotient = Integer.divideUnsigned(dividend, divisor);

        System.out.println("The quotient of " + dividend + " and " + divisor + " is " + quotient);
    }
}

Output: The quotient of 100 and 5 is 20

Divide signed integers

In this step, divide two signed integers using the divideUnsigned() method.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int dividend = -100;
        int divisor = -5;
        int quotient = Integer.divideUnsigned(dividend, divisor);

        System.out.println("The quotient of " + dividend + " and " + divisor + " is " + quotient);
    }
}

Output: The quotient of -100 and -5 is 0

Divide by zero

In this step, attempt to divide an integer by zero using the divideUnsigned() method.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int dividend = 100;
        int divisor = 0;
        int quotient = Integer.divideUnsigned(dividend, divisor);

        System.out.println("The quotient of " + dividend + " and " + divisor + " is " + quotient);
    }
}

Output: Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideUnsignedDemo.main(DivideUnsignedDemo.java:6)

Divide using a loop

In this step, divide an integer with different divisors using a loop.

public class DivideUnsignedDemo {
    public static void main(String[] args){
        int dividend = 100;
        for (int i = -5; i <= 5; i++) {
            if (i != 0) {
                int quotient = Integer.divideUnsigned(dividend, i);
                System.out.println("The quotient of " + dividend + " and " + i + " is " + quotient);
            }
            else{
                System.out.println("Cannot divide by zero");
            }
        }
    }
}

Output:

The quotient of 100 and -5 is 0
The quotient of 100 and -4 is 0
The quotient of 100 and -3 is 0
The quotient of 100 and -2 is 0
The quotient of 100 and -1 is 0
Cannot divide by zero
The quotient of 100 and 1 is 100
The quotient of 100 and 2 is 50
The quotient of 100 and 3 is 33
The quotient of 100 and 4 is 25
The quotient of 100 and 5 is 20

Compile and run the program

In this step, open your terminal, navigate to the ~/project directory, compile the DivideUnsignedDemo.java file using the javac command and then run the compiled file using the java command.

javac DivideUnsignedDemo.java && java DivideUnsignedDemo

Output:

The quotient of 100 and 5 is 20
The quotient of -100 and -5 is 0
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at DivideUnsignedDemo.main(DivideUnsignedDemo.java:6)

The quotient of 100 and -5 is 0
The quotient of 100 and -4 is 0
The quotient of 100 and -3 is 0
The quotient of 100 and -2 is 0
The quotient of 100 and -1 is 0
Cannot divide by zero
The quotient of 100 and 1 is 100
The quotient of 100 and 2 is 50
The quotient of 100 and 3 is 33
The quotient of 100 and 4 is 25
The quotient of 100 and 5 is 20

Modify the program

In this step, modify the DivideUnsignedDemo program to calculate the quotient of two integers x and y.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int x = 200;
        int y = -8;
        int quotient = Integer.divideUnsigned(x, y);

        System.out.println("The quotient obtained by dividing " + x + " with " + y + " is " + quotient);
    }
}

Output: The quotient obtained by dividing 200 with -8 is 1431655764

Summary

This lab has provided a step-by-step guide on how to use the divideUnsigned() method in Java. You have learned how to import the java.lang.Integer class, define the main() method, call the divideUnsigned() method, divide signed integers, divide by zero, divide using a loop, compile and run the program, and modify the program to calculate the quotient of two integers.

Other Java Tutorials you may like