Introdução
O método divideUnsigned() é um dos métodos da classe Integer em Java. O método é usado para retornar o quociente (sem sinal) obtido pela divisão do primeiro argumento (dividendo) pelo segundo argumento (divisor). Este laboratório fornece um guia passo a passo sobre como usar o método divideUnsigned() em Java.
Importar a classe Integer
Nesta etapa, importe a classe java.lang.Integer para a classe DivideUnsignedDemo.
import java.lang.Integer;
Definir o método main
Nesta etapa, defina o método main() dentro da classe DivideUnsignedDemo.
public class DivideUnsignedDemo {
public static void main(String[] args) {
// code here
}
}
Chamar o método divideUnsigned()
Nesta etapa, defina duas variáveis inteiras - dividendo e divisor - e, em seguida, use-as como argumentos para chamar o método divideUnsigned(). O método retorna o quociente sem sinal obtido pela divisão do dividendo pelo 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);
}
}
Saída: The quotient of 100 and 5 is 20
Dividir inteiros com sinal
Nesta etapa, divida dois inteiros com sinal usando o método divideUnsigned().
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);
}
}
Saída: The quotient of -100 and -5 is 0
Divisão por zero
Nesta etapa, tente dividir um inteiro por zero usando o método divideUnsigned().
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);
}
}
Saída: Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideUnsignedDemo.main(DivideUnsignedDemo.java:6)
Dividir usando um loop
Nesta etapa, divida um inteiro com diferentes divisores usando um 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");
}
}
}
}
Saída:
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
Compilar e executar o programa
Nesta etapa, abra seu terminal, navegue até o diretório ~/project, compile o arquivo DivideUnsignedDemo.java usando o comando javac e, em seguida, execute o arquivo compilado usando o comando java.
javac DivideUnsignedDemo.java && java DivideUnsignedDemo
Saída:
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
Modificar o programa
Nesta etapa, modifique o programa DivideUnsignedDemo para calcular o quociente de dois inteiros x e 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);
}
}
Saída: The quotient obtained by dividing 200 with -8 is 1431655764
Resumo
Este laboratório forneceu um guia passo a passo sobre como usar o método divideUnsigned() em Java. Você aprendeu como importar a classe java.lang.Integer, definir o método main(), chamar o método divideUnsigned(), dividir inteiros com sinal, dividir por zero, dividir usando um loop, compilar e executar o programa e modificar o programa para calcular o quociente de dois inteiros.



