abner2
This commit is contained in:
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
62
src/exercicio01/AgendaContatos.java
Normal file
62
src/exercicio01/AgendaContatos.java
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class Contato {
|
||||||
|
String nome;
|
||||||
|
String telefone;
|
||||||
|
String email;
|
||||||
|
|
||||||
|
public Contato(String nome, String telefone, String email) {
|
||||||
|
this.nome = nome;
|
||||||
|
this.telefone = telefone;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AgendaContatos {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
ArrayList<Contato> contatos = new ArrayList<>();
|
||||||
|
|
||||||
|
while (contatos.size() < 10) {
|
||||||
|
System.out.println("Contato " + (contatos.size() + 1) + ":");
|
||||||
|
System.out.print("Nome (ou 'sair' para parar): ");
|
||||||
|
String nome = scanner.nextLine();
|
||||||
|
|
||||||
|
if (nome.equalsIgnoreCase("sair")) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("Telefone: ");
|
||||||
|
String telefone = scanner.nextLine();
|
||||||
|
System.out.print("Email: ");
|
||||||
|
String email = scanner.nextLine();
|
||||||
|
|
||||||
|
contatos.add(new Contato(nome, telefone, email));
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nTodos os contatos:");
|
||||||
|
for (Contato c : contatos) {
|
||||||
|
System.out.println("Nome: " + c.nome + " | Tel: " + c.telefone + " | Email: " + c.email);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("\nDigite nome para buscar: ");
|
||||||
|
String nomeBusca = scanner.nextLine();
|
||||||
|
|
||||||
|
for (Contato c : contatos) {
|
||||||
|
if (c.nome.equalsIgnoreCase(nomeBusca)) {
|
||||||
|
System.out.println("Encontrado!");
|
||||||
|
System.out.println("Telefone: " + c.telefone);
|
||||||
|
System.out.println("Email: " + c.email);
|
||||||
|
scanner.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Contato nao encontrado.");
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
71
src/exercicio01/Calculadora.java
Normal file
71
src/exercicio01/Calculadora.java
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Calculadora {
|
||||||
|
|
||||||
|
public double soma(double a, double b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double subtracao(double a, double b) {
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double multiplicacao(double a, double b) {
|
||||||
|
return a * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double divisao(double a, double b) {
|
||||||
|
if (b != 0) {
|
||||||
|
return a / b;
|
||||||
|
} else {
|
||||||
|
System.out.println("Erro: Divisao por zero!");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
Calculadora calc = new Calculadora();
|
||||||
|
|
||||||
|
System.out.print("Digite o primeiro numero: ");
|
||||||
|
double num1 = scanner.nextDouble();
|
||||||
|
|
||||||
|
System.out.print("Digite o segundo numero: ");
|
||||||
|
double num2 = scanner.nextDouble();
|
||||||
|
|
||||||
|
System.out.println("Escolha a operacao:");
|
||||||
|
System.out.println("1 - Soma");
|
||||||
|
System.out.println("2 - Subtracao");
|
||||||
|
System.out.println("3 - Multiplicacao");
|
||||||
|
System.out.println("4 - Divisao");
|
||||||
|
int opcao = scanner.nextInt();
|
||||||
|
|
||||||
|
double resultado = 0;
|
||||||
|
switch (opcao) {
|
||||||
|
case 1:
|
||||||
|
resultado = calc.soma(num1, num2);
|
||||||
|
System.out.println("Resultado: " + resultado);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
resultado = calc.subtracao(num1, num2);
|
||||||
|
System.out.println("Resultado: " + resultado);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
resultado = calc.multiplicacao(num1, num2);
|
||||||
|
System.out.println("Resultado: " + resultado);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
resultado = calc.divisao(num1, num2);
|
||||||
|
if (num2 != 0) {
|
||||||
|
System.out.println("Resultado: " + resultado);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Opcao invalida!");
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/exercicio01/CalculadoraFatorial.java
Normal file
34
src/exercicio01/CalculadoraFatorial.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class CalculadoraFatorial {
|
||||||
|
|
||||||
|
public long calcularFatorial(int n) {
|
||||||
|
if (n < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (n == 0 || n == 1) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return n * calcularFatorial(n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
CalculadoraFatorial calc = new CalculadoraFatorial();
|
||||||
|
|
||||||
|
System.out.print("Digite um numero inteiro nao negativo: ");
|
||||||
|
int numero = scanner.nextInt();
|
||||||
|
|
||||||
|
long resultado = calc.calcularFatorial(numero);
|
||||||
|
|
||||||
|
if (resultado == -1) {
|
||||||
|
System.out.println("Erro: Numero deve ser nao negativo!");
|
||||||
|
} else {
|
||||||
|
System.out.println("Fatorial de " + numero + " = " + resultado);
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/exercicio01/ConversorTemperatura.java
Normal file
44
src/exercicio01/ConversorTemperatura.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ConversorTemperatura {
|
||||||
|
|
||||||
|
public double celsiusParaFahrenheit(double celsius) {
|
||||||
|
return (celsius * 9.0 / 5.0) + 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double fahrenheitParaCelsius(double fahrenheit) {
|
||||||
|
return (fahrenheit - 32) * 5.0 / 9.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
ConversorTemperatura conversor = new ConversorTemperatura();
|
||||||
|
|
||||||
|
System.out.println("Escolha a conversao:");
|
||||||
|
System.out.println("1 - Celsius para Fahrenheit");
|
||||||
|
System.out.println("2 - Fahrenheit para Celsius");
|
||||||
|
int opcao = scanner.nextInt();
|
||||||
|
|
||||||
|
System.out.print("Digite a temperatura: ");
|
||||||
|
double temperatura = scanner.nextDouble();
|
||||||
|
|
||||||
|
double resultado = 0;
|
||||||
|
|
||||||
|
switch (opcao) {
|
||||||
|
case 1:
|
||||||
|
resultado = conversor.celsiusParaFahrenheit(temperatura);
|
||||||
|
System.out.println(temperatura + "°C = " + resultado + "°F");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
resultado = conversor.fahrenheitParaCelsius(temperatura);
|
||||||
|
System.out.println(temperatura + "°F = " + resultado + "°C");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Opcao invalida!");
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/exercicio01/MaiorMenorMatriz.java
Normal file
44
src/exercicio01/MaiorMenorMatriz.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class MaiorMenorMatriz {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.print("Digite o numero de linhas: ");
|
||||||
|
int linhas = scanner.nextInt();
|
||||||
|
System.out.print("Digite o numero de colunas: ");
|
||||||
|
int colunas = scanner.nextInt();
|
||||||
|
|
||||||
|
int[][] matriz = new int[linhas][colunas];
|
||||||
|
|
||||||
|
System.out.println("Digite os elementos da matriz:");
|
||||||
|
for (int i = 0; i < linhas; i++) {
|
||||||
|
for (int j = 0; j < colunas; j++) {
|
||||||
|
System.out.print("Elemento [" + i + "][" + j + "]: ");
|
||||||
|
matriz[i][j] = scanner.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int maior = matriz[0][0];
|
||||||
|
int menor = matriz[0][0];
|
||||||
|
|
||||||
|
for (int i = 0; i < linhas; i++) {
|
||||||
|
for (int j = 0; j < colunas; j++) {
|
||||||
|
if (matriz[i][j] > maior) {
|
||||||
|
maior = matriz[i][j];
|
||||||
|
}
|
||||||
|
if (matriz[i][j] < menor) {
|
||||||
|
menor = matriz[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Maior elemento: " + maior);
|
||||||
|
System.out.println("Menor elemento: " + menor);
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/exercicio01/Numero.java
Normal file
30
src/exercicio01/Numero.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
public class Numero {
|
||||||
|
private int valor;
|
||||||
|
|
||||||
|
public Numero(int valor) {
|
||||||
|
this.valor = valor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValor(int valor) {
|
||||||
|
this.valor = valor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValor() {
|
||||||
|
return valor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void imprimirValor() {
|
||||||
|
System.out.println("Valor: " + valor);
|
||||||
|
System.out.println("Endereco de memoria (hash): " + System.identityHashCode(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Numero num = new Numero(42);
|
||||||
|
num.imprimirValor();
|
||||||
|
|
||||||
|
num.setValor(100);
|
||||||
|
num.imprimirValor();
|
||||||
|
}
|
||||||
|
}
|
||||||
74
src/exercicio01/Pessoa.java
Normal file
74
src/exercicio01/Pessoa.java
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
public class Pessoa {
|
||||||
|
private String nome;
|
||||||
|
private int idade;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
public Pessoa() {
|
||||||
|
this.nome = "Não informado";
|
||||||
|
this.idade = 0;
|
||||||
|
this.email = "Não informado";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pessoa(String nome, int idade, String email) {
|
||||||
|
this.nome = nome;
|
||||||
|
this.idade = idade;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNome() {
|
||||||
|
return nome;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIdade() {
|
||||||
|
return idade;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNome(String nome) {
|
||||||
|
if (nome != null && !nome.trim().isEmpty()) {
|
||||||
|
this.nome = nome;
|
||||||
|
} else {
|
||||||
|
System.out.println("Nome não pode ser vazio!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdade(int idade) {
|
||||||
|
if (idade >= 0 && idade <= 150) {
|
||||||
|
this.idade = idade;
|
||||||
|
} else {
|
||||||
|
System.out.println("Idade deve estar entre 0 e 150 anos!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
if (email != null && email.contains("@")) {
|
||||||
|
this.email = email;
|
||||||
|
} else {
|
||||||
|
System.out.println("Email deve conter '@'!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exibirInformacoes() {
|
||||||
|
System.out.println("Nome: " + nome);
|
||||||
|
System.out.println("Idade: " + idade);
|
||||||
|
System.out.println("Email: " + email);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isMaiorIdade() {
|
||||||
|
return idade >= 18;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Pessoa{" +
|
||||||
|
"nome='" + nome + '\'' +
|
||||||
|
", idade=" + idade +
|
||||||
|
", email='" + email + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
65
src/exercicio01/SistemaFuncionarios.java
Normal file
65
src/exercicio01/SistemaFuncionarios.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class Funcionario {
|
||||||
|
String nome;
|
||||||
|
int id;
|
||||||
|
double salario;
|
||||||
|
String departamento;
|
||||||
|
|
||||||
|
public Funcionario(String nome, int id, double salario, String departamento) {
|
||||||
|
this.nome = nome;
|
||||||
|
this.id = id;
|
||||||
|
this.salario = salario;
|
||||||
|
this.departamento = departamento;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SistemaFuncionarios {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
ArrayList<Funcionario> funcionarios = new ArrayList<>();
|
||||||
|
|
||||||
|
System.out.print("Quantos funcionarios deseja cadastrar? ");
|
||||||
|
int qtd = scanner.nextInt();
|
||||||
|
scanner.nextLine();
|
||||||
|
|
||||||
|
for (int i = 0; i < qtd; i++) {
|
||||||
|
System.out.println("Funcionario " + (i + 1) + ":");
|
||||||
|
System.out.print("Nome: ");
|
||||||
|
String nome = scanner.nextLine();
|
||||||
|
System.out.print("ID: ");
|
||||||
|
int id = scanner.nextInt();
|
||||||
|
System.out.print("Salario: ");
|
||||||
|
double salario = scanner.nextDouble();
|
||||||
|
scanner.nextLine();
|
||||||
|
System.out.print("Departamento: ");
|
||||||
|
String departamento = scanner.nextLine();
|
||||||
|
|
||||||
|
funcionarios.add(new Funcionario(nome, id, salario, departamento));
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("Digite o departamento para calcular total de salarios: ");
|
||||||
|
String depBusca = scanner.nextLine();
|
||||||
|
|
||||||
|
double totalSalarios = 0;
|
||||||
|
for (Funcionario f : funcionarios) {
|
||||||
|
if (f.departamento.equalsIgnoreCase(depBusca)) {
|
||||||
|
totalSalarios += f.salario;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Total de salarios do departamento " + depBusca + ": R$ " + totalSalarios);
|
||||||
|
|
||||||
|
System.out.println("\nTodos os funcionarios:");
|
||||||
|
for (Funcionario f : funcionarios) {
|
||||||
|
System.out.println("ID: " + f.id + " | Nome: " + f.nome +
|
||||||
|
" | Salario: R$ " + f.salario + " | Depto: " + f.departamento);
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/exercicio01/SomaDiagonais.java
Normal file
36
src/exercicio01/SomaDiagonais.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class SomaDiagonais {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.print("Digite a ordem da matriz quadrada: ");
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
|
||||||
|
int[][] matriz = new int[n][n];
|
||||||
|
|
||||||
|
System.out.println("Digite os elementos da matriz:");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
System.out.print("Elemento [" + i + "][" + j + "]: ");
|
||||||
|
matriz[i][j] = scanner.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int somaPrincipal = 0;
|
||||||
|
int somaSecundaria = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
somaPrincipal += matriz[i][i];
|
||||||
|
somaSecundaria += matriz[i][n - 1 - i];
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Soma da diagonal principal: " + somaPrincipal);
|
||||||
|
System.out.println("Soma da diagonal secundaria: " + somaSecundaria);
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/exercicio01/TestePessoa.java
Normal file
30
src/exercicio01/TestePessoa.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
public class TestePessoa {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Pessoa pessoa1 = new Pessoa();
|
||||||
|
pessoa1.exibirInformacoes();
|
||||||
|
|
||||||
|
Pessoa pessoa2 = new Pessoa("João Silva", 25, "joao@email.com");
|
||||||
|
pessoa2.exibirInformacoes();
|
||||||
|
|
||||||
|
pessoa1.setNome("Maria Santos");
|
||||||
|
pessoa1.setIdade(30);
|
||||||
|
pessoa1.setEmail("maria@email.com");
|
||||||
|
pessoa1.exibirInformacoes();
|
||||||
|
|
||||||
|
pessoa1.setNome("");
|
||||||
|
pessoa1.setIdade(-5);
|
||||||
|
pessoa1.setEmail("email-invalido");
|
||||||
|
|
||||||
|
System.out.println(pessoa1.getNome() + " é maior de idade? " + pessoa1.isMaiorIdade());
|
||||||
|
|
||||||
|
Pessoa menor = new Pessoa("Pedro Junior", 16, "pedro@email.com");
|
||||||
|
System.out.println(menor.getNome() + " é maior de idade? " + menor.isMaiorIdade());
|
||||||
|
|
||||||
|
System.out.println(pessoa1.toString());
|
||||||
|
System.out.println(pessoa2.toString());
|
||||||
|
System.out.println(menor.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/exercicio01/ValorContainer.java
Normal file
41
src/exercicio01/ValorContainer.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ValorContainer {
|
||||||
|
public int valor;
|
||||||
|
|
||||||
|
public ValorContainer(int valor) {
|
||||||
|
this.valor = valor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void trocarValores(ValorContainer refA, ValorContainer refB) {
|
||||||
|
int temp = refA.valor;
|
||||||
|
refA.valor = refB.valor;
|
||||||
|
refB.valor = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.print("Digite valor para objA: ");
|
||||||
|
int valorA = scanner.nextInt();
|
||||||
|
System.out.print("Digite valor para objB: ");
|
||||||
|
int valorB = scanner.nextInt();
|
||||||
|
|
||||||
|
ValorContainer objA = new ValorContainer(valorA);
|
||||||
|
ValorContainer objB = new ValorContainer(valorB);
|
||||||
|
|
||||||
|
System.out.println("Antes da troca:");
|
||||||
|
System.out.println("objA.valor: " + objA.valor);
|
||||||
|
System.out.println("objB.valor: " + objB.valor);
|
||||||
|
|
||||||
|
trocarValores(objA, objB);
|
||||||
|
|
||||||
|
System.out.println("Depois da troca:");
|
||||||
|
System.out.println("objA.valor: " + objA.valor);
|
||||||
|
System.out.println("objB.valor: " + objB.valor);
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/exercicio01/VerificadorPalindromo.java
Normal file
28
src/exercicio01/VerificadorPalindromo.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package exercicio01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class VerificadorPalindromo {
|
||||||
|
|
||||||
|
public boolean isPalindromo(String texto) {
|
||||||
|
String textoLimpo = texto.replaceAll("\\s+", "").toLowerCase();
|
||||||
|
String textoReverso = new StringBuilder(textoLimpo).reverse().toString();
|
||||||
|
return textoLimpo.equals(textoReverso);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
VerificadorPalindromo verificador = new VerificadorPalindromo();
|
||||||
|
|
||||||
|
System.out.print("Digite uma palavra ou frase: ");
|
||||||
|
String entrada = scanner.nextLine();
|
||||||
|
|
||||||
|
if (verificador.isPalindromo(entrada)) {
|
||||||
|
System.out.println("'" + entrada + "' eh um palindromo!");
|
||||||
|
} else {
|
||||||
|
System.out.println("'" + entrada + "' nao eh um palindromo.");
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user