Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1.  
  2. public class Aufgabe3 {
  3.  
  4. public static void main(String[] args) {
  5. Bauernhof bauernhof = new Bauernhof();
  6. Tier kuh = new Tier(720f, "Kuh", 4);
  7. Tier spinne = new Tier(0.5f, "Spinne", 8);
  8. Tier schlange = new Tier(.1f, "Schlange", 0);
  9.  
  10. bauernhof.tierHinzufuegen(kuh);
  11. bauernhof.tierHinzufuegen(spinne);
  12. bauernhof.tierHinzufuegen(schlange);
  13.  
  14. System.out.println(bauernhof.ausgeben());
  15. System.out.println(bauernhof.anzahlBeine());
  16. bauernhof.fuettern();
  17. }
  18. }
  19.  
  20. class Bauernhof {
  21. public int j = 0;
  22. public Tier[] stall;
  23.  
  24. public Bauernhof() {
  25. Tier[] stall = new Tier[10];
  26. stall[0] = new Tier(1.6f, "Ente", 2);
  27. }
  28.  
  29. public void tierHinzufuegen(Tier tier) {
  30. for (int i = 0; i < 10; i++) {
  31. if (stall[i] == null) {
  32. stall[i] = tier;
  33. j = i;
  34. i = 10;
  35. } else if (i == 9) {
  36. System.out.println("Stall ist voll!");
  37. }
  38. }
  39. }
  40.  
  41. public void fuettern() {
  42. int i = 0;
  43. while(i <= j)
  44. stall[i].setGewicht(stall[++i].getGewicht() * 1.1);
  45. }
  46.  
  47. public String ausgeben() {
  48. String ergebnis = "Liste:\n";
  49. for (int i = 0; i < stall.length; i++) {
  50. ergebnis = ergebnis + "Platz: " + i + " Status-> " + stall[i].ausgeben() + "\n";
  51. }
  52. return ergebnis;
  53. }
  54.  
  55. public int anzahlBeine() {
  56. int anazhlBeine = 0;
  57. for (int i = 0; i < 10; i++)
  58. anazhlBeine += stall[i].getAnzahlBeine();
  59.  
  60. return anazhlBeine;
  61. }
  62. }
  63.  
  64. class Tier {
  65. private float gewicht;
  66. private String bezeichnung;
  67. private int anzahlBeine;
  68.  
  69. public Tier(String bezeichnung) {
  70. this.bezeichnung = bezeichnung;
  71. this.anzahlBeine = 0;
  72. this.gewicht = 0;
  73. }
  74.  
  75. public Tier(float gewicht, String gattung, int anzahlBeine) {
  76. this.gewicht = gewicht;
  77. this.bezeichnung = gattung;
  78. this.anzahlBeine = anzahlBeine;
  79. }
  80.  
  81. public String ausgeben() {
  82. return "Gattung=" + bezeichnung + " Beine=" + anzahlBeine + " Gewicht=" + gewicht;
  83. }
  84.  
  85. public String getGattung() {
  86. return bezeichnung;
  87. }
  88.  
  89. public int getAnzahlBeine() {
  90. return anzahlBeine;
  91. }
  92.  
  93. public float getGewicht() {
  94. return gewicht;
  95. }
  96.  
  97. public void setGewicht(double gewicht) {
  98. this.gewicht = (float) gewicht;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement