Übungsstunde 9 Einführung in die Programmierung
Probleme bei Übung 8 [TODO Assistent]
Nachbesprechung Übung 9
Aufgabe 1: Interaktive Karte public class City extends PointOfInterest { int inhabitants ; double area; City (String name, int east, int north, int inhabitants, double area) { super(name, east, north); this.inhabitants = inhabitants ; this.area = area; Color color () { return new Color (200, 0, 0); String description () { return super.description () + " (" + inhabitants + " Einw., " + area + " km)";
Aufgabe 1: Interaktive Karte void show() { while(window.isopen()) {... for (int i = 0; i < pois.length ; i++) { PointOfInterest p = pois[i]; window.setcolor (p.color()); window.fillcircle (toguix(p.x), toguiy(p.y), 5); if (Math.abs(window.getMouseX () - toguix(p.x)) < 10 && Math.abs(window.getMouseY () - toguiy(p.y)) < 10) { window.drawstring (p.description (), toguix(p.x) + 10, toguiy(p.y)); window.refreshandclear (20)
Aufgabe 2: Miles And More (Bonus!) Feedback nach der Korrektur direkt per SVN
Spieler[] spieler = { new ZufallsWortSpieler(), new ZufallsWortSpielerMitGedaechtnis(), new BuchstabenSpieler(), new HaeufigeBuchstabenSpieler(), new SimulierenderBuchstabenSpieler(), new SimulierenderNGrammSpieler() ; Aufgabe 3: Ratespiel Spieler KonsolenSpieler ZufallsWortSpieler ZufallsWortSpielerMit Gedaechtnis Pr t h c TippsSpieler BuchstabenSpieler HaeufigeBuchstaben Spieler ff sto s g un üf Ni SimulierenderTipps Spieler Simulierender Buchstaben Spieler SimulierenderNGramm Spieler
Beispiel BuchstabenSpieler public class BuchstabenSpieler extends Spieler { String[] buchstaben = { "a",..., "z" ; String[] woerter; boolean[] aussortiert; boolean[] tippverwendet; public String gibtipp() { int uebrigew = zaehlefalse(aussortiert); int uebriget = zaehlefalse(tippverwendet); if (uebrigew > 2 && uebriget > 0) { // verwende einen Tipp, den wir noch nicht probiert haben else { // probiere eins der noch übriggebliebenen Wörter public void bekommehinweis( String tipp, String hinweis) { // setze aussortiert[i] auf 'true', falls das i-te Wort gemäss // erhaltenem Hinweis nicht mehr möglich ist.
Beispiel BuchstabenSpieler public class BuchstabenSpieler extends TippsSpieler { protected String[] buchstaben = {"a",..., "z"; protected String[] tipps(string[] woerter) { return buchstaben; protected int tippindex() { for(int i = 0; i < tipps.length; i++) if(!tippverwendet[i]) return i; Die Musterlösung extrahiert einen Teil der Funktionalität in eine Superklasse TippsSpieler, die auch von anderen Spielern verwendet wird
Vorbesprechung Übung 10
Aufgabe 1: Klassen (Bonus!) Koordinate koordinate = new Koordinate(1, 2); Punkt punkt = new Punkt(koordinate); Rechteck rechteck = new Rechteck(koordinate, 10, 20); Quadrat quadrat = new Quadrat(koordinate, 42); Kreis kreis = new Kreis(koordinate, 55); Zweidimensional Punkt Koordinate Form Zweidimensional[] objekte = new Zweidimensional[] { punkt, rechteck, quadrat, kreis ; Form[] formen = new Form[] { rechteck, quadrat, kreis ; Rechteck[] rechtecke = new Rechteck[] { rechteck, quadrat ; Kreis System.out.println(koordinate); for (Zweidimensional obj : objekte) { System.out.println(obj); System.out.println("--"); for (Form f : formen) { System.out.println(f + ", Flaeche: " + f.flaeche()); System.out.println("--"); for (Rechteck r : rechtecke) { System.out.println( r + ", Flaeche: " + r.flaeche() + ", Diagonale: " + r.diagonale()); Rechteck Quadrat
Anhang B: Ausgabe Bonusaufgabe [1, 2] Punkt([1, 2]) Rechteck([1, 2], 10, 20) Quadrat([1, 2], 42) Kreis([1, 2], 55) -Rechteck([1, 2], 10, 20), Flaeche: 200 Quadrat([1, 2], 42), Flaeche: 1764 Kreis([1, 2], 55), Flaeche: 9503 -Rechteck([1, 2], 10, 20), Flaeche: 200, Diagonale: 22 Quadrat([1, 2], 42), Flaeche: 1764, Diagonale: 59 Ausgabe muss genau so aussehen
Aufgabe 2: Interfaces
Aufgabe 2: Interfaces
public class PointOfInterest implements Drawable { @Override public void draw(window window) { // TODO // SwissMap-Klasse // TODO: Add more components to window window.addcomponent(this); for(pointofinterest poi : pois){ window.addcomponent(poi); while(window.isopen()) { // All drawing and interaction should be // handled by components, so don't add // anything here. window.refreshandclear(20);
window.drawimagecentered( path, x, y, scale);
city_night.png mountain_night.png lake_night.png
Aufgabe 3: Exceptions Ziel: Fehlerbehandlung mit Exceptions Was, wenn datarow sinnlose Werte enthält? Person() soll eine checked-exception public class Person { public int age; // years public int weight; // kg public int height; // cm public boolean ismale; public Person( String datarow) { Scanner scanner = new Scanner(dataRow); age = scanner.nextint(); weight = scanner.nextint(); height = scanner.nextint(); String gender = scanner.next(); ismale = gender.equals( "m"); public double bodymassindex() { return 10000 * weight / (height * height); IllegalPersonFormatException werfen public class IllegalPersonFormatException extends Exception { public IllegalPersonFormatException( String message) { super(message);
Zusatzübungen
Exceptions public class TryCatch { public static void main(string[] args) { System.out.println( "main()" ); int ireturned = new TryCatch().m(); System.out.println( "m returned " + ireturned); public int m() { int i = 0; try { System.out.println( "m(): try" ); i = 100 / 0; catch (Exception e) { System.out.println( "m(): catch" ); i = 200; return i; Was wird ausgegeben?
Exceptions 2 public class TryCatch { public static void main(string[] args) { System.out.println( "main()" ); new TryCatch().m(); Was wird ausgegeben? public void m() { try { System.out.println( "m(): try" ); int i = 1 / 0; catch (NullPointerException e) { System.out.println( "m(): catch NullPE" ); catch (ArithmeticException e) { // Div-by-0 System.out.println( "m(): catch DivBy0" ); catch (Exception e) { System.out.println( "m(): catch Exc" ); Exception RuntimeException NullPointerException ArithmeticException
Exceptions 2 public class TryCatch { public static void main(string[] args) { System.out.println( "main()" ); new TryCatch().m(); public void m() { try { System.out.println( "m(): try" ); int i = 1 / 0; catch (NullPointerException e) { System.out.println( "m(): catch NullPE" ); catch (Exception e) { System.out.println( "m(): catch Exc" ); catch (ArithmeticException e) { // Div-by-0 System.out.println( "m(): catch DivBy0" ); Fängt nie etwas Fehler
Exceptions 3 public class TryCatch { public static void main(string[] args) { System.out.println( "main()" ); try { new TryCatch().m(); catch (RuntimeException e) { System.out.println( "main(): catch Exc" ); public void m() { try { System.out.println( "m(): try" ); int i = 1 / 0; catch (RuntimeException e) { System.out.println( "m(): catch Exc" ); throw e; Was wird ausgegeben?
Exceptions 4 public class TryCatch { public static void main(string[] args) { System.out.println( "main()" ); try { new TryCatch().m(); catch (Exception e) { System.out.println( "caught exception" ); Fängt checked und unchecked Exceptions Wenn checked Exception in der Methode nicht abgefangen wird, throws. checked Exceptions public void m() throws FileNotFoundException { try { throw new FileNotFoundException(); catch (RuntimeException e) { System.out.println( "unchecked exception" ); Exception RuntimeException IOException FileNotFoundException unchecked Exceptions
Diamanten class Vehicle { public void start() {System.out.println("start");; public void stop() {System.out.println("stop");; class Car extends Vehicle { public void honk() {System.out.println("*toot*");; class Boat extends Vehicle { public void swim() {System.out.println("swim..."); public void start() {System.out.println("depart");; class AmphiCar extends Car, Boat { Nicht möglich in Java!
Vehicle Diamanten start() stop() Car Boat honk() swim() start() AmphiCar Nicht möglich in Java!
<<I>> Vehicle Interfaces! start() stop() interface Vehicle { public void start(); public void stop(); interface Car extends Vehicle { public void honk(); interface Boat extends Vehicle { public void swim(); <<I>> Car <<I>> Boat interface AmphiCar extends Car, Boat { honk() swim() <<I>> AmphiCar Möglich mit Interfaces!
Interfaces! interface Vehicle { public void start(); public void stop(); class AbstractVehicle implements Vehicle { public void start() { System.out.println( "start");; public void stop() { System.out.println( "stop");; interface Car extends Vehicle { public void honk(); class BMWCar extends AbstractVehicle implements Car { public void honk() { System.out.println( "*honk*" );; interface Boat extends Vehicle { public void swim(); class SpeedBoat extends AbstractVehicle implements Boat { public void swim() { System.out.println( "swim..." ); public void start() { System.out.println( "depart" ); interface AmphiCar extends Car, Boat { class AmphiCarM770 extends AbstractVehicle implements AmphiCar { public void honk() { System.out.println( "*toot*" );; public void swim() { System.out.println( "swim..." );
<<I>> Vehicle start() stop() AbstractVehicle start() stop() <<I>> Car <<I>> Boat honk() swim() <<I>> AmphiCar BMWCar SpeedBoat honk() swim() AmphiCarM770
class Wal { void a() { System.out.print( "Wal a " ); b(); void b() { System.out.print( "Wal b " ); String tostring() { return "Wal"; class Zahnwal extends Wal { void b() { System.out.print( "Zahnwal b " ); Was wird von a() ausgegeben, wenn a() auf einem Zahnwal-Objekt aufgerufen wird?
class Wal { void a() { System.out.print( "Wal a " ); b(); void b() {System.out.print( "Wal b " ); String tostring() { return "Wal"; class Zahnwal extends Wal { void b() { System.out.print( "Zahnwal b " ); class Schweinswal extends Delfinartig { void b() { System.out.print( "Schweinswal b " ); class Delfinartig extends Zahnwal { void a() { System.out.print( "Delfinartig a super.a(); " ); String tostring() { return "Delfinartig" ; Wal [] intheocean = { new Zahnwal(), new Wal(), new Schweinswal(), new Delfinartig(); for (int i = 0; i < intheocean.length; i++) { System.out.println( intheocean [i]); intheocean [i].a(); System.out.println(); intheocean [i].b(); System.out.println(); System.out.println(); Was wird ausgegeben? (für den Zahnwal, Wal, Schweinswal und Delfinartig )
Wal Klassendiagramm a() b() tostring() class Wal { void a() { void b() { Zahnwal String tostring() { class Zahnwal extends Wal { void b() { a() b() tostring() class Delfinartig extends Zahnwal { void a() { String tostring() { class Schweinswal extends Delfinartig { void b() { Delfinartig a() b() tostring() Schweinswal a() b() tostring()
Subtype-Relation method Wal Zahnwal Delfinartig Schweinswal a Wal a b() Wal a b() Delfinartig a Wal a b() Delfinartig a Wal a b() b Wal b Zahnwal b Zahnwal b Schweinswal b Wal Delfinartig Delfinartig tostring Wal
Rekursion und einfache Liste Visualisierung (Link)