Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2013-03-13T12:10:31+00:00 → 2013-03-13T12:29:57+00:00
<language>Java</language> | |
| − | '''Beispiel Kürzen (Java)''' |
| + | = Beispiel Bubblesort (Java) = |
| + | Dabei werden jeweils nur die benachbarten Zahlen vertauscht. Das ist der einfachste Algorithmus, nur nicht sonderlich effektiv... |
| − | Quelltext: |
| − | |
| − | == Objektorientierte Programmierung == |
| − | === Technische Voraussetzungen === |
| − | Zur Objektorientierten Programmierung in Java ist die Policy-Datei |
| − | * <tt>JavaPolicy.class</tt> |
| − | erforderlich. Entsprechende Hinweise findet man unter: [[JavaPolicy]] |
| − | === Definition der Klasse ''Bruch''=== |
| − | <code id="51406cb7a8cc3"> |
| − | public class Bruch { |
| − | protected int z; |
| − | protected int n; |
| − | public Bruch(int z, int n) { |
| − | this.z = z; |
| − | this.n = n; |
| − | } |
| − | public int Zaehler() { |
| − | return z; |
| − | } |
| − | public int Nenner() { |
| − | return n; |
| − | } |
| − | public void Ausgabe() { |
| − | System.out.println(z + "/" + n); |
| − | } |
| + | <language>Java</language> |
| + | == Bubblesort == |
| + | <code id="51407083a977d"> |
| + | public int[] ZufallsZahlen(int min, int max, int n) { |
| + | int[] Zahlen = new int[n]; |
| + | for (int i = 0; i < n; i++) |
| + | Zahlen[i] = (int)Math.floor(Math.random()*(max-min+1)+min); |
| + | return Zahlen; |
} | |
| + | public void ZahlenAusgabe(int[] liste) { |
| + | for (int i = 0; i < liste.length; i++) |
| + | if (i < liste.length-1) |
| + | System.out.print(liste[i]+", "); |
| + | else |
| + | System.out.println(liste[i]); |
| + | } |
| + | public void BubbleSort(int[] liste) { |
| + | int h; |
| + | for (int j = 0; j < liste.length-1; j++) |
| + | for (int i = j; i < liste.length; i++) |
| + | if (liste[i] < liste[j]) { |
| + | h = liste[i]; liste[i] = liste[j]; liste[j] = h; |
| + | } |
| + | } |
</code> | |
| − | === Instanz der Klasse ''Bruch''=== |
| − | <run id="51406cb7a8d4e"> |
| − | Bruch1 = new Bruch(1, 2); |
| − | System.out.print("Bruch1: "); Bruch1.Ausgabe(); |
| + | |
| + | <run id="51407083a97e9"> |
| + | int[] ZahlenListe = ZufallsZahlen(1, 9000, 20); |
| + | System.out.println("Unsortierte Zufallszahlen:"); |
| + | ZahlenAusgabe(ZahlenListe); |
| + | System.out.println("Sortierte Zufallszahlen:"); |
| + | BubbleSort(ZahlenListe); |
| + | ZahlenAusgabe(ZahlenListe); |
</run> |