Txerrinko

NumCapic

Mar 18th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. package packProyecto04;
  2.  
  3. import java.io.IOException;
  4.  
  5. public class NumCapic {
  6.  
  7.     private long num;
  8.    
  9.     private boolean esCapicua;
  10.    
  11.     public NumCapic()
  12.     {
  13.         num = 0;
  14.     }
  15.    
  16.     public NumCapic( int pNum )
  17.     {
  18.         num = pNum;
  19.        
  20.         esCapicua = capicua();
  21.     }
  22.    
  23.     public int contDigitos()
  24.     {
  25.         long nuevo = num;
  26.        
  27.         int cont = 1;
  28.        
  29.         while ( nuevo >= 10 )
  30.         {
  31.             nuevo = nuevo / 10;
  32.             cont++;
  33.         }  
  34.        
  35.         return cont;
  36.     }
  37.        
  38.     public long digitoEnPosicionN( int pPosicion )
  39.     {
  40.         long nuevo = num;
  41.        
  42.         long resto = 0;
  43.        
  44.         for ( int i = 1 ; i <= contDigitos() - pPosicion + 1 ; i++ )
  45.         {
  46.             resto = nuevo % 10;
  47.             nuevo = nuevo / 10;
  48.         }
  49.        
  50.         return resto;
  51.     }      
  52.    
  53.     public boolean capicua()
  54.     {
  55.         int cont = 0;
  56.        
  57.         for ( int p = 1 ; p <= contDigitos() / 2 ; p++ )
  58.         {
  59.             if ( digitoEnPosicionN(p) == digitoEnPosicionN(contDigitos() - p + 1 ) )
  60.             {
  61.                 cont++;
  62.             }
  63.         }
  64.        
  65.         return cont == contDigitos() / 2;
  66.     }
  67.    
  68.     public boolean capicuaR()
  69.     {
  70.         long inv = num;
  71.        
  72.         long resto;
  73.        
  74.         long nuevo = 0;
  75.        
  76.         while ( inv >= 10 )
  77.         {
  78.             resto = inv % 10;
  79.            
  80.             inv = inv / 10;
  81.            
  82.             nuevo = (nuevo + resto) * 10;
  83.         }  
  84.        
  85.         nuevo = nuevo + inv;
  86.        
  87.         if ( nuevo == num )
  88.         {
  89.             return true;
  90.         }
  91.         else
  92.         {
  93.             return false;
  94.         }      
  95.     }  
  96.    
  97.     public static void main ( String args[] ) throws IOException
  98.     {  
  99.         System.out.println("Teclea numero: ");
  100.        
  101.         int x = Consola.leeInt();
  102.                
  103.         NumCapic nc1 = new NumCapic( x );
  104.        
  105.         if( nc1.capicua() )
  106.            
  107.             System.out.println("es capicua");
  108.        
  109.         else
  110.        
  111.             System.out.println("no es capicua");
  112.    
  113.         //******************************************************
  114.        
  115.         System.out.println("Teclea numero: ");
  116.        
  117.         x = Consola.leeInt();
  118.                
  119.         NumCapic nc2 = new NumCapic( x );
  120.        
  121.         if( nc2.capicua() )
  122.            
  123.             System.out.println("es capicua");
  124.        
  125.         else
  126.        
  127.             System.out.println("no es capicua");
  128.    
  129.         //******************************************************
  130.        
  131.         System.out.println("Teclea numero: ");
  132.        
  133.         x = Consola.leeInt();
  134.                
  135.         NumCapic nc3 = new NumCapic( x );
  136.        
  137.         if( nc3.capicua() )
  138.            
  139.             System.out.println("es capicua");
  140.        
  141.         else
  142.        
  143.             System.out.println("no es capicua");
  144.    
  145.         //******************************************************
  146.                
  147.         System.out.println("Teclea numero: ");
  148.        
  149.         x = Consola.leeInt();
  150.                
  151.         NumCapic nc4 = new NumCapic( x );
  152.        
  153.         if( nc4.capicua() )
  154.            
  155.             System.out.println("es capicua");
  156.        
  157.         else
  158.        
  159.             System.out.println("no es capicua");
  160.    
  161.         //******************************************************
  162.        
  163.         System.out.println("Teclea numero: ");
  164.        
  165.         x = Consola.leeInt();
  166.                
  167.         NumCapic nc5 = new NumCapic( x );
  168.        
  169.         if( nc5.capicua() )
  170.            
  171.             System.out.println("es capicua");
  172.        
  173.         else
  174.        
  175.             System.out.println("no es capicua");
  176.     }  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment