Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Sortierbar
- {
- public int istGroesser(Sortierbar element);
- }
- abstract class Figur
- {
- protected String farbe;
- protected int radius;
- public Figur(String f, int r)
- {
- this.farbe=f;
- this.radius=r;
- }
- public abstract String toString();
- }
- class Kugel extends Figur implements Sortierbar
- {
- public Kugel(String f, int r)
- {
- super(f,r);
- }
- public int istGroesser(Sortierbar k) {
- Kugel k1 = (Kugel)k;
- if (k1.radius < this.radius) {
- return -1;
- }
- if (k1.radius == this.radius) {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- public String toString()
- {
- String text = "";
- text = "Kugel: Farbe: "+this.farbe +" Radius "+this.radius;
- return text;
- }
- }
- class Bubblesort
- {
- static void sort (Sortierbar []sort)
- {
- boolean getauscht;
- do {
- getauscht = false;
- for (int i = 0; i < sort.length - 1; i++) {
- if (sort[i].istGroesser(sort[i+1]) > 0) {
- Sortierbar temp = sort[i];
- sort[i] = sort[i + 1];
- sort[i + 1] = temp;
- getauscht = true;
- }
- }
- }
- while (getauscht);
- }
- }
- public class Sortieren {
- public static void main(String[] args)
- {
- Kugel[] array;
- array = new Kugel[10];
- array[0] = new Kugel("weiss", 2);
- array[1] = new Kugel("blau", 4);
- array[2] = new Kugel("gelb", 1);
- array[3] = new Kugel("gruen", 10);
- array[4] = new Kugel("rot", 33);
- array[5] = new Kugel("violett", 62);
- array[6] = new Kugel("weiss", 44);
- array[7] = new Kugel("weiss", 55);
- array[8] = new Kugel("weiss", 6);
- array[9] = new Kugel("weiss", 11);
- Bubblesort.sort(array);
- for (int i = 0; i < array.length; i++) {
- System.out.println(array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement