/* * * Lösningsförslag tentamen DIT950 * Datum 130828 * */ /* * -1- */ a) För samtliga gäller: Se anteckningar b) c) d) /* * - 2 - */ (Inget UML diagram här kontakta Jvh om du vill ha en) a) Ok, "B doIt" b) Runtime, can't cast between branches c) Compile, C abstract d) Runtime, can't cast between branches e) Ok, "D doOther" f) Runtime, will get anonymous subclass to IX. Can't cast between branches /* * -3 - */ public final class Planet { private final double mass; private final String name; private final Date datOfDiscovery; public Planet(double mass, String name, Date datOfDiscovery) { super(); this.mass = mass; this.name = name; this.datOfDiscovery = (Date) datOfDiscovery.clone(); // def. copy } public double getMass() { return mass; } public String getName() { return name; } public Date getDatOfDiscovery() { return (Date) datOfDiscovery.clone(); // def. copy } } /* * - 4 - */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) // Same type equals, if mixed type use instanceof return false; final A other = (A) obj; if (id != other.id) return false; return true; } /* * - 5- */ // Not tail recursive private int sum(Node n) { if (n == null) { return 0; } else { return n.getValue() + sum(n.getLeft()) + sum(n.getRight()); } } // Tail recursive ValueHolder v = new ValueHolder(); private void sum2(Node n, ValueHolder v ) { if (n == null) { return; } else { v.incI(n.getValue()); sum2(n.getLeft(), v); sum2(n.getRight(),v); } } /* * -6 - */ a) Prints 5, 7 b) Prints 5, 12 The fundamental fact to understand it that the type we stared with never change (sub). Wherever we are we have always the type sub. So "this" is a sub. Calling m() is same as calling this.m()! I.e. method in sub called. /* * - 7 - */ See slides, State /* * - 8 - */ public final class Freezable { private String s; private Object o; // Assume must have public Freezable() { } public String getS() { // Possible exception if null return s; } public Object getO() { // Possible exception if null return o; } // Inner creator class public static class Creator { private Freezable f = new Freezable(); public Creator setS(String s) { // No set method on f but we're inside Freezable f.s = s; return this; } public Creator setO(Object o) { f.o = o; return this; } public Freezable freeze() { if( this.f == null){ throw new IllegalStateException("Object already frozen"); } if (f.o == null || f.s == null) { throw new IllegalArgumentException("Must initialize"); } Freezable f = this.f; this.f = null; return f; } } } /* * - 9 - */ List list1 = new ArrayList(); // Ok ? supertype to all types Integer i = list1.get(0); // No, elements of unknown type not integer Object o = list1.get(0); // Ok, any type also "the unknown" can be treated as Object list1.add(99); // No, list elements unknown can't add integer (could be String) list1.add(o); // Same as above List list2 = new ArrayList(); // Ok, Number is sub type to itself o = list2.get(0); // Ok, Object goes for anything Number n = list2.get(0); // Ok, elements are Numbers (any subtype of) i = list2.get(0); // No, list could be double or complex or ... list2.add(99); // Same as above List list3 = new ArrayList(); // Ok,Number super type of integer i = list3.get(0); // No, could be a list Object o = list3.get(0); // Ok, Object goes for anything list3.add(55); // Ok integer allowed in any super type of integer list3.add(55.0); // No, list possible integer