faecher:informatik:oberstufe:algorithmen:sorting:problemstellung:start

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung
Nächste Überarbeitung
Vorherige Überarbeitung
faecher:informatik:oberstufe:algorithmen:sorting:problemstellung:start [06.02.2023 09:56] – [Eigene sortierbare Objekte] Frank Schiebelfaecher:informatik:oberstufe:algorithmen:sorting:problemstellung:start [08.02.2023 16:06] (aktuell) – [Sortiert?] Frank Schiebel
Zeile 55: Zeile 55:
  
 <code java> <code java>
-public class Date implements Comparable<Date> // Hier wird angegeben, dass das Comparable Interface implementiert wird.+public class Time implements Comparable<Time> // Hier wird angegeben, dass das Comparable Interface implementiert wird.
 { {
-    private final int monthday, year;+    private int hourminute;
          
-    public Date(int m, int d, int y)+    public Time(int h, int m)
     {     {
-        month m+        hour h
-        day d; +        minute m;
-        year = y;+
     }     }
  
-    public int compareTo(Date that)+    public int compareTo(Time that)
     {     {
-       if (this.year < that.year ) return -1; +       if (this.hour < that.hour ) return -1; 
-       if (this.year > that.year ) return +1; +       if (this.hour > that.hour ) return +1; 
-       if (this.month < that.month) return -1; +       if (this.minute > that.minute ) return +1; 
-       if (this.month > that.month) return +1; +       if (this.minute < that.minute ) return -1;
-       if (this.day < that.day ) return -1; +
-       if (this.day > that.day ) return +1;+
        return 0;        return 0;
     }     }
Zeile 79: Zeile 76:
  
 </code> </code>
 +
 +
 +Der Vergleich zweier Objekte des Typs ''Time'' findet dann folgendermaßen statt:
 +
 +<code java>
 +Time t1 = new Time(15,5);
 +Time t2 = new Time(15,7);
 +
 +if (t1.compareTo(t2) < 0 ) {
 +    System.out.println("t1 ist kleiner als t2"); 
 +
 +if (t1.compareTo(t2) > 0 )
 +    System.out.println("t2 ist kleiner als t1"); 
 +}
 +
 +if (t1.compareTo(t2) == 0 )
 +    System.out.println("t2 und t1 sind gleich"); 
 +}
 +
 +</code>
 +
 +----
 +{{:aufgabe.png?nolink  |}}
 +=== (A1) ===
 +{{ :faecher:informatik:oberstufe:algorithmen:sorting:problemstellung:ssp.png?200|}}
 +  * Teste das Beispiel mit der Klasse ''Time'' in BlueJ
 +  * Denke dir eine eigene Klasse aus, die das Comparable Interface implementiert, implementiere und teste sie.
 +  * Kann man das Spiel Schere-Stein-Papier mit einer Klasse ''Spielzug'' implementieren, die das   Comparable Interface implementiert? Begründe deine Antwort.
 +
 +===== Hilfsfunktionen =====
 +
 +Da der Vergleich mittels ''etwas.compareTo(anderesEtwas) < 0'' ein wenig unhandlich ist, vereinbaren wir für diesen Wikibereich zwei **Hilfsfunktionen**, die uns die Implementation der Sortieralgorithmen erleichtern werden:
 +
 +==== Kleiner... ====
 +
 +Die Hilfsfunktion ''less'' nimmt zwei Elemete des Typs Comparable entgegen und liefert ''true'' zurück, wenn das erste kleiner ist als das zweite:
 +
 +<code java>
 +private boolean less(Comparable v, Comparable w)
 +
 +    return v.compareTo(w) < 0; 
 +}
 +</code>
 +
 +==== Platztausch ====
 +
 +Die Hilfsfunktion ''exch'' vertauscht zwei Elemente eines Arrays:
 +<code java>
 +private void exch(Comparable[] a, int i, int j)
 +{
 +    Comparable swap = a[i];
 +    a[i] = a[j];
 +    a[j] = swap;
 +}
 +</code>
 +
 +==== Sortiert? ====
 +
 +Die letzte Hilfsfunktion gibt ''true'' zurück, wenn das Array sortiert ist:
 +
 +<code java>
 +private boolean isSorted(Comparable[] a)
 +{
 +    for (int i = 1; i < a.length; i++) {
 +        if (less(a[i], a[i-1])) return false;
 +        }
 +    return true;
 +}
 +</code>
 +
 +
 +==== Material ====
 +
 +{{simplefilelist>:faecher:informatik:oberstufe:algorithmen:sorting:problemstellung:*}}
 +
 +
  • faecher/informatik/oberstufe/algorithmen/sorting/problemstellung/start.1675677372.txt.gz
  • Zuletzt geändert: 06.02.2023 09:56
  • von Frank Schiebel