Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 876

Warning: Invalid argument supplied for foreach() in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 877

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 881

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 876

Warning: Invalid argument supplied for foreach() in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 877

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 881

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 876

Warning: Invalid argument supplied for foreach() in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 877

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 881

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 876

Warning: Invalid argument supplied for foreach() in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 877

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 881

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 876

Warning: Invalid argument supplied for foreach() in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 877

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 881

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 876

Warning: Invalid argument supplied for foreach() in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 877

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 881

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /www/htdocs/w010bef3/rbb_wiki/includes/MagicWord.php on line 902
Interfaces: Unterschied zwischen den Versionen – Wiki der 13ITO4
K (more schön)
K (Kategorie hinzugefügt)
 
Zeile 10: Zeile 10:
 
* Eine Klasse kann jedoch mehrere Interfaces implementieren
 
* Eine Klasse kann jedoch mehrere Interfaces implementieren
 
* Schlüsselwort "implements"
 
* Schlüsselwort "implements"
 +
 +
== Beispiel: ==
 +
 +
interface myInterface {
 +
    public int getX();
 +
    public void setX(int x);
 +
}
 +
 +
public class implX implements myInterface {
 +
    int x = 0;
 +
    public int getX(){return x;}
 +
    public void setX(int x){this.x = x}
 +
}
 +
 +
== Aufgabe ==
 +
 +
Wir haben eine Heizung, die mit verschiedenen Stoffen angeheizt werden kann. Alle diese Stoffe haben einen bestimmten Heizwert der angibt für wie viele Stunden mit diesem Stoff Wärme erzeugt werden kann.
 +
Als Stoffe haben wir Holz (Brennwert = 3), Öl (Brennwert = 2) und Gas (Brennwert = 1).
 +
 +
== Lösung ==
 +
=== Heizanlage.java ===
 +
 +
public class Heizanlage {
 +
 +
private int waermeSpeicher = 0;
 +
 +
  public void speicherAuffuellen(Verheizbar stoff) {
 +
    this.waermeSpeicher += stoff.verbrennen();
 +
    System.out.println(this.waermeSpeicher + " Stunden kann geheizt werden");
 +
  }
 +
 +
  // gibt false zurück, falls der Speicher leer ist
 +
  public boolean wohnungHeizen(int zeit) {
 +
 +
    if (this.waermeSpeicher - zeit < 0) {
 +
      System.out.println("Es kann nur noch für " + (this.waermeSpeicher) + " Stunden geheizt werden");
 +
      this.waermeSpeicher = 0;
 +
    }
 +
    else  {
 +
    System.out.println("Habe geheizt...");
 +
      this.waermeSpeicher -= zeit;
 +
    }
 +
    if (this.waermeSpeicher == 0) {
 +
      System.out.println("Bitte Speicher auffüllen!");
 +
      return false;
 +
    }
 +
    return true;
 +
  }
 +
}
 +
=== Verheizbar.java ===
 +
 +
public interface Verheizbar {
 +
  // verbrennt den brennbaren Stoff und liefert die Stunden zurück,
 +
  // wie lange die Wohnung mit diesem Stoff geheizt werden kann
 +
  int verbrennen();
 +
}
 +
 +
 +
=== Gas.java ===
 +
 +
public class Gas implements Verheizbar
 +
{
 +
public int verbrennen() {
 +
    return 3;
 +
  }
 +
}
 +
 +
=== Holz.java ===
 +
public class Holz implements Verheizbar {
 +
 +
  public int verbrennen() {
 +
    return 2;
 +
  }
 +
}
 +
 +
=== Oel.java ===
 +
 +
public class Oel implements Verheizbar{
 +
public int verbrennen() {
 +
    return 1;
 +
  }
 +
}
 +
 +
=== app.java ===
 +
 +
public class app {
 +
 +
public static void main(String[] args) {
 +
 
 +
Heizanlage anlage = new Heizanlage();
 +
 
 +
  // Heizanlage auffüllen
 +
 +
  anlage.speicherAuffuellen(new Holz());
 +
 +
  anlage.speicherAuffuellen(new Gas());
 +
 +
  anlage.speicherAuffuellen(new Holz());
 +
 +
  anlage.speicherAuffuellen(new Oel());
 +
 +
  anlage.speicherAuffuellen(new Holz());
 +
 
 +
 +
// Vorrat anlegen
 +
 +
  Verheizbar[] vorrat = new Verheizbar[8];
 +
 +
  vorrat[0] = new Holz();
 +
 +
  vorrat[1] = new Oel();
 +
 +
  vorrat[2] = new Holz();
 +
 +
  vorrat[3] = new Gas();
 +
 +
  vorrat[4] = new Gas();
 +
 +
  vorrat[5] = new Holz();
 +
 +
  vorrat[6] = new Oel();
 +
 +
  vorrat[7] = new Holz();
 +
 +
 
 +
 +
  // Tag mit 24 Stunden
 +
 +
  for (int i = 0; i < 24; i++) {
 +
 +
    if (!anlage.wohnungHeizen(1)) {
 +
 +
      for (int j = 0; j < vorrat.length; j++) {
 +
 +
        if (vorrat[j] != null) {
 +
 +
          anlage.speicherAuffuellen(vorrat[j]);
 +
 +
          vorrat[j] = null;
 +
 +
          break;
 +
 +
        }
 +
 +
      }
 +
 +
    }
 +
 +
  }
 +
}
 +
}
 +
 +
 +
  
 
[https://docs.google.com/presentation/d/1273DDSpMTjLdIBAnkC6maFoJ_jwRysKMNoZgbASU5pI/edit#slide=id.p Präsentation]
 
[https://docs.google.com/presentation/d/1273DDSpMTjLdIBAnkC6maFoJ_jwRysKMNoZgbASU5pI/edit#slide=id.p Präsentation]
 +
[[Kategorie:Anwendungsentwicklung]]

Aktuelle Version vom 15. Juli 2013, 12:08 Uhr