Advertisement
Michik

Redes de Computadores - Trabalho Conversão

Mar 10th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.90 KB | None | 0 0
  1. //5 ADS NOTURNO
  2. //Marcelo Kazuo Ichimura - 0210481513032
  3. //Redes de computadores - atividade de conversão
  4.  
  5. //--------------------------------------------------------------------------------------------------
  6. //Código da tela
  7. public class frmConversao extends javax.swing.JFrame {
  8.     Metodos met = new Metodos();
  9.     TestarExcecao testa = new TestarExcecao();
  10.  
  11.     ...
  12.  
  13.     try {
  14.         //Variáveis
  15.         int primeiro = Integer.parseInt(txtPrimeiro.getText());
  16.         int segundo = Integer.parseInt(txtSegundo.getText());
  17.         int terceiro = Integer.parseInt(txtTerceiro.getText());
  18.         int quarto = Integer.parseInt(txtQuarto.getText());
  19.            
  20.         int masum = Integer.parseInt(txtMasum.getText());
  21.         int masdois = Integer.parseInt(txtMasdois.getText());
  22.         int mastres = Integer.parseInt(txtMastres.getText());
  23.         int masquatro = Integer.parseInt (txtMasquatro.getText());
  24.        
  25.         String enderecoRede=null;
  26.         String convertido=null;
  27.         String enderecoBroadcast = null;
  28.         String hostMin = null;
  29.         String hostMax = null;
  30.         int max;
  31.         int testMax;
  32.      
  33.         //  testa as exceções
  34.         testa.teste(masum);
  35.         testa.teste(masdois);
  36.         testa.teste(mastres);
  37.         testa.teste(masquatro);
  38.            
  39.         testa.teste(primeiro);
  40.         testa.teste(segundo);
  41.         testa.teste(terceiro);
  42.         testa.teste(quarto);
  43.    
  44.         int[] vetIP;
  45.         vetIP = new int[8];
  46.         int[] vetMask;
  47.         vetMask = new int[8];
  48.         int[] vetAND;
  49.         vetAND = new int[8];
  50.         int[] vetOR;
  51.         vetOR = new int[8];
  52.         int[] vetInvertido;
  53.         vetInvertido = new int [8];
  54.            
  55.         //Chama os métodos e faz as conversões e operações necessário dos 4 octetos
  56.         //Conversão do IP para bin
  57.         //Conversão das máscara
  58.         //Inversão da mascara
  59.         //Operação OR, Operação END
  60.         vetIP = met.converterBinario(primeiro);
  61.         vetMask = met.converterBinario(masum);
  62.         vetInvertido = met.inverteMask(vetMask);
  63.         vetOR = met.calculoOR(vetIP, vetInvertido);
  64.         vetAND = met.calculoAND(vetIP, vetMask);
  65.         enderecoRede =""+met.converteDecimal(vetAND)+".";
  66.         enderecoBroadcast =""+met.converteDecimal(vetOR)+".";
  67.  
  68.         vetIP = met.converterBinario(segundo);
  69.         vetMask = met.converterBinario(masdois);
  70.         vetInvertido = met.inverteMask(vetMask);
  71.         vetOR = met.calculoOR(vetIP, vetInvertido);            
  72.         vetAND = met.calculoAND(vetIP, vetMask);
  73.         enderecoRede = enderecoRede+met.converteDecimal(vetAND)+".";
  74.         enderecoBroadcast =enderecoBroadcast+met.converteDecimal(vetOR)+".";
  75.            
  76.         vetIP = met.converterBinario(terceiro);
  77.         vetMask = met.converterBinario(mastres);
  78.         vetInvertido = met.inverteMask(vetMask);
  79.         vetOR = met.calculoOR(vetIP, vetInvertido);            
  80.         vetAND = met.calculoAND(vetIP, vetMask);
  81.         enderecoRede = enderecoRede+met.converteDecimal(vetAND)+".";
  82.         enderecoBroadcast =enderecoBroadcast+met.converteDecimal(vetOR)+".";
  83.  
  84.         vetIP = met.converterBinario(quarto);
  85.         vetMask = met.converterBinario(masquatro);
  86.         vetInvertido = met.inverteMask(vetMask);
  87.         vetOR = met.calculoOR(vetIP, vetInvertido);            
  88.         vetAND = met.calculoAND(vetIP, vetMask);
  89.         testMax = met.converteDecimal(vetMask);
  90.  
  91.         //  Teste para determinar o host máximo e mínimo
  92.         if (testMax != 255){
  93.             max = met.converteDecimal(vetOR)-1;
  94.             hostMax = enderecoBroadcast+max;
  95.             hostMin = enderecoRede+ ((met.converteDecimal(vetAND))+1);
  96.          }
  97.          enderecoRede = enderecoRede+met.converteDecimal(vetAND);
  98.         if (testMax  == 255){
  99.             hostMin = enderecoRede;
  100.             hostMax = enderecoRede;
  101.          }
  102.  
  103.         enderecoBroadcast =enderecoBroadcast+met.converteDecimal(vetOR);
  104.            
  105.  
  106.         //Coloca os valores nas caixas de textos
  107.         txtEndrede.setText(enderecoRede);
  108.         txtEndbroad.setText(enderecoBroadcast);            
  109.         txtHostMin.setText (hostMin);
  110.         txtHostMax.setText (hostMax);
  111.            
  112.  
  113.         //Bloqueia os campos       
  114.         txtPrimeiro.setEnabled(false);
  115.         txtSegundo.setEnabled(false);
  116.         txtTerceiro.setEnabled(false);
  117.         txtQuarto.setEnabled(false);
  118.         txtMasum.setEnabled(false);
  119.         txtMasdois.setEnabled(false);
  120.         txtMastres.setEnabled(false);
  121.         txtMasquatro.setEnabled(false);
  122.            
  123.              
  124.         //Excecoes              
  125.         }
  126.         catch(NumberFormatException e){
  127.             JOptionPane.showMessageDialog(null, "Digite valores entre 0 e 255 em todos os campos!", "Erro", 0);
  128.         }
  129.         catch (ConversaoExcecoes er){
  130.             JOptionPane.showMessageDialog(null, "Digite valores maiores ou igual 0 e menores ou iguais a 255", "Erro", 0);
  131.         }
  132.         catch (Exception ex){
  133.             System.out.println("Erro"+ex);
  134.         }    
  135.     }
  136.  
  137. private void btnLimparMousePressed(java.awt.event.MouseEvent evt) {                                      
  138.             txtPrimeiro.setEnabled(true);
  139.             txtSegundo.setEnabled(true);
  140.             txtTerceiro.setEnabled(true);
  141.             txtQuarto.setEnabled(true);
  142.             txtMasum.setEnabled(true);
  143.             txtMasdois.setEnabled(true);
  144.             txtMastres.setEnabled(true);
  145.             txtMasquatro.setEnabled(true);
  146.            
  147.             txtPrimeiro.setText(null);
  148.             txtSegundo.setText(null);
  149.             txtTerceiro.setText(null);
  150.             txtQuarto.setText(null);
  151.             txtMasum.setText(null);
  152.             txtMasdois.setText(null);
  153.             txtMastres.setText(null);
  154.             txtMasquatro.setText(null);
  155.            
  156.             txtHostMax.setText(null);
  157.             txtHostMin.setText(null);
  158.             txtEndbroad.setText(null);
  159.             txtEndrede.setText(null);
  160.     }          
  161.  
  162.  
  163.  
  164.  
  165. //--------------------------------------------------------------------------------------------------
  166. //CÓDIGO DA CLASSE COM OS  MÉTODOS
  167.  
  168. public class Metodos {
  169.    
  170.     //Recebe um valor e transforma em binário
  171.     public int[] converterBinario(int binario){
  172.         int[] binarioVetor;
  173.         binarioVetor = new int[8];
  174.  
  175.         if (binario >= 128){
  176.                 binario = binario - 128;
  177.                 binarioVetor[0] = 1;
  178.         }
  179.         else
  180.                 binarioVetor[0] = 0;
  181.  
  182.  
  183.         if (binario >= 64){
  184.                 binario = binario - 64;
  185.                 binarioVetor[1] = 1;
  186.         }
  187.         else
  188.                 binarioVetor[1] = 0;
  189.  
  190.        
  191.         if (binario >= 32){
  192.                 binario = binario - 32;
  193.                 binarioVetor[2] = 1;
  194.         }
  195.         else
  196.                 binarioVetor[2] = 0;
  197.  
  198.  
  199.        
  200.         if (binario >= 16){
  201.                 binario = binario - 16;
  202.                 binarioVetor[3] = 1;
  203.         }
  204.         else
  205.                 binarioVetor[3] = 0;
  206.  
  207.  
  208.  
  209.         if (binario >= 8){
  210.                 binario = binario - 8;
  211.                 binarioVetor[4] = 1;
  212.         }
  213.         else
  214.                 binarioVetor[4] = 0;
  215.  
  216.  
  217.  
  218.         if (binario >= 4){
  219.                 binario = binario - 4;
  220.                 binarioVetor[5] = 1;
  221.         }
  222.         else
  223.                 binarioVetor[5] = 0;
  224.  
  225.  
  226.  
  227.         if (binario >= 2){
  228.                 binario = binario - 2;
  229.                 binarioVetor[6] = 1;
  230.         }
  231.         else
  232.                 binarioVetor[6] = 0;
  233.  
  234.  
  235.  
  236.         if (binario >= 1){
  237.                 binario = binario - 1;
  238.                 binarioVetor[7] = 1;
  239.         }
  240.         else
  241.                 binarioVetor[7] = 0;
  242.        
  243.         return binarioVetor;
  244.     }
  245.  
  246.     //recebe um vetor e inverte o valor 0 pra 1 e 1 pra 0
  247.     public int[] inverteMask (int[] mask){
  248.         int[] maskInv;
  249.         maskInv = new int[8];
  250.  
  251.         for (int i = 0; i<=7; i++){
  252.             if (mask[i] == 1)
  253.                 maskInv[i] = 0;
  254.             else
  255.                 maskInv[i] = 1;
  256.         }
  257.             return maskInv;
  258.     }
  259.  
  260.  
  261.     //Recebe dois vetores e faz o and entre eles
  262.     public int[] calculoAND (int[] binario, int[]mask){
  263.         int[] and;
  264.         and = new int[8];
  265.  
  266.         for (int i = 0; i<=7; i++){
  267.             if (binario[i] == 1 && mask[i] == 1)
  268.                 and[i] = 1;
  269.             else
  270.                 and[i] = 0;
  271.             }
  272.        
  273.         return and;
  274.     }
  275.  
  276.  
  277.     //Recebe dois vetores e faz o or entre eles
  278.     public int[] calculoOR (int[] binario, int[] mask){
  279.         int[] or;
  280.         or = new int[8];
  281.  
  282.         for (int i = 0; i<=7; i++){
  283.             if (binario[i] == 1 || mask[i] ==1)
  284.                 or[i] = 1;
  285.             else
  286.                 or[i] = 0;
  287.             }
  288.        
  289.         return or;
  290.     }
  291.  
  292.  
  293.     //Recebe valor em binário de um vetor e transforma em um int decimal
  294.     public int converteDecimal (int[] binario){
  295.         int decimal=0;
  296.    
  297.         if (binario[0] == 1)
  298.             decimal = decimal +128;
  299.  
  300.         if (binario[1] == 1)
  301.             decimal = decimal + 64;
  302.  
  303.         if (binario[2] == 1)
  304.             decimal = decimal + 32;
  305.  
  306.         if (binario[3] == 1)
  307.             decimal = decimal + 16;
  308.        
  309.         if (binario[4] == 1)
  310.             decimal = decimal + 8;
  311.  
  312.         if (binario[5] == 1)
  313.             decimal = decimal + 4;
  314.  
  315.         if (binario[6] == 1)
  316.             decimal = decimal + 2;
  317.  
  318.         if (binario[7] == 1)
  319.             decimal = decimal + 1;
  320.  
  321.         return decimal;
  322.         }
  323.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement