Java Integer RemainderUnsigned Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the remainderUnsigned() method of the Integer class in Java to return the remainder (unsigned) obtained by dividing the first argument with the second argument.

Define the class and main method

Define the public class and main method that will contain your code block.

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

Define the dividend and divisor

Define two integer variables to hold the values of the dividend and the divisor.

int dividend = 100;
int divisor = 5;

Find the remainder for signed values

Calculate the remainder for signed values using the modulus operator (%).

int signedRemainder = dividend % divisor;
System.out.println("The signed remainder of " + dividend + " / " + divisor + " is " + signedRemainder);

Find the remainder for unsigned values

Calculate the remainder for unsigned values using the remainderUnsigned() method.

int unsignedRemainder = Integer.remainderUnsigned(dividend, divisor);
System.out.println("The unsigned remainder of " + dividend + " / " + divisor + " is " + unsignedRemainder);

Test the code

Test the code by compiling and running the program:

javac RemainderUnsignedLab.java
java RemainderUnsignedLab

The output should show the signed remainder and unsigned remainder of the division.

User-defined Example

Create a user-defined example where the user inputs the dividend and divisor, and the program outputs the unsigned remainder.

Scanner input = new Scanner(System.in);
System.out.print("Enter the dividend: ");
dividend = input.nextInt();
System.out.print("Enter the divisor: ");
divisor = input.nextInt();
int unsignedRemainder = Integer.remainderUnsigned(dividend, divisor);
System.out.println("The unsigned remainder of " + dividend + " / " + divisor + " is " + unsignedRemainder);

Test the User-defined Example

Test the user-defined example by running the code:

javac RemainderUnsignedLab.java
java RemainderUnsignedLab

The program should prompt the user for two integers and output the unsigned remainder of the division.

Handle exceptions

Add exception handling to the user-defined example to handle invalid inputs.

Scanner input = new Scanner(System.in);
try {
    System.out.print("Enter the dividend: ");
    dividend = input.nextInt();
    System.out.print("Enter the divisor: ");
    divisor = input.nextInt();
    int unsignedRemainder = Integer.remainderUnsigned(dividend, divisor);
    System.out.println("The unsigned remainder of " + dividend + " / " + divisor + " is " + unsignedRemainder);
} catch (Exception e) {
    System.out.println("Invalid input! Please enter valid integers.");
}

Test the code

Test the program by running the code:
javac RemainderUnsignedLab.java
java RemainderUnsignedLab

The program should handle invalid inputs and output the unsigned remainder for valid inputs.

Summary

In this lab, you learned how to use the remainderUnsigned() method of the Integer class in Java to return the remainder (unsigned) obtained by dividing the first argument with the second argument. You also learned how to handle exceptions and create user-defined examples.

Other Java Tutorials you may like