LUNDS TEKNISKA HÖGSKOLA 1(3) Institutionen för datavetenskap Tentamen, EDAA01 Programmeringsteknik fördjupningskurs 2017 08 14, 8.00 13.00 Anvisningar: Denna tentamen består av 5 uppgifter. Preliminärt ger uppgifterna 1. 1 + 1.5 + 1.5 = 4 p 2. 2 + 1 + 1 + 2 + 2 = 8 p 3. 9 p 4. 6 + 3 = 9 p 5. 6 p Tillåtet hjälpmedel: Java snabbreferens. För full poäng ska dina lösningar inte vara onödigt ineffektiva. I slutet av tentamen finns dokumentation av någon/några klasser och interface i Java Collections Framework. När rättningen är klar meddelas detta på kursens hemsida (cs.lth.se/edaa01). 1. a) Definiera begreppet algoritm. b) Definiera begreppet datastruktur. Ge exempel på någon datastruktur som behandlats i kursen. c) Definiera begreppet abstrakt datatyp. Ge exempel på någon abstrakt datatyp som behandlats i kursen. 2. I fig. 1 visas en (förenklad) bild över de klasser och interface i Java Collection Framework som har med mängder (eng. set) att göra. Klassen HashSet är implementerad med hjälp av en öppen hashtabell och klassen TreeSet med hjälp av ett balanserat binärt sökträd. Collection Set betyder ärver från ("extends") betyder implementerar ("implements") SortedSet HashSet TreeSet Figur 1: a) Beskriv en likhet och en skillnad mellan interface och abstrakta klasser. b) Vad innebär arv i samband med interface? Dvs. vad innebär det t.ex. att SortedSet ärver Set. c) Vi ser i fig. 1 att TreeSet implementerar SortedSet. Varför implementerar inte också HashSet interfacet SortedSet. Motivera ditt svar.
2(3) d) I både HashSet och TreeSet finns flera konstruktorer. I HashSet finns bl.a. konstruktorn /** Constructs a new set containing the elements in the specified collection. */ HashSet(Collection<? extends E> c); Motsvarande konstruktor finns i klassen TreeSet: /** Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements. */ TreeSet(Collection<? extends E> c); Vad har respektive konstruktor för tidskomplexitet i medelfall. Motivera dina svar. e) Beskriv vad som menas med en öppen hashtabell (eng. separate chaining). I din beskrivning ska det ingå en skiss med ett exempel som visar hur elementen lagras i hashtabellen. 3. Ett polynom kan betraktas som följd av par där varje par består av en exponent och dess tillhörande koefficient. T.ex. representerar paren (0,3), (3,-2), (5,4) polynomet 3 2x 3 + 4x 5. Vi inskränker oss här till polynom med heltalskoefficienter. Skriv en klass LinkedPolynomial som använder en enkellänkad lista för att hantera polynom enlig ovan. Endast de exponenter vars koefficient är skild från 0 ska finnas med i listan. En exponent får bara förekomma högst en gång i listan. Din klass ska innehålla följande statiskt nästlade klass som beskriver de noder listan ska byggas upp av: private static class Node { private int exp; private int coeff; private Node next; private Node(int exp, int coeff) { this.exp = exp; this.coeff = coeff; next = null; Klassen ska innehålla följande två metoder: /** Returnera koefficienten för exponenten exp. Om denna exponent inte finns i polynomet ska 0 returneras. Om exp är negativ ska IllegalArgumentException genereras.*/ public int getcoefficient(int exp); /** Addera termen coeff upphöjt till exp till polynomet. Om exp är negativ ska IllegalArgumentException genereras. */ public void addterm(int exp, int coeff); Ledning: Tänk på att resultatet kan bli 0 när två koefficienter adderas. Bara de exponenter vars koefficient är skild från 0 ska finnas med i listan.
3(3) 4. a) Skriv en klass MapPolynomial som löser uppgiften i förra uppgiften, men byt ut den enkellänkade listan mot en map där nycklarna är exponenter och värdena deras respektive koefficienter. Endast de exponenter vars koefficient är skild från 0 ska finnas med i mappen. Använd någon klass som implementerar java.util.map. Se dokumentation sist i tentan. b) Lägg till följande metod i klassen MapPolynomial. /** Returnera ett nytt polynom som innehåller resultatet då man deriverar polynomet. Det ursprungliga polynomet ska vara oförändrat. */ public MapPolynomial derivative(); Ledning: Derivatan av x k är kx k 1 för k > 0. Exempel: Derivatan för 3 2x 3 + 4x 5 är 6x 2 + 20x 4 5. Ett binärt träd representeras av följande klasser: public class BinaryTree<E> { private Node<E> root; public BinaryTree() { root = null; private static class Node<E> { private E data; private Node<E> left; private Node<E> right; private Node(E data) { this.data = data; left = right = null; Implementera följande metod i klassen BinaryTree: public boolean isequal(binarytree<e> other); Metoden ska undersöka om trädet har samma form och samma innehåll som ett annat träd, other. Implementeringen ska vara rekursiv och du får gärna införa en hjälpmetod (som då också måste implementeras). Du får dock inte införa några ytterligare attribut i klassen.
java.lang Interface Iterable<T> Type Parameters: T - the type of elements returned by the iterator Iterator<T> iterator() Returns an iterator over a set of elements of type T. java.util Interface Iterator<E> Type Parameters: E - the type of elements returned by this iterator Methods boolean hasnext() Returns true if the iteration has more elements. E next() Returns the next element in the iteration. void remove() Removes from the underlying collection the last element returned by this iterator (optional operation). java.lang Interface Comparable<T> Type Parameters: T - the type of objects that this object may be compared to int compareto(t o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. java.lang Interface Comparator<T> Type Parameters: T - the type of objects that may be compared by this comparator int compare(t o1, T o2) Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
java.util!interface Map<K,V> Type Parameters: K - the type of keys maintained by this map V - the type of mapped values All Known Implementing Classes: AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. Method Summary void clear() Removes all of the mappings from this map (optional operation). boolean containskey(object key) Returns true if this map contains a mapping for the specified key. boolean containsvalue(object value) Returns true if this map maps one or more keys to the specified value. Set<Map.Entry<K,V>> entryset() Returns a Set view of the mappings contained in this map. boolean equals(object o) Compares the specified object with this map for equality. V get(object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. int hashcode() Returns the hash code value for this map. boolean isempty() Returns true if this map contains no key-value mappings. Set<K> keyset() Returns a Set view of the keys contained in this map. V put(k key, V value) Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. void putall(map<? extends K,? extends V> m) Copies all of the mappings from the specified map to this map (optional operation). V remove(object key) Removes the mapping for a key from this map if it is present (optional operation). Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. int size() Returns the number of key-value mappings in this map. Collection<V> values() Returns a Collection view of the values contained in this map. java.util Class HashMap<K,V> Hash table based implementation of the Map interface. Constructor Summary HashMap() Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75). HashMap(int initialcapacity) Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75). HashMap(int initialcapacity, float loadfactor) Constructs an empty HashMap with the specified initial capacity and load factor. HashMap(Map<? extends K,? extends V> m) Constructs a new HashMap with the same mappings as the specified Map. java.util Class TreeMap<K,V> A Red-Black tree. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Constructor Summary TreeMap() Constructs a new, empty tree map, using the natural ordering of its keys. TreeMap(Comparator<? super K> comparator) Constructs a new, empty tree map, ordered according to the given comparator. TreeMap(Map<? extends K,? extends V> m) Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys. TreeMap(SortedMap<K,? extends V> m) Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map. java.util!interface Map.Entry<K,V> A map entry (key-value pair). Method Summary boolean equals(object o) Compares the specified object with this entry for equality. K getkey() Returns the key corresponding to this entry. V getvalue() Returns the value corresponding to this entry. int hashcode() Returns the hash code value for this map entry.