Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. interface Sortierbar
  2. {
  3.     public int istGroesser(Sortierbar element);
  4.  
  5. }
  6. abstract class Figur
  7. {
  8.     protected String farbe;
  9.     protected int radius;
  10.  
  11.     public Figur(String f, int r)
  12.     {
  13.         this.farbe=f;
  14.         this.radius=r;
  15.     }
  16.     public abstract String toString();
  17. }
  18.  
  19. class Kugel extends Figur implements Sortierbar
  20. {
  21.  
  22.     public Kugel(String f, int r)
  23.     {
  24.         super(f,r);
  25.     }
  26.     public int istGroesser(Sortierbar k) {
  27.         Kugel k1 = (Kugel)k;
  28.         if (k1.radius < this.radius) {
  29.             return -1;
  30.         }
  31.         if (k1.radius == this.radius) {
  32.             return 0;
  33.         }
  34.         else
  35.         {
  36.             return 1;
  37.         }
  38.     }
  39.  
  40.  
  41.     public String toString()
  42.     {
  43.         String text = "";
  44.         text = "Kugel: Farbe: "+this.farbe +" Radius "+this.radius;
  45.         return text;
  46.     }
  47.  
  48. }
  49. class Bubblesort
  50. {
  51.     static void sort (Sortierbar []sort)
  52.     {
  53.         boolean getauscht;
  54.  
  55.     do {
  56.         getauscht = false;
  57.         for (int i = 0; i < sort.length - 1; i++) {
  58.             if (sort[i].istGroesser(sort[i+1]) > 0) {
  59.                 Sortierbar temp = sort[i];
  60.                 sort[i] = sort[i + 1];
  61.                 sort[i + 1] = temp;
  62.                 getauscht = true;
  63.             }
  64.         }
  65.     }
  66.     while (getauscht);
  67. }
  68. }
  69.  
  70. public class Sortieren {
  71.     public static void main(String[] args)
  72.  
  73.     {
  74.  
  75.         Kugel[] array;
  76.         array = new Kugel[10];
  77.         array[0] = new Kugel("weiss", 2);
  78.         array[1] = new Kugel("blau", 4);
  79.         array[2] = new Kugel("gelb", 1);
  80.         array[3] = new Kugel("gruen", 10);
  81.         array[4] = new Kugel("rot", 33);
  82.         array[5] = new Kugel("violett", 62);
  83.         array[6] = new Kugel("weiss", 44);
  84.         array[7] = new Kugel("weiss", 55);
  85.         array[8] = new Kugel("weiss", 6);
  86.         array[9] = new Kugel("weiss", 11);
  87.  
  88.         Bubblesort.sort(array);
  89.         for (int i = 0; i < array.length; i++) {
  90.             System.out.println(array[i]);
  91.  
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement