Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. The Parrot class represents a parrot with an age in years and the ability to learn sounds which it can repeat back when asked to speak. The declaration of the Parrot class is shown below.
  2. public class Parrot
  3. {
  4.  
  5. /** Constructs a new Parrot object */
  6. public Parrot(String name)
  7. { /* implementation not shown */ }
  8.  
  9. /** @return the age of the parrot in years */
  10. public int getAge()
  11. { /* implementation not shown */ }
  12.  
  13. /** Adds sound to the list of sounds the parrot can make
  14. * @param sound the sound to add */
  15. public void train(String sound)
  16. { /* implementation not shown */ }
  17.  
  18. /** @return a random sound that the parrot can make */
  19. public String speak()
  20. { /* implementation not shown */ }
  21.  
  22. // There may be instance variables, constructors, and methods that are not shown.
  23. }
  24.  
  25.  
  26.  
  27.  
  28. A pirate parrot is a type of parrot. A pirate parrot knows how to make the sound “Polly want a cracker” immediately upon birth. A pirate parrot can also steal souls whose age becomes part of the pirate parrot’s age. A pirate parrot is represented by thePirateParrot class, which you will write.
  29. Assume that the following code segment appears in a class other than PirateParrot. The code segment shows an example of using the PirateParrot class.
  30. PirateParrot polly = new PirateParrot("Polly");
  31.  
  32. System.out.println(polly.getAge()); // prints 0
  33. /* code to increase Polly's age by 5 years */
  34. System.out.println(polly.getAge()); // prints 5
  35.  
  36. polly.stealSoul(5);
  37. polly.stealSoul(10);
  38. System.out.println(polly.getAge()); // prints 20
  39.  
  40. polly.train("Walk the plank");
  41. polly.train("Off with his head");
  42.  
  43. // Polly retires from his life as a pirate to a cushy life as a pet
  44.  
  45. Parrot myPetPolly = polly;
  46. System.out.println(myPetPolly.getAge()); // prints 20
  47. myPetPolly.train("Time for bed");
  48. System.out.println(myPetPolly.speak());
  49. /* prints one of the following, chosen at random:
  50. * Polly want a cracker
  51. * Walk the plank
  52. * Off with his head
  53. * Time for bed
  54. */
  55.  
  56.  
  57. Write the PirateParrot class. Your code must produce the indicated results when invoked by the code given above.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement