Exercicio 1
import java.util.Scanner;
public class TestandoDivisor {
static int divisao (int n1, int n2) {
if ( n1 == 0 || n2 == 0) {
throw new ErroDeDivisorException("ERRO!\nVocê não pode digitar o valor 0 " +
"como divisor! ");
}
return n1 / n2;
}
public static void main(String[] args) {
int num1;
int num2;
int resultado;
Scanner sc = new Scanner(System.in);
try {
System.out.println("Digite o primeiro numero: ");
num1 = sc.nextInt();
System.out.println("Digite o segundo numero: ");
num2 = sc.nextInt();
System.out.println("\n" + num1 + "/" + num2);
resultado = divisao(num1, num2);
System.out.println("\n\nO resultado da divisao é " + resultado);
} catch (ErroDeDivisorException e){
System.out.println(e.getMessage());
}
}
}
============================================
public class ErroDeDivisorException extends RuntimeException{
private String mensagem;
public ErroDeDivisorException(String mensagem){
this.mensagem = mensagem;
}
public String getMessage() {
return mensagem;
}
}
*****************************************************************
Exercício 2 ->
import java.util.Scanner;
public class LerSenha {
static boolean testarSenha(int senha, int teste) {
if (senha == teste){
System.out.println("SENHA CORRETA!\n\n\tSeja bem vindo\n************************");
return true;
}else
throw new SenhaInvalidaException("SENHA INCORRETA! \nTente novamente!\n\n");
}
public static void main(String[] args) {
int senhaReal = 110405;
int senhaTeste;
boolean resultado = false;
Scanner sc = new Scanner(System.in);
while (!resultado) {
try {
System.out.println("SENHA DE ACESSO: ");
senhaTeste = sc.nextInt();
System.out.println("Senha Digita: " + senhaTeste);
resultado = testarSenha(senhaReal, senhaTeste);
} catch (SenhaInvalidaException e) {
System.out.println(e.getMessage());
}
}
}
}
============================================
public class SenhaInvalidaException extends RuntimeException{
private String mensagem;
public SenhaInvalidaException(String mensagem){
this.mensagem = mensagem;
}
public String getMessage() {
return mensagem;
}
}