JHamming - Hamming.java
Cerca
 











Hamming.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6.  
  7. import java.util.ArrayList;
  8.  
  9. /**
  10.  *
  11.  * @author Paolo
  12.  */
  13. public class Hamming {
  14.  
  15.     private String code;
  16.  
  17.     public Hamming(String pCode){
  18.         this.code=pCode;
  19.     }
  20.  
  21.     public boolean isBinaryNum() {
  22.             for (int i = 0; i < this.code.length(); i++) {
  23.                 if (this.code.charAt(i) != '0' && this.code.charAt(i) != '1') {
  24.                     return false;
  25.                 }
  26.             }
  27.             return true;
  28.     }
  29.  
  30.     private String confronta(String old, String nuovo) {
  31.         int oldLeng = old.length();
  32.         int nuovoLeng = nuovo.length();
  33.         if (nuovoLeng < oldLeng) {
  34.             for (int i = 0; i < (oldLeng - nuovoLeng); i++) {
  35.                 nuovo = "0" + nuovo;
  36.             }
  37.         }
  38.         String retval = "";
  39.         for (int i = 0; i < old.length(); i++) {
  40.             if (old.charAt(i) == nuovo.charAt(i)) {
  41.                 retval = retval + "0";
  42.             } else {
  43.                 retval = retval + "1";
  44.             }
  45.         }
  46.         return retval;
  47.     }
  48.  
  49.     public String controlla() {
  50.         ArrayList<Integer> uni = new ArrayList<Integer>();
  51.         for (int i = 0; i < this.code.length(); i++) {
  52.             if (this.code.charAt(i) == '1') {
  53.                 uni.add((this.code.length() - i));
  54.             }
  55.         }
  56.         String old = Integer.toBinaryString(uni.get(0));
  57.         for (int i = 1; i < uni.size(); i++) {
  58.             String nuovo = Integer.toBinaryString(uni.get(i));
  59.             old = confronta(old, nuovo);
  60.         }
  61.         return old;
  62.     }
  63.  
  64.     public void setCode(String pCode) {
  65.         this.code = pCode;
  66.     }
  67.  
  68. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.