1
marc-oliver pahl marc-oliver pahl marc-oliver pahl
Informatik I – Kapitel 7
„ Klassen und höhere Datentypen“
Zusammenfassung des Kapitel 7
Küchlin, Weber, Einführung in die Informatik, 2.Auflage
25.1.2004
marc-oliver pahl marc-oliver pahl marc-oliver pahl
date example (major changes to the book p.191)
2 public class Date{private int day, month, year;
private static final byte D = 1; private static final byte M = 1;
private static final short Y = 1900;
// constructors
public Date(){ this(D, M, Y); } public Date(int y){ this(D, M, y); } public Date(int m, int y){ this(D, m, y); }
public Date(int d, int m, int y){ setDay(d); setMonth(m); setYear(y); } // selectors (the selectors are also class-methods)
public int getDay(){ return day; } public int getMonth(){ return month; } public int getYear(){ return year; } public void setDay(int d){ day=d; } public void setMonth(int m){ month=m; } public void setYear(int y){ year=y; }
// methods (for information about the toString method see JDK-Doc „Object“) public String toString(){ return (day+"."+month+"."+year); }
}
2
marc-oliver pahl marc-oliver pahl marc-oliver pahl
Wie teste ich meine Klasse?
3• Zum Testen der Date-Klasse habe ich im Projekt noch die folgende Main-Klasse angelegt.
Analog könnt Ihr die Klassen, die Ihr schreibt, testen!
public class Main {
public static void main(String[ ] args) {
Date geburtsDatum = new Date(10, 11, 1980);
Date defaultDatum = new Date();
System.out.println("geburtsDatum: "+geburtsDatum );
System.out.println("defaultDatum: "+defaultDatum );
// hier wird automatisch die .toString-Methode aufgerufen }
}
Das zip-File mit dem Beispielcode befindet sich auf der Webseite.
marc-oliver pahl marc-oliver pahl marc-oliver pahl
Was passiert bei der Objekterstellung?
4Date
private int day;
private int month;
private int year;
public Date() ...
public Date(int d, int m, int y) public int getDay()
...
public void setDay( int d ) ...
Heap
day = 10;
month = 11;
year = 1980;
Date geburtsDatum Date geburtsDatum =
new Date(10, 11, 1980);
KOPIE
Es wird also eine neue Instanz der Date-
Klasse mit dem Namen geburtsDatum
erzeugt!
3
marc-oliver pahl marc-oliver pahl marc-oliver pahl
Klassen-/ Instanzvariablen
5Id
private static int counter=1;
private int id;
public Id(){id = counter++;}
public int getId(){return id;}
class Test{
public static void main(String[] args){
Id idA = new Id();
Id idB = new Id();
Id idC = new Id();
System.out.println( “idA: “+idA.getId() );
System.out.println( “idB: “+idB.getId() );
System.out.println( “idC: “+idC.getId() );
} }
Heap Id idA counter id =1
Id idB counter id =2
Id idC counter id =3
X X2 3
X4
1 2 3
marc-oliver pahl marc-oliver pahl marc-oliver pahl
Exceptions
6• dienen der kontrollierten Ausnahmebehandlung Throwable
Error Exception
public class MyException extends Exception{
public MyException(String val){ super(val); } }
MyException
public static void main(String[] args) { try{ myProcedure(); }
catch(MyException e){ System.out.println("MyException: "+e.getMessage()); } }
public static void myProcedure() throws MyException{
throw new MyException("Ich bin eine Exception");
}
Die Exception:
Methode, die die Exception wirft:
Code, der die Methode aufruft:
4
marc-oliver pahl marc-oliver pahl marc-oliver pahl
Assertions
7• generieren abhängig von einer Bedingung zur Laufzeit Meldungen und dienen
– der aktiven Dokumentation, also der Ausgabe von Informationen über den aktuellen Zustand, indem sie z.B. die Schleifenbedingungen über- prüfen...
– der Fehlersuche (Debugging)
erg := 0;
return erg;
erg := k + erg;
k := k - 1;
k := n;
[n >= 0]
[k < 0]
[k>=0]
public static int sum_rek(int n){
assert n>=0 : “\ !(n>=0) n=“+n;
if ( n == 0 ) return 0;
return ( n + sum_rek( n-1 ) ) }
zu dem Beispiel siehe auch Folien sum_java
marc-oliver pahl marc-oliver pahl marc-oliver pahl
einfach verkettete Listen (simply linked lists)
8SimplyLinkedList private Node startNode;
...
public SimplyLinkedList() ...
public T getDataAt(int i) ...
Node
private Node nextNode;
private T value;
public Node(T value) ...
public T getValue() ...
T steht jeweils für einen Datentyp, z.B. int, String, Date, ...
Node nextNode
Node nextNode
Node nextNode
Node nextNode
Node nextNode
KOPIEN/ INSTANZEN...
5
marc-oliver pahl marc-oliver pahl marc-oliver pahl
doppelt verkettete Listen (doubly linked lists)
9DoublyLinkedList private Node startNode;
...
public SimplyLinkedList() ...
public T getDataAt(int i) ...
Node
private Node nextNode;
private Node prevNode;
private T value;
...
public Node(T value) ...
public T getValue() ...
Node nextNode prevNode
...
Node nextNode prevNode
Node nextNode prevNode
Node nextNode prevNode
marc-oliver pahl marc-oliver pahl marc-oliver pahl
Stack/ Queue
10Stack: LastInFirstOut:
23 42 65 7
push(8);
top();
8
8
Queue: FirstInFirstOut:
23 42 65 7
push(8);
top();
8
7
Die Anordnung der Werte von oben nach unten ist willkürlich gewählt und könnte genau so gut andersrum sein.