Last active
October 25, 2023 10:33
-
-
Save Magoninho/f9dd7f9693b1db40c4806a0ebf8a2b5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class PrimeiraQuestao { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
try { | |
System.out.print("Digite o primeiro número: "); | |
int n1 = scanner.nextInt(); | |
System.out.print("Digite o segundo número: "); | |
int n2 = scanner.nextInt(); | |
int resultado = n1 / n2; | |
System.out.println("O resultado da divisão é: " + resultado); | |
} catch (ArithmeticException e) { | |
System.out.println("Ocorreu um erro na divisão: " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
} | |
// primeira | |
public class Divisao { | |
public static void main(String[] args) { | |
try { | |
if (args.length != 2) { | |
throw new IllegalArgumentException("Por favor, forneça exatamente dois números como argumentos da linha de comando."); | |
} | |
int numero1 = Integer.parseInt(args[0]); | |
int numero2 = Integer.parseInt(args[1]); | |
int resultado = numero1 / numero2; | |
System.out.println("O resultado da divisão é: " + resultado); | |
} catch (NumberFormatException e) { | |
System.out.println(" Os argumentos fornecidos não são números válidos."); | |
} catch (ArithmeticException e) { | |
System.out.println("Ocorreu uma divisão por zero."); | |
} catch (IllegalArgumentException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
//segunda | |
import java.util.Scanner; | |
public class TerceiraQuestao { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
try { | |
System.out.print("Digite a primeira string: "); | |
String string1 = scanner.nextLine(); | |
System.out.print("Digite a segunda string: "); | |
String string2 = scanner.nextLine(); | |
if (string1 == null || string2 == null) { | |
throw new NullPointerException("Uma das strings é nula."); | |
} | |
int comparacao = string1.compareTo(string2); | |
if (comparacao < 0) { | |
System.out.println("A primeira string é lexicograficamente menor que a segunda."); | |
} else if (comparacao > 0) { | |
System.out.println("A primeira string é lexicograficamente maior que a segunda."); | |
} else { | |
System.out.println("As strings são lexicograficamente iguais."); | |
} | |
} catch (NullPointerException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
//terceira | |
public class QuartaQuestao { | |
public static void main(String[] args) { | |
try { | |
verificarString("TESTE"); | |
verificarString("Teste"); | |
verificarString("123"); | |
} catch (NonLetterCharacterException e) { | |
System.out.println("Erro: " + e.getMessage()); | |
} catch (NonUppercaseLetterException e) { | |
System.out.println("Erro: " + e.getMessage()); | |
} | |
} | |
public static void verificarString(String str) throws NonLetterCharacterException, NonUppercaseLetterException { | |
for (char c : str.toCharArray()) { | |
if (!Character.isLetter(c)) { | |
throw new NonLetterCharacterException("A string contém um caractere que não é uma letra."); | |
} | |
if (!Character.isUpperCase(c)) { | |
throw new NonUppercaseLetterException("A string contém uma letra que não é maiúscula."); | |
} | |
} | |
} | |
} | |
class NonLetterCharacterException extends Exception { | |
public NonLetterCharacterException(String message) { | |
super(message); | |
} | |
} | |
class NonUppercaseLetterException extends Exception { | |
public NonUppercaseLetterException(String message) { | |
super(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment