C
ComparableI
Gast
Hi,
geben sind die folgenden Klassen:
und
Man soll nun beide Klassen so verändern, dass eine Liste von
mit
sortiert werden und zwar einmal aufsteigend nach dem
des Berufs eines Menschen und einmal absteigend nach dem
des Menschen.
Mein Versuch:
Mein Problem ist nun folgendes. Wenn ich eine Testklasse schreibe und
aufrufe, wird nur nach
sortiert.
Das ist mein Code und die Ausgabe unten.
Ausgabe:
Frage: Wie bekomme ich denn eine Sortierung aufsteigend nach
nun hin, so wie es verlangt ist? Ich darf nur das Interface
verwenden.
geben sind die folgenden Klassen:
Java:
public class Human {
public String name;
public int weight;
public Occupation occupation;
}
und
Java:
public class Occupation {
public String name;
public double salary;
}
Man soll nun beide Klassen so verändern, dass eine Liste von
Code:
Human
Code:
Collections.sort()
Code:
salary
Code:
weight
Mein Versuch:
Java:
public class Occupation implements Comparable<Occupation> {
public String name;
public double salary;
@Override
public int compareTo(Occupation o) {
if (o == null)
throw new NullPointerException("Passed argument is null.");
if (this.salary == o.salary)
return 0;
else if (this.salary < o.salary)
return -1;
else
return 1;
}
}
Java:
public class Human implements Comparable<Human> {
public String name;
public int weight;
public Occupation occupation;
@Override
public int compareTo(Human human) {
if (human == null)
throw new NullPointerException("Nullobjekt übergeben.");
if (this.weight == human.weight)
return 0;
else if (this.weight > human.weight)
return -1;
else
return 1;
}
}
Mein Problem ist nun folgendes. Wenn ich eine Testklasse schreibe und
Code:
Collections.sort
Code:
size
Das ist mein Code und die Ausgabe unten.
Java:
package klausur;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HumanTest {
public static void main(String[] argv) {
List<Human> humans = new ArrayList<Human>();
Human human = new Human();
human.name = "Eike";
Occupation occupation = new Occupation();
occupation.name = "Beruf 1";
occupation.salary = 45000;
human.occupation = occupation;
human.weight = 88;
humans.add(human);
human = new Human();
human.name = "Dirk";
occupation = new Occupation();
occupation.name = "Beruf 2";
occupation.salary = 35000;
human.occupation = occupation;
human.weight = 72;
humans.add(human);
human = new Human();
human.name = "Andrea";
occupation = new Occupation();
occupation.name = "Beruf 3";
occupation.salary = 40000;
human.occupation = occupation;
human.weight = 59;
humans.add(human);
human = new Human();
human.name = "Arne";
occupation = new Occupation();
occupation.name = "Beruf 4";
occupation.salary = 82000;
human.occupation = occupation;
human.weight = 88;
humans.add(human);
human = new Human();
human.name = "Minka";
occupation = new Occupation();
occupation.name = "Beruf 5";
occupation.salary = 54000;
human.occupation = occupation;
human.weight = 88;
humans.add(human);
for (Human p : humans)
System.out.println(p.name + "\t" + p.occupation.name + "\t"
+ p.occupation.salary + "\t" + p.weight);
Collections.sort(humans);
System.out.println("\nNun sortiert:\n");
for (Human p : humans)
System.out.println(p.name + "\t" + p.occupation.name + "\t"
+ p.occupation.salary + "\t" + p.weight);
}
}
Ausgabe:
Eike Beruf 1 45000.0 88
Dirk Beruf 2 35000.0 72
Andrea Beruf 3 40000.0 59
Arne Beruf 4 82000.0 88
Minka Beruf 5 54000.0 88
Nun sortiert:
Eike Beruf 1 45000.0 88
Arne Beruf 4 82000.0 88
Minka Beruf 5 54000.0 88
Dirk Beruf 2 35000.0 72
Andrea Beruf 3 40000.0 59
Frage: Wie bekomme ich denn eine Sortierung aufsteigend nach
Code:
salary
Code:
Comparable