Bergervei/Java-Balkendiagramm
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>Java</language>
== Definition und Ausgabe eines eindimensionalen Arrays ==
<code id="4f626d7c52dcd">
int[] Zahlen = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public void ZahlenAusgabe(int[] liste) {
for (int i = 0; i < liste.length; i++)
if (i == liste.length-1)
System.out.println(liste[i]);
else
System.out.print(liste[i]+", ");
}
</code>
<run id="4f626d7c52f87">
ZahlenAusgabe(Zahlen);
</run>
== Zufallszahlen ==
<code id="4f626d7c52e3b">
int min = 1;
int max = 1000;
int n = 20;
int[] ZahlenListe = new int[n];
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.random()*(max-min+2)+min-1);
return Zahlen;
}
</code>
<run id="4f626d7c52feb">
ZahlenListe = ZufallsZahlen(min, max, n);
ZahlenAusgabe(ZahlenListe);
</run>
== BubbleSort ==
<code id="4f626d7c52ea0">
int[] SortListe = new int[n];
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>
<run id="4f626d7c53048">
System.arraycopy(ZahlenListe, 0, SortListe, 0, ZahlenListe.length);
BubbleSort(SortListe);
ZahlenAusgabe(SortListe);
</run>
== Darstellung im Balkendiagramm ==
<code id="4f626d7c52f04">
public Object BalkenDiagramm(Object canvas, Object turtle) {
double Rand = 50;
double MaxBreite = 400-2*Rand;
double MaxHoehe = 300-2*Rand;
private int[] MinMax(int[] liste) {
int[] Ergebnis = {0, 0};
for (int i = 0; i < liste.length; i++) {
if (Ergebnis[0] > liste[i]) Ergebnis[0] = liste[i];
if (Ergebnis[1] < liste[i]) Ergebnis[1] = liste[i];
}
return Ergebnis;
}
private void Balken(double hoehe, double breite) {
turtle.forward(breite); turtle.left(90);
turtle.forward(hoehe); turtle.left(90);
turtle.forward(breite); turtle.left(90);
turtle.forward(hoehe); turtle.left(90);
turtle.forward(breite);
}
public void Darstellung(int[] liste) {
int[] ExtremWert = new int[2];
ExtremWert = MinMax(liste);
double Min = ExtremWert[0];
double Max = ExtremWert[1];
double Breite = MaxBreite/liste.length;
canvas.clear();
turtle.hide();
turtle.move(Rand, Rand+Max/(Max-Min)*MaxHoehe);
turtle.right(90);
for (int i = 0; i < liste.length; i++)
Balken(liste[i]/(Max-Min)*MaxHoehe, Breite);
}
return this;
}
</code>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
'''Ungeordnete Zufallszahlen:'''
<canvas w="400" h="300"></canvas>
</td>
<td> </td>
<td align="center">
'''Geordnete Zufallszahlen:'''
<canvas w="400" h="300"></canvas>
</td>
</tr>
</table>
<run id="4f626d7c530f4">
Object BalkenDiagramm1 = BalkenDiagramm(canvas1, turtle1);
Object BalkenDiagramm2 = BalkenDiagramm(canvas2, turtle2);
BalkenDiagramm1.Darstellung(ZahlenListe);
BalkenDiagramm2.Darstellung(SortListe);
System.out.println("ok");
</run>