Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
  2.  
  3. /**
  4. * Write a description of class Rabbit here.
  5. *
  6. * @author (your name)
  7. * @version (a version number or a date)
  8. */
  9. public class Rabbit extends Animal
  10. {
  11. // Characteristics shared by all rabbits (static fields).
  12.  
  13. // The age at which a rabbit can start to breed.
  14. private static final int BREEDING_AGE = 5;
  15. // The age to which a rabbit can live.
  16. private static final int MAX_AGE = 50;
  17. // The likelihood of a rabbit breeding (in percent).
  18. private static final double BREEDING_PROBABILITY = 12;
  19. // The maximum number of births.
  20. private static final int MAX_LITTER_SIZE = 5;
  21.  
  22. // Individual characteristics (instance fields).
  23.  
  24. /**
  25. * Default constructor for testing.
  26. */
  27. public Rabbit()
  28. {
  29. this(true);
  30. }
  31.  
  32. /**
  33. * Create a new rabbit. A rabbit may be created with age
  34. * zero (a new born) or with a random age.
  35. *
  36. * @param randomAge If true, the rabbit will have a random age.
  37. */
  38. public Rabbit(boolean randomAge)
  39. {
  40. super();
  41. if(randomAge) {
  42. setAge(Greenfoot.getRandomNumber(MAX_AGE));
  43. }
  44. }
  45.  
  46. /**
  47. * This is what the rabbit does most of the time - it runs
  48. * around. Sometimes it will breed or die of old age.
  49. */
  50. public void act()
  51. {
  52. incrementAge();
  53. if (isAlive()) {
  54. int births = breed();
  55. for(int b = 0; b < births; b++) {
  56. Location loc = getField().freeAdjacentLocation(getX(), getY());
  57. if (loc != null) {
  58. Rabbit newRabbit = new Rabbit(false);
  59. getField().addObject(newRabbit, loc.getX(), loc.getY());
  60. }
  61. }
  62. Location newLocation = getField().freeAdjacentLocation(getX(), getY());
  63. // Only transfer to the updated field if there was a free location
  64. if(newLocation != null) {
  65. setLocation(newLocation.getX(), newLocation.getY());
  66. }
  67. else {
  68. // can neither move nor stay - overcrowding - all locations taken
  69. setDead();
  70. }
  71. }
  72. }
  73.  
  74. /**
  75. * Increase the age.
  76. * This could result in the rabbit's death.
  77. */
  78. private void incrementAge()
  79. {
  80. setAge(getAge() + 1);
  81. if(getAge() > MAX_AGE) {
  82. setDead();
  83. }
  84. }
  85.  
  86. /**
  87. * Generate a number representing the number of births,
  88. * if it can breed.
  89. * @return The number of births (may be zero).
  90. */
  91. private int breed()
  92. {
  93. int births = 0;
  94. if(canBreed() && Greenfoot.getRandomNumber(100) <= BREEDING_PROBABILITY) {
  95. births = Greenfoot.getRandomNumber(MAX_LITTER_SIZE) + 1;
  96. }
  97. return births;
  98. }
  99.  
  100. /**
  101. * @return A string representation of the rabbit.
  102. */
  103. public String toString()
  104. {
  105. return "Rabbit, age " + getAge();
  106. }
  107.  
  108. /**
  109. * A rabbit can breed if it has reached the breeding age.
  110. */
  111. private boolean canBreed()
  112. {
  113. return getAge() >= BREEDING_AGE;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement