Advertisement
Guest User

Teste Aprog

a guest
Nov 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package testeaprog;
  7.  
  8. /**
  9.  *
  10.  * @author Ruben
  11.  */
  12. public class TesteAprog {
  13.  
  14.     /**
  15.      * @param args the command line arguments
  16.      */
  17.     public static void main(String[] args) {
  18.        // Ja implementado
  19.     }
  20.    
  21.     public static boolean isPrimo (int numero){
  22.         /**
  23.          * JA IMPLEMENTADO
  24.          * retorno utopico
  25.          * teve que ser declarado para o metodo nao dar erro
  26.          */
  27.         return true;
  28.     }
  29.    
  30.     // Alinea a)
  31.    
  32.     public static int pesquisarPrimos(int [] vetor, int nElems, int [] vetorPrimos){
  33.         int nPrimos = 0;
  34.         for (int i = 0; i < nElems; i++){
  35.             /**
  36.              * Chama o metodo isPrimo para cada numero do vetor
  37.              */
  38.             if(isPrimo(vetor[i])){
  39.                 vetorPrimos[nPrimos] = vetor[i];
  40.                 nPrimos++;
  41.             }
  42.         }
  43.         return nPrimos;
  44.     }
  45.    
  46.     // Alinea B
  47.    
  48.     public static int alterarValor(int [] vetor, int nElems){
  49.         /**
  50.          * Temos que definir um numero como menor para se comparar.
  51.          * Definimos então o primeiro.
  52.          */
  53.         int menor = vetor[0];
  54.         for (int i = 1; i < nElems; i++){
  55.             if (vetor[i] < menor){
  56.                 menor = vetor[i];
  57.             }
  58.         }
  59.         /**
  60.          * Altera o valor.
  61.          * Para isso utilizamos um artificio de obter o tamanho do numero
  62.          * convertendo este para String para utilizarmos o metodo .length()
  63.          */
  64.         int alteracoes = 0;
  65.         for (int i = 0; i < nElems; i++){
  66.            if(Integer.toString(vetor[i]).length() > 3){
  67.                vetor[i] += menor;
  68.                alteracoes++;
  69.            }
  70.         }
  71.         return alteracoes;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement