Last active
November 10, 2017 19:33
-
-
Save diogocapela/4ef158e91de9f1aafae69d8ad45c03bd to your computer and use it in GitHub Desktop.
APROG - PL06 Implementação de Algoritmos em Java e Decomposição Modular
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 Main { | |
public static void main(String[] args) { | |
String palavra; | |
boolean econtrou = false; | |
Scanner scanner = new Scanner(System.in); | |
do { | |
System.out.println("Introduza uma palavra:"); | |
palavra = scanner.next(); | |
econtrou = checkPalindrome(palavra); | |
} while (!econtrou); | |
System.out.println(palavra + "é um palindrome!"); | |
} | |
public static boolean checkPalindrome(String pal) { | |
boolean resposta = true; | |
pal = pal.toLowerCase(); | |
int tamanho = pal.length(); | |
for (int i = 0; i < tamanho / 2; i++) { | |
if (pal.charAt(i) != pal.charAt(tamanho - 1 - i)) { | |
resposta = false; | |
break; | |
} | |
} | |
return resposta; | |
} | |
} |
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 Main { | |
public static void main(String[] args) { | |
int totalAlunos; | |
int totalDisciplinas; | |
String disciplinaNome; | |
int disciplinaAprovados; | |
String impressao; | |
Scanner scanner = new Scanner(System.in); | |
do { | |
System.out.println("Introduza o número de alunos:"); | |
totalAlunos = scanner.nextInt(); | |
} while(totalAlunos <= 0); | |
do { | |
System.out.println("Introduza o número de disciplinas:"); | |
totalDisciplinas = scanner.nextInt(); | |
} while(totalDisciplinas <= 0); | |
for(int i = 1; i <= totalDisciplinas; i++) { | |
System.out.println("Introduza o nome da disciplina " + i + ":"); | |
disciplinaNome = scanner.next(); | |
do { | |
System.out.println("Introduza o número de alunos aprovados a " + disciplinaNome + ":"); | |
disciplinaAprovados = scanner.nextInt(); | |
} while(disciplinaAprovados > totalAlunos); | |
impressao = imprimirInfoDisciplina(disciplinaNome, totalAlunos, disciplinaAprovados); | |
System.out.println(impressao); | |
test(); | |
} | |
} | |
public static String imprimirInfoDisciplina(String nome, int alunosTotal, int alunosAprovados) { | |
String positivas = ""; | |
String negativas = ""; | |
String retorno; | |
for(int i = 1; i <= alunosAprovados; i++) { | |
positivas += "*"; | |
} | |
for(int i = 1; i <= (alunosTotal - alunosAprovados); i++) { | |
negativas += "*"; | |
} | |
retorno = "Disciplina: " + nome + "\n- Positivas: " + positivas + "\n- Negativas: " + negativas; | |
return retorno; | |
} | |
public static void test() { | |
if(imprimirInfoDisciplina("Matemática", 10, 5).equals("Disciplina: Matemática\n- Positivas: *****\n- Negativas: *****")) { | |
System.out.println("Test: OK"); | |
} else { | |
System.out.println("Test: NOT OK"); | |
} | |
} | |
} |
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
public class Main { | |
public static void main(String[] args) { | |
double anguloDoTriangulo = calculateTriangleAngle(0, 10, 10,"cb"); | |
System.out.println(anguloDoTriangulo); | |
} | |
public static double calculateTriangleAngle(double a, double b, double c, String angle) { | |
double resultado = 0; | |
angle = angle.toLowerCase(); | |
if(angle.equals("ab") || angle.equals("ba")) { | |
resultado = Math.acos(((a*a) + (b*b) - (c*c)) / (2*a*b)); | |
} else if(angle.equals("ac") || angle.equals("ca")) { | |
resultado = Math.acos(((a*a) + (c*c) - (b*b)) / (2*a*c)); | |
} else if(angle.equals("bc") || angle.equals("cb")) { | |
resultado = Math.acos(((b*b) + (c*c) - (a*a)) / (2*b*c)); | |
} else { | |
System.out.println("Ângulo introduzido incorrecto!"); | |
} | |
resultado = Math.toDegrees(resultado); | |
if(Double.isNaN(resultado) || a <= 0 || b <= 0 || c <= 0) { | |
System.out.println("Triângulo com dimensões impossíveis!"); | |
} | |
return resultado; | |
} | |
} |
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 Main { | |
public static void main(String[] args) { | |
double ladoA; | |
double ladoB; | |
double ladoC; | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Introduza o lado A:"); | |
ladoA = scanner.nextDouble(); | |
System.out.println("Introduza o lado B:"); | |
ladoB = scanner.nextDouble(); | |
System.out.println("Introduza o lado C:"); | |
ladoC = scanner.nextDouble(); | |
double anguloDoTriangulo1 = calculateTriangleAngle(ladoA, ladoB, ladoC,"ab"); | |
double anguloDoTriangulo2 = calculateTriangleAngle(ladoA, ladoB, ladoC,"ac"); | |
double anguloDoTriangulo3 = calculateTriangleAngle(ladoA, ladoB, ladoC,"bc"); | |
System.out.println("Ângulo AB: " + anguloDoTriangulo1); | |
System.out.println("Ângulo AC: " + anguloDoTriangulo2); | |
System.out.println("Ângulo BC: " + anguloDoTriangulo3); | |
System.out.println("Soma dos ângulos: " + (anguloDoTriangulo1 + anguloDoTriangulo2 + anguloDoTriangulo3)); | |
} | |
public static double calculateTriangleAngle(double a, double b, double c, String angle) { | |
double resultado = 0; | |
angle = angle.toLowerCase(); | |
if(angle.equals("ab") || angle.equals("ba")) { | |
resultado = Math.acos(((a*a) + (b*b) - (c*c)) / (2*a*b)); | |
} else if(angle.equals("ac") || angle.equals("ca")) { | |
resultado = Math.acos(((a*a) + (c*c) - (b*b)) / (2*a*c)); | |
} else if(angle.equals("bc") || angle.equals("cb")) { | |
resultado = Math.acos(((b*b) + (c*c) - (a*a)) / (2*b*c)); | |
} else { | |
System.out.println("Ângulo introduzido incorrecto!"); | |
} | |
resultado = Math.toDegrees(resultado); | |
if(Double.isNaN(resultado) || a <= 0 || b <= 0 || c <= 0) { | |
System.out.println("Triângulo com dimensões impossíveis!"); | |
} | |
return resultado; | |
} | |
public static void test() { | |
if( | |
calculateTriangleAngle(10,10,10,"ab") == 60.00000000000001 && | |
calculateTriangleAngle(10,10,10,"ac") == 60.00000000000001 && | |
calculateTriangleAngle(10,10,10,"bc") == 60.00000000000001 | |
) { | |
System.out.println("OK"); | |
} else { | |
System.out.println("NOT OK"); | |
} | |
} | |
} |
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
public static int quantidadeDigitosComunsNaMesmaPosicao(int num1, int num2) { | |
String num1String; | |
String num2String; | |
int resultado = 0; | |
if(num1 < 0 || num2 < 0) { | |
System.out.println("Números introduzidos incorrectos."); | |
} else { | |
num1String = Integer.toString(num1); | |
num2String = Integer.toString(num2); | |
if(num2String.length() > num1String.length()) { | |
String temp = num1String; | |
num1String = num2String; | |
num2String = temp; | |
} | |
for(int i = 0; i < num2String.length(); i++) { | |
if(num1String.charAt(i) == num2String.charAt(i)) { | |
resultado++; | |
} | |
} | |
} | |
return resultado; | |
} |
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 Main { | |
public static void main(String[] args) { | |
int n; | |
int par1; | |
int par2; | |
int maiorDigitosComuns = 0; | |
int maiorPar1 = 0; | |
int maiorPar2 = 0; | |
Scanner scanner = new Scanner(System.in); | |
do { | |
System.out.println("Introduza N:"); | |
n = scanner.nextInt(); | |
} while (n <= 0); | |
for (int i = 0; i < n; i++) { | |
System.out.println("Introduza um novo par de números:"); | |
System.out.println("----------"); | |
System.out.println("Par 1:"); | |
par1 = scanner.nextInt(); | |
System.out.println("Par 2:"); | |
par2 = scanner.nextInt(); | |
if (quantidadeDigitosComunsNaMesmaPosicao(par1, par2) > maiorDigitosComuns) { | |
maiorDigitosComuns = quantidadeDigitosComunsNaMesmaPosicao(par1, par2); | |
maiorPar1 = par1; | |
maiorPar2 = par2; | |
} | |
} | |
System.out.println("O par que têm mais digitos em comum é o " + maiorPar1 + " e " + maiorPar2 + ". Têm " + maiorDigitosComuns + " digitos em comum."); | |
} | |
public static int quantidadeDigitosComunsNaMesmaPosicao(int num1, int num2) { | |
String num1String; | |
String num2String; | |
int resultado = 0; | |
if (num1 < 0 || num2 < 0) { | |
System.out.println("Números introduzidos incorrectos."); | |
} else { | |
num1String = Integer.toString(num1); | |
num2String = Integer.toString(num2); | |
if (num2String.length() > num1String.length()) { | |
String temp = num1String; | |
num1String = num2String; | |
num2String = temp; | |
} | |
for (int i = 0; i < num2String.length(); i++) { | |
if (num1String.charAt(i) == num2String.charAt(i)) { | |
resultado++; | |
} | |
} | |
} | |
return resultado; | |
} | |
} |
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 Main { | |
public static void main(String[] args) { | |
String tipo; | |
double raio; | |
double altura; | |
double volume; | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Qual é o tipo de sólido?"); | |
tipo = scanner.next(); | |
tipo = tipo.toLowerCase(); | |
if (tipo.equals("esfera")) { | |
System.out.println("Qual é o raio?"); | |
raio = scanner.nextInt(); | |
volume = calcularVolume(tipo, raio, 0); | |
System.out.println("----------"); | |
System.out.println("Volume: " + volume); | |
System.out.println("----------"); | |
} else if (tipo.equals("cilindro") || tipo.equals("cone")) { | |
System.out.println("Qual é o raio?"); | |
raio = scanner.nextInt(); | |
System.out.println("Qual é a altura?"); | |
altura = scanner.nextInt(); | |
volume = calcularVolume(tipo, raio, altura); | |
} else if (tipo.equals("fim")) { | |
} | |
} | |
public static double calcularVolume(String tipoDeSolido, double raio, double altura) { | |
double volume = 0; | |
tipoDeSolido = tipoDeSolido.toLowerCase(); | |
if (tipoDeSolido.equals("esfera")) { | |
volume = (4 / 3) * Math.PI * Math.pow(raio, 3); | |
} else if (tipoDeSolido.equals("cilindro")) { | |
volume = Math.PI * Math.pow(raio, 2) * altura; | |
} else if (tipoDeSolido.equals("cone")) { | |
volume = (1 / 3) * Math.PI * Math.pow(raio, 2) * altura; | |
} else { | |
} | |
return volume; | |
} | |
} |
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
public static boolean verificarOctal(int num) { | |
boolean eOctal = true; | |
String numString = Integer.toString(num); | |
for (int i = 0; i < numString.length(); i++) { | |
int digito = Character.getNumericValue(numString.charAt(i)); | |
if (digito > 7 || digito < 0) { | |
eOctal = false; | |
} | |
} | |
return eOctal; | |
} |
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
public static double converterOctalEmDecimal(double numOctal) { | |
double numDecimal = 0; | |
int potencia = 0; | |
while(numOctal > 0) { | |
// Sacar o algarismo da direita | |
double alg = numOctal % 10; | |
// Converter para decimal e juntar ao numDecimal | |
numDecimal = numDecimal + alg * Math.pow(8, potencia); | |
// Remover os algarismos utilizados do numOctal | |
numOctal = Math.floor(numOctal / 10); | |
// Aumentar a ordem da potencia | |
potencia++; | |
} | |
return numDecimal; | |
} |
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 Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int numero; | |
do { | |
System.out.println("Escreve o numero em base hexadecimal, para terminal digite 0"); | |
numero = scanner.nextInt(); | |
if (numero == 0) { | |
System.out.println("processo terminado"); | |
} else { | |
String resultado = hexadecimal(numero); | |
System.out.println("O numero em base hexadecimal é " + resultado); | |
} | |
} while (numero != 0); | |
} | |
public static String hexadecimal(int decimal) { | |
String hexadecimal = ""; | |
while (decimal != 0) { | |
int algdec = decimal % 16; | |
String algdecString = ""; | |
switch (algdec) { | |
case 0: | |
algdecString = "0"; | |
break; | |
case 1: | |
algdecString = "1"; | |
break; | |
case 3: | |
algdecString = "3"; | |
break; | |
case 4: | |
algdecString = "4"; | |
break; | |
case 5: | |
algdecString = "5"; | |
break; | |
case 6: | |
algdecString = "6"; | |
break; | |
case 7: | |
algdecString = "7"; | |
break; | |
case 8: | |
algdecString = "8"; | |
break; | |
case 9: | |
algdecString = "9"; | |
break; | |
case 10: | |
algdecString = "A"; | |
break; | |
case 11: | |
algdecString = "B"; | |
break; | |
case 12: | |
algdecString = "C"; | |
break; | |
case 13: | |
algdecString = "D"; | |
break; | |
case 14: | |
algdecString = "E"; | |
break; | |
case 15: | |
algdecString = "F"; | |
break; | |
default: | |
algdecString = ""; | |
break; | |
} | |
hexadecimal = algdecString + hexadecimal; | |
decimal = decimal / 16; | |
} | |
return hexadecimal; | |
} | |
} |
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
public static boolean verificarSeNumeroEPerfeito(double num) { | |
boolean ePerfeito = false; | |
double clone = num; | |
double soma = 0; | |
for(int i = 1; i < num; i++) { | |
if(num % i == 0) { | |
soma = soma + i; | |
} | |
} | |
if(soma == clone) { | |
ePerfeito = true; | |
} | |
return ePerfeito; | |
} |
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 Main { | |
public static void main(String[] args) { | |
int n; | |
double num; | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Introduza N:"); | |
n = scanner.nextInt(); | |
for(int i = 0; i < n; i++) { | |
System.out.println("Introduza um número:"); | |
num = scanner.nextDouble(); | |
if(verificarSeNumeroEPerfeito(num)) { | |
System.out.println("O número com a posição " + i + " é um número perfeito."); | |
} | |
} | |
} | |
public static boolean verificarSeNumeroEPerfeito(double num) { | |
boolean ePerfeito = false; | |
double clone = num; | |
double soma = 0; | |
for(int i = 1; i < num; i++) { | |
if(num % i == 0) { | |
soma = soma + i; | |
} | |
} | |
if(soma == clone) { | |
ePerfeito = true; | |
} | |
return ePerfeito; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment