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.

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