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.


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/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") 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/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-117734{{"`Java Integer RemainderUnsigned Method`"}} java/classes_objects -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/class_methods -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/exceptions -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/modifiers -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/oop -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/user_input -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/wrapper_classes -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/identifier -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/arrays -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/comments -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/data_types -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/operators -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/output -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/strings -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/variables -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} java/system_methods -.-> lab-117734{{"`Java Integer RemainderUnsigned Method`"}} end

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