Created
January 22, 2017 18:05
-
-
Save Frdnspnzr/8e3a1a670ee27e51d117bb91dead63a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Kaninchen implements Nager { | |
private int ohren = 0; | |
private String name = ""; | |
public Kaninchen(int ohren, String name) { | |
this.ohren = ohren; | |
this.name = name; | |
} | |
@Override | |
public void nagen() { | |
System.out.println("Das Kaninchen mit Ohrenlänge " + ohren + " nagt!"); | |
} | |
public int getOhren() { | |
return ohren; | |
} | |
public String getName() { | |
return this.name; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (o instanceof Kaninchen) { | |
Kaninchen k = (Kaninchen) o; | |
if (k.getOhren() == this.getOhren()) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class KaninchenComparator implements Comparator<Kaninchen> { | |
@Override | |
public int compare(Kaninchen a, Kaninchen b) { | |
int ret = 0; | |
ret = a.getName().compareTo(b.getName()); | |
if (ret == 0) { | |
ret = a.getOhren() - b.getOhren(); | |
} | |
return ret; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Meerschweinchen implements Nager { | |
@Override | |
public void nagen() { | |
System.out.println("Das Meerschweinchen nagt!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Nager { | |
public void nagen(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class NagerListe { | |
public static void main(String[] args) { | |
Kaninchen k1 = new Kaninchen(12,"Klaus"); | |
Kaninchen k2 = new Kaninchen(13,"Sabine"); | |
Kaninchen k3 = new Kaninchen(14,"Eva"); | |
Meerschweinchen m1 = new Meerschweinchen(); | |
Meerschweinchen m2 = new Meerschweinchen(); | |
List<Nager> l1 = new ArrayList<Nager>(); | |
l1.add(k1); | |
l1.add(k2); | |
l1.add(m1); | |
List<Nager> l2 = new ArrayList<Nager>(); | |
l2.add(k3); | |
l2.add(m2); | |
Map<String,List<Nager>> nager = new HashMap<String,List<Nager>>(); | |
nager.put("Max",l1); | |
nager.put("Jule",l2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment