Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //5 ADS NOTURNO
- //Marcelo Kazuo Ichimura - 0210481513032
- //Redes de computadores - atividade de conversão
- //--------------------------------------------------------------------------------------------------
- //Código da tela
- public class frmConversao extends javax.swing.JFrame {
- Metodos met = new Metodos();
- TestarExcecao testa = new TestarExcecao();
- ...
- try {
- //Variáveis
- int primeiro = Integer.parseInt(txtPrimeiro.getText());
- int segundo = Integer.parseInt(txtSegundo.getText());
- int terceiro = Integer.parseInt(txtTerceiro.getText());
- int quarto = Integer.parseInt(txtQuarto.getText());
- int masum = Integer.parseInt(txtMasum.getText());
- int masdois = Integer.parseInt(txtMasdois.getText());
- int mastres = Integer.parseInt(txtMastres.getText());
- int masquatro = Integer.parseInt (txtMasquatro.getText());
- String enderecoRede=null;
- String convertido=null;
- String enderecoBroadcast = null;
- String hostMin = null;
- String hostMax = null;
- int max;
- int testMax;
- // testa as exceções
- testa.teste(masum);
- testa.teste(masdois);
- testa.teste(mastres);
- testa.teste(masquatro);
- testa.teste(primeiro);
- testa.teste(segundo);
- testa.teste(terceiro);
- testa.teste(quarto);
- int[] vetIP;
- vetIP = new int[8];
- int[] vetMask;
- vetMask = new int[8];
- int[] vetAND;
- vetAND = new int[8];
- int[] vetOR;
- vetOR = new int[8];
- int[] vetInvertido;
- vetInvertido = new int [8];
- //Chama os métodos e faz as conversões e operações necessário dos 4 octetos
- //Conversão do IP para bin
- //Conversão das máscara
- //Inversão da mascara
- //Operação OR, Operação END
- vetIP = met.converterBinario(primeiro);
- vetMask = met.converterBinario(masum);
- vetInvertido = met.inverteMask(vetMask);
- vetOR = met.calculoOR(vetIP, vetInvertido);
- vetAND = met.calculoAND(vetIP, vetMask);
- enderecoRede =""+met.converteDecimal(vetAND)+".";
- enderecoBroadcast =""+met.converteDecimal(vetOR)+".";
- vetIP = met.converterBinario(segundo);
- vetMask = met.converterBinario(masdois);
- vetInvertido = met.inverteMask(vetMask);
- vetOR = met.calculoOR(vetIP, vetInvertido);
- vetAND = met.calculoAND(vetIP, vetMask);
- enderecoRede = enderecoRede+met.converteDecimal(vetAND)+".";
- enderecoBroadcast =enderecoBroadcast+met.converteDecimal(vetOR)+".";
- vetIP = met.converterBinario(terceiro);
- vetMask = met.converterBinario(mastres);
- vetInvertido = met.inverteMask(vetMask);
- vetOR = met.calculoOR(vetIP, vetInvertido);
- vetAND = met.calculoAND(vetIP, vetMask);
- enderecoRede = enderecoRede+met.converteDecimal(vetAND)+".";
- enderecoBroadcast =enderecoBroadcast+met.converteDecimal(vetOR)+".";
- vetIP = met.converterBinario(quarto);
- vetMask = met.converterBinario(masquatro);
- vetInvertido = met.inverteMask(vetMask);
- vetOR = met.calculoOR(vetIP, vetInvertido);
- vetAND = met.calculoAND(vetIP, vetMask);
- testMax = met.converteDecimal(vetMask);
- // Teste para determinar o host máximo e mínimo
- if (testMax != 255){
- max = met.converteDecimal(vetOR)-1;
- hostMax = enderecoBroadcast+max;
- hostMin = enderecoRede+ ((met.converteDecimal(vetAND))+1);
- }
- enderecoRede = enderecoRede+met.converteDecimal(vetAND);
- if (testMax == 255){
- hostMin = enderecoRede;
- hostMax = enderecoRede;
- }
- enderecoBroadcast =enderecoBroadcast+met.converteDecimal(vetOR);
- //Coloca os valores nas caixas de textos
- txtEndrede.setText(enderecoRede);
- txtEndbroad.setText(enderecoBroadcast);
- txtHostMin.setText (hostMin);
- txtHostMax.setText (hostMax);
- //Bloqueia os campos
- txtPrimeiro.setEnabled(false);
- txtSegundo.setEnabled(false);
- txtTerceiro.setEnabled(false);
- txtQuarto.setEnabled(false);
- txtMasum.setEnabled(false);
- txtMasdois.setEnabled(false);
- txtMastres.setEnabled(false);
- txtMasquatro.setEnabled(false);
- //Excecoes
- }
- catch(NumberFormatException e){
- JOptionPane.showMessageDialog(null, "Digite valores entre 0 e 255 em todos os campos!", "Erro", 0);
- }
- catch (ConversaoExcecoes er){
- JOptionPane.showMessageDialog(null, "Digite valores maiores ou igual 0 e menores ou iguais a 255", "Erro", 0);
- }
- catch (Exception ex){
- System.out.println("Erro"+ex);
- }
- }
- private void btnLimparMousePressed(java.awt.event.MouseEvent evt) {
- txtPrimeiro.setEnabled(true);
- txtSegundo.setEnabled(true);
- txtTerceiro.setEnabled(true);
- txtQuarto.setEnabled(true);
- txtMasum.setEnabled(true);
- txtMasdois.setEnabled(true);
- txtMastres.setEnabled(true);
- txtMasquatro.setEnabled(true);
- txtPrimeiro.setText(null);
- txtSegundo.setText(null);
- txtTerceiro.setText(null);
- txtQuarto.setText(null);
- txtMasum.setText(null);
- txtMasdois.setText(null);
- txtMastres.setText(null);
- txtMasquatro.setText(null);
- txtHostMax.setText(null);
- txtHostMin.setText(null);
- txtEndbroad.setText(null);
- txtEndrede.setText(null);
- }
- //--------------------------------------------------------------------------------------------------
- //CÓDIGO DA CLASSE COM OS MÉTODOS
- public class Metodos {
- //Recebe um valor e transforma em binário
- public int[] converterBinario(int binario){
- int[] binarioVetor;
- binarioVetor = new int[8];
- if (binario >= 128){
- binario = binario - 128;
- binarioVetor[0] = 1;
- }
- else
- binarioVetor[0] = 0;
- if (binario >= 64){
- binario = binario - 64;
- binarioVetor[1] = 1;
- }
- else
- binarioVetor[1] = 0;
- if (binario >= 32){
- binario = binario - 32;
- binarioVetor[2] = 1;
- }
- else
- binarioVetor[2] = 0;
- if (binario >= 16){
- binario = binario - 16;
- binarioVetor[3] = 1;
- }
- else
- binarioVetor[3] = 0;
- if (binario >= 8){
- binario = binario - 8;
- binarioVetor[4] = 1;
- }
- else
- binarioVetor[4] = 0;
- if (binario >= 4){
- binario = binario - 4;
- binarioVetor[5] = 1;
- }
- else
- binarioVetor[5] = 0;
- if (binario >= 2){
- binario = binario - 2;
- binarioVetor[6] = 1;
- }
- else
- binarioVetor[6] = 0;
- if (binario >= 1){
- binario = binario - 1;
- binarioVetor[7] = 1;
- }
- else
- binarioVetor[7] = 0;
- return binarioVetor;
- }
- //recebe um vetor e inverte o valor 0 pra 1 e 1 pra 0
- public int[] inverteMask (int[] mask){
- int[] maskInv;
- maskInv = new int[8];
- for (int i = 0; i<=7; i++){
- if (mask[i] == 1)
- maskInv[i] = 0;
- else
- maskInv[i] = 1;
- }
- return maskInv;
- }
- //Recebe dois vetores e faz o and entre eles
- public int[] calculoAND (int[] binario, int[]mask){
- int[] and;
- and = new int[8];
- for (int i = 0; i<=7; i++){
- if (binario[i] == 1 && mask[i] == 1)
- and[i] = 1;
- else
- and[i] = 0;
- }
- return and;
- }
- //Recebe dois vetores e faz o or entre eles
- public int[] calculoOR (int[] binario, int[] mask){
- int[] or;
- or = new int[8];
- for (int i = 0; i<=7; i++){
- if (binario[i] == 1 || mask[i] ==1)
- or[i] = 1;
- else
- or[i] = 0;
- }
- return or;
- }
- //Recebe valor em binário de um vetor e transforma em um int decimal
- public int converteDecimal (int[] binario){
- int decimal=0;
- if (binario[0] == 1)
- decimal = decimal +128;
- if (binario[1] == 1)
- decimal = decimal + 64;
- if (binario[2] == 1)
- decimal = decimal + 32;
- if (binario[3] == 1)
- decimal = decimal + 16;
- if (binario[4] == 1)
- decimal = decimal + 8;
- if (binario[5] == 1)
- decimal = decimal + 4;
- if (binario[6] == 1)
- decimal = decimal + 2;
- if (binario[7] == 1)
- decimal = decimal + 1;
- return decimal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement