Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Zettel01;
- public class Animal {
- private String name;
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- private Animal[] animalFood = new Animal[10];
- public void addAnimalFood(Animal animal) {
- for (int i = 0; i < 10; i++) {
- if (animalFood[i] == null) {
- animalFood[i] = animal;
- break;
- }
- }
- }
- public void getAnimalFood() {
- for (int i = 0; i < 10; i++) {
- if (animalFood[i] != null) {
- System.out.println(animalFood[i].getName());
- } else {
- System.out.println(animalFood[i]);
- }
- }
- }
- private Plant[] plantFood = new Plant[10];
- public void addPlantFood(Plant plant) {
- for (int i = 0; i < 10; i++) {
- if (plantFood[i] == null) {
- plantFood[i] = plant;
- break;
- }
- }
- }
- public boolean eatsAnyAnimal() {
- for (int i = 0; i < 10; i++) {
- return animalFood[i] != null;
- }
- return false;
- }
- public boolean eatsAnyPlant() {
- for (int i = 0; i < 10; i++) {
- return plantFood[i] != null;
- }
- return false;
- }
- public void isHerbivore() {
- if (eatsAnyPlant() && !eatsAnyAnimal()) {
- System.out.println(this.name + " is an herbivore.");
- } else {
- System.out.println(this.name + " is not an herbivore.");
- }
- }
- public void isCarnivore() {
- if (eatsAnyAnimal() && !eatsAnyPlant()) {
- System.out.println(this.name + " is an carnivore.");
- }
- else {
- System.out.println(this.name + " is not an carnivore.");
- }
- }
- public void isOmnivore() {
- if (eatsAnyPlant() && eatsAnyAnimal()) {
- System.out.println(this.name + " is an omnivore.");
- } else {
- System.out.println(this.name + " is not an omnivore.");
- }
- }
- }
- package Zettel01;
- public class Main {
- public static void main(String[] args) {
- Animal laohu = new Animal();
- laohu.setName("老虎");
- Animal houzi = new Animal();
- houzi.setName("猴子");
- laohu.addAnimalFood(houzi);
- // Things get printed out here
- laohu.getAnimalFood();
- laohu.isCarnivore();
- // Bio-Test
- System.out.println("-------------BIOTEST-------------");
- Plant gras = new Plant();
- gras.setDescription("Gras ist grün.");
- Plant beeren = new Plant();
- beeren.setDescription("Beeren sind rot.");
- Animal zebra = new Animal();
- zebra.setName("斑马🦓");
- zebra.addPlantFood(gras);
- Animal loewe = new Animal();
- loewe.setName("狮子🦁");
- loewe.addAnimalFood(zebra);
- Animal baer = new Animal();
- baer.setName("熊🐻");
- baer.addPlantFood(beeren);
- Animal fisch = new Animal();
- fisch.setName("鱼🐟");
- Plant alge = new Plant();
- fisch.addPlantFood(alge);
- baer.addAnimalFood(fisch);
- zebra.isHerbivore();
- zebra.isCarnivore();
- zebra.isOmnivore();
- loewe.isHerbivore();
- loewe.isCarnivore();
- loewe.isOmnivore();
- baer.isHerbivore();
- baer.isCarnivore();
- baer.isOmnivore();
- fisch.isHerbivore();
- fisch.isCarnivore();
- fisch.isOmnivore();
- }
- }
Add Comment
Please, Sign In to add comment