Last active
November 17, 2017 19:20
-
-
Save diogocapela/222b8dfdb2729a12b15b4f5879ef56cf to your computer and use it in GitHub Desktop.
APROG - PL07 Estruturas de Dados Indexadas
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
| O programa calcula a percentagem de algarismos pares de uma determinada sequência de 10 números. |
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 menorNumeroArmazenadoNumVetor(int[] vetor) { | |
| int menorNumero = vetor[0]; | |
| for(int i = 1; i < vetor.length; i++) { | |
| if(vetor[i] < menorNumero) { | |
| menorNumero = vetor[i]; | |
| } | |
| } | |
| return menorNumero; | |
| } |
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 menorNumeroArmazenadoNumVetor(int[] vetor) { | |
| int menorNumero = vetor[0]; | |
| int indexMenorNumero = 0; | |
| for(int i = 1; i < vetor.length; i++) { | |
| if(vetor[i] < menorNumero) { | |
| menorNumero = vetor[i]; | |
| indexMenorNumero = i; | |
| } | |
| } | |
| System.out.println("Indíce do menor número: " + indexMenorNumero); | |
| System.out.println("Menor número: " + menorNumero); | |
| return menorNumero; | |
| } |
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 nome; | |
| int vencimento; | |
| double totalVencimentos = 0; | |
| double mediaVencimento; | |
| double limiteVencimento; | |
| int contadorFuncionarios = 0; | |
| String[] funcionariosNomes = new String[50]; | |
| int[] funcionariosVencimentos = new int[50]; | |
| int contadorFuncionariosComVencimentoInferiorLimite = 0; | |
| Scanner scanner = new Scanner(System.in); | |
| System.out.println("Introduza o limite de vencimento para comparação:"); | |
| limiteVencimento = scanner.nextInt(); | |
| do { | |
| // User input | |
| System.out.println("Introduza o nome do funcionário:"); | |
| nome = scanner.next(); | |
| if(!"tt".equals(nome)) { | |
| System.out.println("Introduza o vencimento do funcionário " + nome + ":"); | |
| vencimento = scanner.nextInt(); | |
| // Adicionar o nome e vencimento às arrays e contar o número de funcionários | |
| funcionariosNomes[contadorFuncionarios] = nome; | |
| funcionariosVencimentos[contadorFuncionarios] = vencimento; | |
| contadorFuncionarios++; | |
| // Adicionar o total de vencimentos | |
| totalVencimentos = totalVencimentos + vencimento; | |
| } | |
| } while(!"tt".equals(nome)); | |
| mediaVencimento = totalVencimentos / contadorFuncionarios; | |
| System.out.println("FUNCIONÁRIOS COM VENCIMENTO INFERIOR À MÉDIA:"); | |
| for(int i = 0; i < contadorFuncionarios; i++) { | |
| if(funcionariosVencimentos[i] < mediaVencimento) { | |
| System.out.println(funcionariosNomes[i]); | |
| } | |
| if(funcionariosVencimentos[i] < limiteVencimento) { | |
| contadorFuncionariosComVencimentoInferiorLimite++; | |
| } | |
| } | |
| System.out.println("PERCENTAGEM DE FUNCIONÁRIOS COM VENCIMENTO INFERIOR AO LIMITE INTRODUZIDO:"); | |
| double percentagem = (contadorFuncionariosComVencimentoInferiorLimite/contadorFuncionarios) * 100; | |
| System.out.println(percentagem); | |
| } | |
| } |
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.Arrays; | |
| import java.util.Scanner; | |
| public class Main { | |
| public static void main(String[] args) { | |
| Scanner scanner = new Scanner(System.in); | |
| int n; | |
| do { | |
| System.out.println("Introduza N:"); | |
| n = scanner.nextInt(); | |
| } while(n <= 0); | |
| int[] minhaArray = new int[n]; | |
| for(int i = 0; i < n; i++) { | |
| System.out.println("Introduza um número:"); | |
| minhaArray[i] = scanner.nextInt(); | |
| } | |
| System.out.println("INVERTIDA:"); | |
| int[] minhaArrayInvertida = inverterVector(minhaArray); | |
| System.out.println(Arrays.toString(minhaArrayInvertida)); | |
| System.out.println("RODADA:"); | |
| int[] minhaArrayRodada = rodarVectorDireita(minhaArrayInvertida); | |
| System.out.println(Arrays.toString(minhaArrayRodada)); | |
| } | |
| public static int[] inverterVector(int[] vector) { | |
| int[] vectorInvertido = new int[vector.length]; | |
| for(int i = 0; i < vector.length; i++) { | |
| vectorInvertido[i] = vector[vector.length - i - 1]; | |
| } | |
| return vectorInvertido; | |
| } | |
| public static int[] rodarVectorDireita(int[] vector) { | |
| int[] vectorRodado = new int[vector.length]; | |
| vectorRodado[0] = vector[vector.length - 1]; | |
| for(int i = 1; i < vector.length; i++) { | |
| vectorRodado[i] = vector[i - 1]; | |
| } | |
| return vectorRodado; | |
| } | |
| } |
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); | |
| String[] listaNomes = new String[5]; | |
| String[] listaApelidos = new String[5]; | |
| int quantidadeNomes = lerNomes(listaNomes); | |
| String apelidoDo4 = getApelido(listaNomes[1]); | |
| System.out.println(apelidoDo4); | |
| for(int i = 0; i < quantidadeNomes; i++) { | |
| listaApelidos[i] = getApelido(listaNomes[i]); | |
| } | |
| mostrarListagem(listaApelidos, quantidadeNomes); | |
| int quantiadeApelidosComecamPorS = preencherVetorApelidosComS(listaNomes); | |
| System.out.println("Percentagem de apelidos começados por S:"); | |
| System.out.println((double) (quantiadeApelidosComecamPorS / quantidadeNomes) * 100); | |
| } | |
| public static int lerNomes(String[] listaNomes) { | |
| Scanner scanner = new Scanner(System.in); | |
| String nome; | |
| int quantidadeNomes = 0; | |
| do { | |
| System.out.println("Introduza um nome completo:"); | |
| nome = scanner.nextLine(); | |
| if(!nome.equalsIgnoreCase("FIM")) { | |
| listaNomes[quantidadeNomes] = nome; | |
| quantidadeNomes++; | |
| } | |
| } while(!nome.equalsIgnoreCase("FIM") && quantidadeNomes < 5); | |
| return quantidadeNomes; | |
| } | |
| public static String getApelido(String nomeCompleto) { | |
| String[] partesDoNome = nomeCompleto.split(" "); | |
| String apelido = partesDoNome[partesDoNome.length - 1]; | |
| return apelido; | |
| } | |
| public static int preencherVetorApelidosComS(String[] v1) { | |
| int quantidadeApelidosComS = 0; | |
| String v2[] = new String[v1.length]; | |
| System.out.println("Nomes com apelidos começados por S:"); | |
| for(int i = 0; i < v1.length; i++) { | |
| String apelido = getApelido(v1[i]); | |
| String apelidoCharArray[] = apelido.split(""); | |
| if(apelidoCharArray[0].equalsIgnoreCase("s")) { | |
| v2[quantidadeApelidosComS] = v1[i]; | |
| System.out.println(v1[i]); | |
| quantidadeApelidosComS++; | |
| } | |
| } | |
| return quantidadeApelidosComS; | |
| } | |
| public static void mostrarListagem(String[] vectorNomes, int quantidadeNomes) { | |
| for(int i = 0; i < quantidadeNomes; i++) { | |
| System.out.println("Nome " + i + ": " + vectorNomes[i]); | |
| } | |
| } | |
| } |
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 String[] visitantes; | |
| public static int quantidadeVisitantes; | |
| public static void main(String[] args) { | |
| visitantes = new String[100]; | |
| quantidadeVisitantes = 0; | |
| showMenu(); | |
| } | |
| public static void showMenu() { | |
| Scanner scanner = new Scanner(System.in); | |
| int userChoice; | |
| int number; | |
| String name; | |
| String letra; | |
| do { | |
| System.out.println("#####################################"); | |
| System.out.println("1: Inserir novo visitante"); | |
| System.out.println("2: Listar todos os visitantes"); | |
| System.out.println("3: Atualizar um nome dado"); | |
| System.out.println("4: Eliminar um visitante dado"); | |
| System.out.println("5: Listar os nomes começados por uma dada letra"); | |
| System.out.println("6: Listar nomes repetidos"); | |
| System.out.println("7: Sair"); | |
| System.out.println("#####################################"); | |
| System.out.println("Selecione uma opção:"); | |
| userChoice = scanner.nextInt(); | |
| } while(userChoice <= 0 || userChoice > 7); | |
| switch (userChoice) { | |
| case 1: | |
| System.out.println("-------------------------------------"); | |
| System.out.println("Qual o nome do visitante que quer inserir?"); | |
| name = scanner.next(); | |
| insertVisitante(name); | |
| break; | |
| case 2: | |
| System.out.println("-------------------------------------"); | |
| System.out.println("A listar todos os visitantes activos..."); | |
| listarVisitantes(); | |
| break; | |
| case 3: | |
| System.out.println("-------------------------------------"); | |
| System.out.println("Qual o número do visitante que quer actualizar?"); | |
| number = scanner.nextInt(); | |
| System.out.println("-------------------------------------"); | |
| System.out.println("Número: " + number); | |
| System.out.println("Nome: " + visitantes[number]); | |
| System.out.println("-------------------------------------"); | |
| System.out.println("Actualizar o visitante " + number + " para que nome?"); | |
| name = scanner.next(); | |
| updateVisitante(number, name); | |
| break; | |
| case 4: | |
| System.out.println("-------------------------------------"); | |
| System.out.println("Qual o número do visitante que quer remover?"); | |
| number = scanner.nextInt(); | |
| deleteVisitante(number); | |
| break; | |
| case 5: | |
| System.out.println("-------------------------------------"); | |
| System.out.println("Listar todos os visitantes cujo nome começa por qual letra?"); | |
| letra = scanner.next(); | |
| System.out.println("-------------------------------------"); | |
| System.out.println("A listar todos os visitantes activos cujo nome começa por " + letra + "..."); | |
| listarVisitanteComecadosPor(letra); | |
| break; | |
| case 6: | |
| System.out.println("-------------------------------------"); | |
| System.out.println("A listar todos os visitantes activos com nomes repetidos..."); | |
| listarVisitantesNomesRepetidos(); | |
| break; | |
| case 7: | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| public static void insertVisitante(String name) { | |
| visitantes[quantidadeVisitantes] = name; | |
| quantidadeVisitantes++; | |
| showMenu(); | |
| } | |
| public static void listarVisitantes() { | |
| for(int i = 0; i < quantidadeVisitantes; i++) { | |
| System.out.println("Visitante " + i + ": " + visitantes[i]); | |
| } | |
| showMenu(); | |
| } | |
| public static void updateVisitante(int number, String newName) { | |
| visitantes[number] = newName; | |
| showMenu(); | |
| } | |
| public static void deleteVisitante(int number) { | |
| visitantes[number] = null; | |
| showMenu(); | |
| } | |
| public static void listarVisitanteComecadosPor(String letra) { | |
| for(int i = 0; i < quantidadeVisitantes; i++) { | |
| String[] letras = visitantes[i].split(""); | |
| if(letras[0].equalsIgnoreCase(letra)) { | |
| System.out.println("Visitante " + i + ": " + visitantes[i]); | |
| } | |
| } | |
| showMenu(); | |
| } | |
| public static void listarVisitantesNomesRepetidos() { | |
| for(int i = 0; i < quantidadeVisitantes; i++) { | |
| for(int k = 0; k < quantidadeVisitantes; k++) { | |
| if(visitantes[i] == visitantes[k]) { | |
| System.out.println("Visitantes repetidos:"); | |
| System.out.println("Visitantes " + i + ": " + visitantes[i]); | |
| System.out.println("Visitantes " + k + ": " + visitantes[k]); | |
| } | |
| } | |
| } | |
| showMenu(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment