Übungsstunde 10 Einführung in die Programmierung I
Probleme bei Übung 9 [TODO Assistent]
Nachbesprechung Übung 10
Aufgabe 1 <<I>> Comparable boolean lessthan(comparable other) Ziel: Schreiben von Methoden, die für alle Instanzen von Klassen funktionieren, die Comparable implementieren. public interface Comparable { boolean lessthan(comparable other); Canton lessthan() tostring() City lessthan() tostring() Lake lessthan() tostring() Mountain lessthan() tostring() public class Canton implements Comparable { public boolean lessthan(comparable other) { if(other instanceof Canton) return area < ((Canton) other).area; else throw new NotComparableException( "cannot compare " + this + " with " + other); lessthan muss per instanceof den dynamischen Typen abfragen und casten Hint: beim generischen Comparable<T> ist das nicht mehr nötig
public static Comparable max(comparable[] array) { Comparable max = array[0]; for(int i = 1; i < array.length; i++) if(max.lessthan(array[i])) max = array[i]; return max; Comparable Objekte unterstützen lessthan() Mountain highest = ( Mountain) ArrayUtils.max(mountains); max() gibt ein Comparable zurück, was auch wieder ge-casted werden muss.
Aufgabe 2 Ziel: Fehlerbehandlung mit Exceptions Ankündigen, weil checked Exception public Person(String datarow) throws IllegalPersonFormatException { Scanner scanner = new Scanner(dataRow); try { age = scanner.nextint(); if(age < 0) throw new IllegalPersonFormatException( "negative age" ); //... ismale = gender.equals( "m"); catch(inputmismatchexception e) { throw new IllegalPersonFormatException( "non-int value" ); catch(nosuchelementexception e) { throw new IllegalPersonFormatException( "not enough values" ); Sinnlose Daten Umwandeln von internen Exceptions
Klient der Person-Klasse: static LinkedPersonList readpersons(scanner scanner) { LinkedPersonList persons = new LinkedPersonList(); while(scanner.hasnextline()) { try { persons.addlast( new Person(scanner.nextLine())); catch(illegalpersonformatexception e) { System.out.println( "Error: illegal person data: " + e.getmessage()); return persons; Muss Exception handlen und überspringt dann fehlerhafte Zeilen.
Vorbesprechung Übung 11
Aufgabe 1: Comparable Modern Art Ziel: Kunst und Comparable<T> benutzen public class ModernArtApp { public static void main(string[] args) throws InterruptedException { // create a frame (window) JFrame frame = new JFrame("Modern Art" ); frame.setminimumsize( new Dimension(400, 300)); frame.setdefaultcloseoperation(jframe.exit_on_close); // add a modern art panel to the frame frame.add( new ModernArtPanel()); // show the frame frame.setvisible( true); // repaint the frame (window) every 100 ms, so that there is some action! while(true) { frame.repaint(); Thread.sleep(100);
Comparable<T> <<I>> Comparable<T> int compareto(t other); <<bind>> T Shape public int compareto( Shape other) { // this.area() und other.area() <<I>> Shape void draw(graphics g); double area(); Dank generischem Typ Comparable<T> hat other nun den Typ Shape, auf den wir area() aufrufen können (ohne Cast) Rectangle draw() area() compareto( Shape o) Oval <<implements>> draw() area() compareto( Shape o)
Aufgabe 2 Ziel: Auswerten von einfachen Ausdrücken (Expressions) Enter a value for x: 2.0 >> 1+x 3.0 >> sin(x) 0.9092974268256817 >> a+x unknown variable 'a' >> (1 + sin(x)) * 5^x Syntax error: unexpected token '^', expected end of input >> (1 + sin(x)) * (5^x) 47.73243567064204 >> exit
Aufgabe 2: Parsen Legale Expressions? 1. 2. a 3. x % 5 4. 1b 5. a + b 6. a * 2 7. -1 8. (a + 2 ) 9. 2x+y 10. 1++ 11. (1 + 2) * 3 12. sin() 13. 1 + (2 + (3.1)) 14. x^0.5 15. 1 + 1 + 1 16. sin(x) * cos(x^2) 17. var1
Aufgabe 2: Parsen Folgt diese Zeichenkette der EBNF Beschreibung? Wie gehen Sie vor? ( 1 + 2 ) + 3 Wie schreibt man ein Programm, das dies überprüft?
Aufgabe 2: Parsen Regeln werden zu Methoden Alternativen werden zu if-else-if-else Regeln auf der RHS werden zu Methodenaufrufen void term() { if(next token is open) { consume open token expr(); check and consume close token else if(next token is func) { consume func token expr(); check and consume close token else { atom();
Aufgabe 2: Parsen ( 1 + 2 ) + ) Tokenizer expr() term() term() Error: Expected num or var! expr() atom() term() term() atom() atom()
Aufgabe 2: Parsen Die roten Regeln sind Tokenizer-Regeln, die in der Projektvorlage in der Klasse Tokenizer vorgegeben sind. check consume
Aufgabe 2: Evaluieren Während dem Parsen kann auch gleich evaluiert werden. double evalterm() { if(next token is open) { consume open token double val = eval expr(); check and consume close token return val; else if(/* next token is func */) { consume func token double arg = eval expr(); double val = applythatfunc(arg) check and consume close token return val; else { return evalatom();
Aufgabe 2: Evaluieren ( 1 + 2 ) + 3 Tokenizer 6.0 expr() 3.0 3.0 term() 3.0 expr() term() 3.0 atom() 1.0 1.0 term() atom() 2.0 term() 2.0 atom()
Aufgabe 3: Plotten GUI-x-Achse Für jeden Wert x auf der x-achse einen Wert y auf der y-achse berechnen. Beispiel: GUI-y-Achse y-achse x-achse
Aufgabe 3: Plotten PlotterApp hat einen FunctionChangeListener, der bei Änderung im JTextField die setfunction-methode auf das PlotterPanel aufruft. PlotterPanel JLabel JTextField
Zusatzübungen