49
| | Pascal Schärli [email protected] 12.04.2018 Informatik II – Übung 6

Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

||

Pascal Schärli

[email protected]

12.04.2018

Informatik II – Übung 6

Page 2: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 2

Was gibts heute?

Warm-Up

Nachbesprechung Serie 5

Best-Of Vorlesung:

– Klassenhierarichie

– Abstrakte Klassen

– Factories

Vorbesprechung Serie 6

Page 3: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

Warm - Up

Page 4: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 4

Warm – UpBasisprüfung 2012

int a = 0; for(int i = 0; ……………………… ; i++) {

for(int j = i/100; j > 0; j--) {a++;

} }

a) O(n4)

int a = 0; for(int i = 0; ………………………………. ; i++) {

for(int j = i/100; j > 0; j--) {a++;

} }

b) O(log n)i<n2 i<

i<n*n 1<<(i*i)<n

Wie muss man bei den folgenden Codefragmenten die Lücke füllen, um die gewünschte Laufzeit zu erreichen?

Page 5: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 5

Warm – Up

int a = 0; for(int i = 0; i < N ; i++) {

for(int j = i/100; j > 0; j--) {a++;

} }

int a = 0; for(int i = 0; i < N ; i++) {

for(int j = i; j > 0; j--) {a++;

} }

Hat die selbe Komplexität

→ O(N2)

a)O(N2) = O(n4)

→N = n2

a)O(N2) = O(log(n))

→N =

Page 6: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 6

Warm – Up

Wie kommt man von i< zu 1<<(i*i)<n ?

Bitshift: a<<b – die Zahl a wird als binärzahl um b Bytes nach links geschoben

5<<2 = – 0b101<<2 = 0b10100 = 20

a<<b = a*2b

Es gibt auch a>>b, dann würde a um b Bytes nach rechts geschoben werden.

i <

– i*i<

– 2i*i<n

– 1<<(i*i) < n

Page 7: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

Nachbesprechung

Page 8: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 8

Nachbesprechung U5 – Übersichtlichkeit

if(i>0){return true;

else{return false;

}

return i>0;

List newList = new List(value,list); return newList;

return new List(value,list);

if(list == null){return new List(value, null);

else{return new List(value, list);

}

return new List(value, list);

Page 9: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 9

Nachbesprechung U5 – Übersichtlichkeit

Ctrl + Shift + F-danke :)

Nachbesprechung U5 – Übersichtlichkeit

Page 10: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 10

Nachbesprechung U5A1 public static List add(List list, int value) {

return new List(value, list); }

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

Page 11: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 11

Nachbesprechung U5A1 public static int size(List list) {

if (list == null) return 0;return size(list.next) + 1;

}

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

1+size(list.next);

1+0=11+1=21+2=31+3=4

1+size(list.next); 1+size(list.next); 1+size(list.next);

Page 12: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 12

Nachbesprechung U5A1 public static int sum(List list) {

if (list == null) return 0;return list.value + sum(list.next);

}

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

1+sum(list.next);

8+0=821+8=294+29=331+33=34

4+sum(list.next); 21+sum(list.next); 8+sum(list.next);

Page 13: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 13

Nachbesprechung U5A1 public static List last(List list) {

if (list == null) return null;if (list.next == null) return list;return last(list.next);

}

last(list.next);

[List D]

last(list.next); last(list.next); Return list;

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

[List A] [List B] [List C] [List D]

[List D][List D][List D]

Page 14: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 14

Nachbesprechung U5A1 public static List sublist(List list, int index) throws IndexOutOfBoundsException {

if (list == null || index < 0) throw new IndexOutOfBoundsException();if (index == 0) return list;return sublist(list.next, index-1);

}

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

sublist(list.next,1); sublist(list.next,0); return list;

[List A] [List B] [List C] [List D]

[List C][List C][List C]

Beispiel:sublist(list,2);

Page 15: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 15

Nachbesprechung U5A1 public static int valueAt(List list, int index) throws IndexOutOfBoundsException {

if (list == null || index < 0) throw new IndexOutOfBoundsException();if (index == 0) return list.value;return valueAt(list.next, index-1);

}

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

valueAt(list.next,1);

212121

valueAt(list.next,0); return 21

Beispiel:valueAt(list,2);

Page 16: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 16

Nachbesprechung U5A1 public static int index(List list, int value) throws NoSuchElementException {

if (list == null) throw new NoSuchElementException();if (list.value == value) return 0;return 1 + index(list.next, value);

}

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

1+index(list.next,21);

01+0=11+1=2

1+index(list.next,21); return 0

Beispiel:index(list,21);

Page 17: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 17

Nachbesprechung U5A2 public static void append(List list, int value) throws IllegalArgumentException {

if (list == null) throw new IllegalArgumentException();u5a1.Lists.last(list).next = new List(value, null);

}

val = 1

next =

val = 1

next =

val = 4

next =

val = 21

next = new List(1,null)

Page 18: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 18

Nachbesprechung U5A2 public static void concat(List head, List tail) throws IllegalArgumentException {

if (head == null) throw new IllegalArgumentException();u5a1.Lists.last(head).next = tail;

}

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

Page 19: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 19

Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws IndexOutOfBoundsException {

if (list == null || index < 0) throw new IndexOutOfBoundsException();if (index == 0) {

list.next = new List(value, list.next);} else {

insertAt(list.next, index-1, value);}

}

val = 1

next =

val = 4

next =

val = 8

next = null

val = 21

next =

Beispiel:insertAt(list,1);

insertAt(list.next,0); index == 0

Page 20: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 20

Nachbesprechung U5A2 public static void insertAt(List list, int index, List newList) throws IndexOutOfBoundsException {

if (newList == null) return;if (list == null || index < 0) throw new IndexOutOfBoundsException();if (index == 0) {

u5a1.Lists.last(newList).next = list.next;list.next = newList;

} else {insertAt(list.next, index-1, newList);

} }

Beispiel:list1 →1,8list2 →4,21insertAt(list1,0,list2);

val = 1

next =

val = 4

next =

val = 8

next = null

val = 21

next =

Page 21: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 21

Nachbesprechung U5A2 public static List remove(List list, int index) throws IndexOutOfBoundsException {

if (list == null || index < 0) throw new IndexOutOfBoundsException();if (index == 0) return list.next;list.next = remove(list.next, index-1);return list;

}

val = 1

next =

val = 4

next =

val = 21

next =

val = 8

next = null

list.next=remove(list.next,1);

Beispiel:remove(list,2);

[List A] [List B] [List C] [List D]

return [List D]return [List B]return [List A]

list.next=remove(list.next,0); index == 0;

list.next= [List D]list.next= [List B]

Page 22: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 22

val = 8

next =

[List B2]

val = 1

next =

val = 4

next =

[List A] [List B]

Nachbesprechung U5A3 public static List insertSorted(List list, int value)

{if (list == null) return new List(value, null);if (value < list.value) return new List(value, list);list.next = insertSorted(list.next, value);return list;

}

Beispiel:insertSorted(list,8);

list.next=insertSorted(list.next,8);

return new List(value,list)[List B2]

return [List B]return [List A]

list.next=insertSorted(list.next,8); 8 < 21

list.next= [List B2] list.next= [List B]

val = 21

next = null

[List C]

Page 23: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 23

Nachbesprechung U5A3 public static List sort(List list) {

if (list == null) return null;return insertSorted(sort(list.next), list.value);

}

Page 24: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

Vorlesung

Page 25: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 25

Klassen

public class myClass {

int myInt;

protected static int myStaticInt;public int myPublicInt;private int myPrivateInt;

public void myPublicFunction(int a) {myPrivateFunction(a);

}

protected static void myStaticFunction(int a) {myStaticInt = a;

}

private void myPrivateFunction(int a) {myPrivateInt = myStaticInt+a;

}

}

static

– Klassenspezifisch → für jedes Objekt der Klasse myClass den gleichen Wert

private

– Nur sichtbar in dieser Klasse

public

– Für alle sichtbar

protected

– Nur sichtbar in dieser Klasse und allen Unterklassen

package-private (default)

– Nur sichtbar für das selbe Paket

Page 26: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 26

Klassenhierarchie

public class Auto extends Fahrzeug{ … }

public class Lastwagen extends Auto{ … }

Syntax:

Vererbung dient der Gliederung und Standartisierung der Objekte

(so könnte z.B. in Fahrzeug definiert werden, dass jede Erb-Klasse das Attribut Geschwindigkeit hat)

Page 27: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 27

Klassenhierarchie - Beispiel public class Fahrzeug{

int radzahl;

public Fahrzeug(int radzahl){this.radzahl = radzahl;

} }

public class Auto extends Fahrzeug{ protected float hubraum;

public Auto(float hubraum){super(4);this.hubraum = hubraum;

}

public float getHubraum(){return hubraum;

}

public void setHubraum(float hubraum){if(hubraum>0) this.hubraum = hubraum;

} }

public class Lastwagen extends Auto{ float capacity;

public Lastwagen(float hubraum,float capacity){super(hubraum);this.capacity = capacity;

} }

Page 28: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 28

Klassenhierarchie - Beispiel Fahrzeug myFahrrad = new Fahrzeug(2); Fahrzeug myAuto = new Auto(1400); Fahrzeug myLastwagen = new Lastwagen(80000,50);

System.out.println(myFahrrad.radzahl); System.out.println(myAuto.radzahl); System.out.println(myLastwagen.radzahl);

myFahrrad.setHubraum(300); myAuto.setHubraum(1600); myLastwagen.setHubraum(100000);

Output:

2

4

4

Fehler beim Kompilieren:

The method setHubraum(int) is undefined for the type Fahrzeug

Fahrzeug myFahrrad = new Fahrzeug(2); Fahrzeug myAuto = new Auto(1400); Fahrzeug myLastwagen = new Lastwagen(80000,50);

System.out.println(myFahrrad.radzahl); System.out.println(myAuto.radzahl); System.out.println(myLastwagen.radzahl);

myFahrrad.setHubraum(300); myAuto.setHubraum(1600); myLastwagen.setHubraum(100000);

Fahrzeug→ radzahl

Auto extends Fahrzeug→ hubraum→ getHubraum→ set Hubraum

Lastwagen extends Auto→ capacity

Page 29: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 29

Klassenhierarchie – Type Casts

Warum können wir obwohl wir Objekte vom Typ Auto und Lastwagen haben den Hubraum nicht bestimmen?

Bei der Instanzierung vom Objekt myAuto

dass myAuto vom Typ Fahrzeug ist.

myAuto hätte aber schon alle Eigenschaften der Klasse Auto, Java weis das nur nicht.

Wie kann man also Java sagen, dass myAuto trotzdem vom Typ Auto ist?

→ Type Casts

Fahrzeug myAuto = new Auto(1400);

(Auto)myAuto → Typ Auto

Page 30: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 30

Klassenhierarchie - Beispiel Fahrzeug myFahrrad = new Fahrzeug(2); Fahrzeug myAuto = new Auto(1400); Fahrzeug myLastwagen = new Lastwagen(80000,50);

System.out.println(myFahrrad.radzahl); System.out.println(myAuto.radzahl); System.out.println(myLastwagen.radzahl);

((Auto)myFahrrad).setHubraum(300); ((Auto)myAuto).setHubraum(1600); ((Auto)myLastwagen).setHubraum(100000);

Output:

2

4

4

Runtime Error:

Exception in thread "main" java.lang.ClassCastException: Fahrzeug cannot be cast to Auto

Fahrzeug myFahrrad = new Fahrzeug(2); Fahrzeug myAuto = new Auto(1400); Fahrzeug myLastwagen = new Lastwagen(80000,50);

System.out.println(myFahrrad.radzahl); System.out.println(myAuto.radzahl); System.out.println(myLastwagen.radzahl);

((Auto)myFahrrad).setHubraum(300); ((Auto)myAuto).setHubraum(1600); ((Auto)myLastwagen).setHubraum(100000);

Fahrzeug→ radzahl

Auto extends Fahrzeug→ hubraum→ getHubraum→ set Hubraum

Lastwagen extends Auto→ capacity

Page 31: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 31

Klassenhierarchie – instanceof

Achtung bei Type casts → Falls der Cast nicht möglich ist, gibt es einen Runtime Error!Wie können wir das verhindern?

instanceof Operator → gibt zurück, ob ein Objekt gecasted werden kann.

if(f instanceof Auto) {System.out.println(((Auto)f).getHubraum());

}

Page 32: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 32

Klassenhierarchie - Beispiel ArrayList<Fahrzeug> myList = new ArrayList<Fahrzeug>();

myList.add(new Fahrzeug(2)); myList.add(new Auto(1400)); myList.add(new Lastwagen(80000,50));

for(Fahrzeug f : myList) { if(f instanceof Auto) {

System.out.println(((Auto)f).getHubraum());}

}

Output:

1400

80000

Fahrzeug→ radzahl

Auto extends Fahrzeug→ hubraum→ getHubraum→ set Hubraum

Lastwagen extends Auto→ capacity

Page 33: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 33

Abstrakte Klassen

MyClass extends MyAbstract{… }

Manchmal ist es sinnvoll, eine Methode zu definieren, aber nicht zu implementieren.

Beispiel: Flächeninhalt von geometrischen Objekten:

– Jedes dieser Objekte hat einen Flächeninhalt

– Für verschiedene Formen wird dieser jedoch unterschiedlich berechnet

Zwei Arten:

– abstract

– interface

Page 34: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 34

Abstrakte Klassen - abstract

Man kann sowohl Methoden implementieren, als auch nur deklarieren

Man kann keine Objekte einer abstrakten Klasse instanzieren (weil man sonst Methoden aufrufen könnte, die nicht implementiert sind!)

public abstract class GeomObject {double costPerArea;

public abstract double flaeche();

public double cost() {return flaeche()*costPerArea;

}}

Page 35: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 35

Beispiel - abstract

public class Rechteck extends GeometricObject{private double width;private double height;

public Rechteck(double width, double height,double costPerArea) {

this.width = width;this.height = height;this.costPerArea = costPerArea;

}

public double flaeche() {return width*height;

} }

public abstract class GeometricObject {double costPerArea;public abstract double flaeche();

public double cost() {return flaeche()*costPerArea;

} }

public class Kreis extends GeometricObject{private double radius;

public Kreis(double radius, double costPerArea) {this.radius=radius;this.costPerArea = costPerArea;

}

public double flaeche() {return radius*radius*3.14159;

} }

Page 36: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 36

Abstrakte Klassen - interface

MyClass implements MyInterface{… }

Vom Prinzip her ähnlich zu abstrakten Klassen, aber: Nur Methoden deklarieren, nicht implementieren

Vorteil: Eine Klasse kann mehrere Interfaces implementieren, aber nur von einer Klassen erben

public interface GeometricObject {double costPerArea;public double flaeche();public double cost();

}

Page 37: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 37

Abstrakte Klassen - interface

Wann nutzt man Interfaces?– Wenn man als Entwickler eine Schnittstelle anbieten will

– Beispiel: Wir definieren ein Interface IStack für einen Stack. Dieses Interface garantiert, dass die nötigen Funktionen: push, pop, peek, empty usw… vorhanden sind.

– Wie der Stack dann implementiert ist, belibt offen aber wenn ein Objekt das Interface IStack implementiert, kann man sicher sein, dass alle benötigten Funktionen vorhanden sind.

Page 38: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 38

Factories

Zwei Informatiker Arbeiten mit Bäumen:– Damit sie gleichzeitig Arbeiten können implementiert sie einer, der andere nutzt sie

– Anfangs wird entschieden: Wir benutzen einen ArrayTree

– Während der Entwicklung merkt man: ListTree wäre für diese Anwendung besser geeignet.

Benutze: ArrayTree

Meinung geändert:

Ändere alle ArrayTree zu ListTree

Page 39: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 39

Factories - Beispiel

Tree t = TreeFactory.makeTree();

Statt den Konstruktor direkt zu benutzen, wird dies über eine

TreeFactory gemacht!

Page 40: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

Vorbesprechung

Page 41: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 41

U6A1 - Klassen, Schnittstellen und Typumwandlungen

interface I{…}abstract class A1 implements I{…}abstract class A2 implements I{…}class C1 extends A1{…}class C2 implements I{…}class C3 extends A2{…}

I

A1

C1 C2

A2

C3

A1 c = new C1();

I i = c;

A1 a1 = c;

A2 a2 = c;

C1 c1 = c;

C2 c2 = c;

C3 c3 = c;

A1 c = new C1();

I i = c;

A1 a1 = c;

A2 a2 = c;

C1 c1 = c;

C2 c2 = c;

C3 c3 = c;

Page 42: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 42

U6A1 - Klassen, Schnittstellen und Typumwandlungen

interface I{…}abstract class A1 implements I{…}abstract class A2 implements I{…}class C1 extends A1{…}class C2 implements I{…}class C3 extends A2{…}

I

A1

C1 C2

A2

C3

A1 c = new C1();

I i = (I)c;

A1 a1 = (A1)c;

A2 a2 = (A2)c;

C1 c1 = (C1)c;

C2 c2 = (C2)c;

C3 c3 = (C3)c;

A1 c = new C1();

I i = (I)c;

A1 a1 = (A1)c;

A2 a2 = (A2)c;

C1 c1 = (C1)c;

C2 c2 = (C2)c;

C3 c3 = (C3)c;

Page 43: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 43

U6A2 - Schnittstellen und Implementierungen

Gegeben Stack ListStack, welcher das Interface IStack implementiert.

– Fabrikmethode implementieren (Wie auf der Folie „Factories-Beispiel“)

→ alle Tests bestanden, die Aufgabe ist aber noch nicht fertig

– Funktion empty im Interface und in ListStack inklusive Dokumentation hinzufügen.

– Test hinzufügen, um zu überprüfen, dass empty korrekt implementiert wurde.

Page 44: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 44

U6A3 - Polymorphie

Wir wollen eine verkettete Liste wie in Übung 5 machen, value soll aber diesesmal ein beliebiges Objekt sein dürfen.

Implementiert die Funktinen in der Klasse ListUtils (die Funktionen sind sehr ähnlich wie in der letzen Serie)

Fabrikmethode implementieren, wie gehabt

Page 45: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 45

U6A3 - Polymorphie

Jezt wollen wir ein Objekt erstellen, welches wir dann in unserer Liste speichern können.

abstract class GeometricObject

– class Rectangle extends GeometricObject

– class Triangle extends GeometricObject

Funktion area

GeometricObject implementiert Comparable → smallerThan in GeometricObject

Page 46: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 46

U6A3 - Polymorphie

Jezt können wir mit Hilfe vom interface Comparable die Funktion sort implementieren

Implementiert den selben Sortieralgoritmus wie in der Serie 5.

Anstatt if(a < b) heisst es nun if(a.smallerThan(b))

Page 47: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 47

U6A4 - Stacks und Optimierungen

Für „fortgeschrittene“ - Ihr seid aber alle schon genug fortgeschritten ;)

Klasse ChunkedStack

– Stack, welcher als Mischung vom ListStack und ArrayStack implementiert werden soll

– «first» → Array → Array → Array → null

– Was ist schneller – ChunkedStack oder ListStack?

– Wie wächst der Aufwand für size in Abhängigkeit der Listengrösse?

– Kann man size auch besser implementieren?

Page 48: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 48

Bonusübung 1

Ihr dürft bei dieser Übung auch Java Klassen importieren, z.B. java.util.Arrays

Achtung: es konnte sein, dass mehrere Studenten die selbe Legi-Nr. haben! Dies wurde jetzt aber behoben.

Abgabe bis am 15.April 00:00 → Das heisst am Samstag Abend.

Falls ihr bei den Public Tests 100% habt, aber nicht beim Einreichen:

Kreiert eigene Testfiles mit randfällen, welche ihr evt nicht berücksichtigt.

Page 49: Informatik II – Übung 6pascscha/downloads/Slides 6.pdf · Pascal Schärli | 12.04.2018 | 19 Nachbesprechung U5A2 public static void insertAt(List list, int index, int value) throws

|| 12.04.2018Pascal Schärli 49

Viel Spass!