001package algs34;
002
003import java.util.Arrays;
004import java.util.Objects;
005import stdlib.*;
006
007public class XStudent {
008        int i;
009        double d;
010        int[] a;
011        Object o;
012
013        public boolean equals(Object y) {
014                //Trace.draw ();
015                if (y == this) return true;
016                if (y == null) return false;
017                if (y.getClass() != this.getClass()) return false;
018                XStudent that = (XStudent) y;
019                if (Integer.compare (this.i, that.i) != 0) return false;
020                if (Double.compare (this.d, that.d) != 0) return false;
021                if (! Arrays.equals (this.a, that.a)) return false;
022                if (! Objects.equals (this.o, that.o)) return false;
023                // don't use ((this.o).equals(that.o)) unless you know that (this.o!=null)
024                return true;
025        }
026        public int hashCode() {
027                int h = 17;
028                h = 31 * h + Integer.hashCode (i);
029                h = 31 * h + Double.hashCode (d);
030                h = 31 * h + Arrays.hashCode (a);
031                h = 31 * h + Objects.hashCode (o);
032                // don't use (o.hashCode()) unless you know that (o!=null)
033                return h;
034        }
035
036
037        public XStudent (double d, int[] a, Object o) {
038                this.d = d;
039                this.a = a;
040                this.o = o;
041        }
042        public static void main (String[] args) {
043                //Trace.run ();
044                Object p = new Object ();
045                XStudent x = new XStudent (0.1, new int[] { 1,2,3 }, p);
046                XStudent y = new XStudent (0.1, new int[] { 1,2,3 }, p);
047                StdOut.format ("(x==y)=%b x.equals(y)=%b\n", x==y, Objects.equals(x,y));
048                StdOut.format ("x=%s Objects.hashCode(x)=%x\n", x, Objects.hashCode (x));
049                StdOut.format ("y=%s Objects.hashCode(y)=%x\n", y, Objects.hashCode (y));
050        }
051}