gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Hash, primeri se sada sami pokrecu.
[spa2-materijali.git] / Hash / Gadjanje.java
index 364c495..b70b6d0 100644 (file)
@@ -9,22 +9,6 @@ public class Gadjanje extends InfoTip {
        private int[] rezultati;
        private static int MAX_DUZ = 20;
 
-       public Gadjanje(String linija) {
-               String[] delovi = linija.replaceAll("  ", " ").split(" ");
-               if (delovi.length > 0) {
-                       try {
-                               int br = Integer.parseInt(delovi[0]);
-                               rezultati = new int[br];
-                               for (int i = 0; i < br; i++) {
-                                       rezultati[i] = Integer.parseInt(delovi[i + 1]);
-                               }
-                       } catch (Exception ex) {
-                               throw new RuntimeException(ex);
-                       }
-               } else
-                       throw new RuntimeException("Pogresan broj argumenata u redu");
-       }
-
        public Gadjanje() {
                // prazan konstruktor potreban za test program
        }
@@ -48,7 +32,7 @@ public class Gadjanje extends InfoTip {
                if (rezultati != null && o2.rezultati != null) {
                        if (o2.rezultati.length == rezultati.length) {
                                for (int i = 0; i < rezultati.length; i++) {
-                                       if (o2.rezultati[i] != rezultati[i]){
+                                       if (o2.rezultati[i] != rezultati[i]) {
                                                // cim je nesto razlicito nisu isti
                                                return false;
                                        }
@@ -66,8 +50,10 @@ public class Gadjanje extends InfoTip {
        @Override
        public int hashCode() {
                int rez = 0;
-               for (int i = 0; i < rezultati.length; i++) {
-                       rez = (rez + rezultati[i] * (MAX_DUZ + 1 - i)) * 7;
+               if (rezultati != null) {
+                       for (int i = 0; i < rezultati.length; i++) {
+                               rez = (rez + rezultati[i] * (MAX_DUZ + 1 - i)) * 7;
+                       }
                }
                return rez;
        }
@@ -91,4 +77,26 @@ public class Gadjanje extends InfoTip {
                return str;
        }
 
+       // pomocni metod za lakse testiranje
+       public static void main(String[] args) {
+               new TestHash(new Gadjanje(), "mete").run();
+       }
+       
+       // ovaj konstruktor nije neophodan, ostavljen je kao primer
+       public Gadjanje(String linija) {
+               String[] delovi = linija.replaceAll("  ", " ").split(" ");
+               if (delovi.length > 0) {
+                       try {
+                               int br = Integer.parseInt(delovi[0]);
+                               rezultati = new int[br];
+                               for (int i = 0; i < br; i++) {
+                                       rezultati[i] = Integer.parseInt(delovi[i + 1]);
+                               }
+                       } catch (Exception ex) {
+                               throw new RuntimeException(ex);
+                       }
+               } else
+                       throw new RuntimeException("Pogresan broj argumenata u redu");
+       }
+
 }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner