gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Primeri za hash i equals sa propratnim dokumentom i fajlovima
[spa2-materijali.git] / Hash / primeri / XO.java
diff --git a/Hash/primeri/XO.java b/Hash/primeri/XO.java
new file mode 100644 (file)
index 0000000..f148e8e
--- /dev/null
@@ -0,0 +1,69 @@
+import org.svetovid.io.SvetovidReader;
+
+/**
+ * Igra XO
+ * 
+ * Prosiruje InfoTip samo zbog jednostavnosti u TestHash, nema razloga inace.
+ */
+public class XO extends InfoTip {
+       public static final int DIM = 3;
+       private final int[][] tabla = new int[DIM][DIM];
+
+       public XO() {
+       }
+
+       @Override
+       public InfoTip ucitaj(SvetovidReader r) {
+               XO rez = new XO();
+               for (int i = 0; i < DIM; i++) {
+                       for (int j = 0; j < DIM; j++) {
+                               rez.tabla[i][j] = r.readInt();
+                       }
+               }
+               return rez;
+       }
+
+       public boolean equals(Object o) {
+               // Objekat je identican
+               if (this == o) {
+                       return true;
+               }
+               // Null je uvek razlicit
+               if (o == null) {
+                       return false;
+               }
+               // Ako su klase razlicite, objekti ne mogu bili jednaki
+               if (getClass() != o.getClass()) {
+                       return false;
+               }
+
+               // menjamo tip da mozemo da poredimo
+               XO o2 = (XO) o;
+               // posto je u ovoj klasi uvek inicijalizovano polje table
+               // i uvek je DIM x DIM ne moramo proveravati null
+               for (int i = 0; i < DIM; i++) {
+                       for (int j = 0; j < DIM; j++) {
+                               if (o2.tabla[i][j] != tabla[i][j]) {
+                                       return false;
+                               }
+                       }
+               }
+               return true;
+       }
+
+       @Override
+       public int hashCode() {
+               int rez = 0;
+               for (int i = 0; i < DIM; i++) {
+                       for (int j = 0; j < DIM; j++) {
+                               rez += tabla[i][j];
+                       }
+               }
+               return rez;
+       }
+
+       // pomocni metod za lakse testiranje
+       public static void main(String[] args) {
+               new TestHash(new XO(), "xo").run();
+       }
+}
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner