Txerrinko

OpArray

Mar 18th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package Ejercicio3;
  2.  
  3. import java.io.IOException;
  4.  
  5.  
  6. import Ejercicio2.Consola;
  7.  
  8. public class OpArray {
  9.  
  10.     int [] enteros;
  11.    
  12.     int n;
  13.    
  14.     OpArray (int n ){
  15.        
  16.         enteros=new int [n];
  17.     }
  18.    
  19.     void cargarArray () throws IOException{
  20.         int b,num;
  21.        
  22.         System.out.println("Introduce numero:");
  23.         num=Consola.leeInt();
  24.             for( b=0;b< enteros.length && num!=0;b++){
  25.                    
  26.                     enteros[b]=new Integer( num );
  27.                    
  28.                     System.out.println("Introduce numero:");
  29.                     num=Consola.leeInt();  
  30.             }
  31.             n=b;
  32.     }
  33.    
  34.      void ver (){
  35.          
  36.          for (int i=0 ;i<n;i++)
  37.          System.out.println("enteros[i]");
  38.          
  39.      }
  40.      public int sustituir (int n) throws IOException{
  41.        
  42.         int cont=0;
  43.         for (int i=0; i<enteros.length;i++){
  44.            
  45.            
  46.             if (enteros[i]==n)
  47.             System.out.print("Introduce numero");
  48.             enteros[i]=Consola.leeInt();
  49.             cont++;
  50.            
  51.         }        
  52.          return cont;
  53.      }
  54.    
  55.      
  56.      public String toString ()
  57.      {
  58.         String cadena="";
  59.        
  60.         for (int i=0; i<enteros.length;i++){
  61.             cadena=cadena+enteros[i];
  62.            
  63.         }
  64.          return cadena;
  65.          
  66.      }
  67.      public boolean esPrimo (int num){
  68.          int cont=0;
  69.          for (int d=2;d <=n/2;d++){
  70.              if (num%d==0){
  71.                  cont++;
  72.              }
  73.          }
  74.          
  75.          if (cont==0)
  76.              return true;
  77.          else
  78.              return false;
  79.      }
  80.    
  81.      public int  indicePrimo (){
  82.         boolean parar=false;
  83.         int i=0;
  84.    
  85.         while ( i<enteros.length && !parar){
  86.            
  87.             if(esPrimo (enteros[i]) ){
  88.                 parar=true;
  89.             }
  90.         }
  91.         if (parar)
  92.                     return i;
  93.                
  94.                 else
  95.                     return -1;
  96.                
  97.         }
  98.  
  99.        
  100.    
  101.    
  102. public static void main(String[] args) throws IOException {
  103.     // TODO Auto-generated method stub
  104.  
  105.     OpArray elem= new OpArray(21);
  106.      elem.cargarArray();
  107.  
  108.     elem.ver();
  109.     elem.sustituir(20);
  110.     elem.toString();
  111.     elem.indicePrimo();
  112.    
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment