Class called Parrotto be developed as a subclass of the class Bird (click to view this file),
Question:
Class called Parrotto be developed as a subclass of the class Bird (click to view this file), which has already been developed. Birds have aname, adescription,afavouriteFood and a vocabulary. By default a bird's vocabulary does not contain any phrases. Parrots are birds that may have some phrases in their vocabulary, and they may be talking or not talking.
only the class Parrot in this part. Class Bird is provided already in the testing environment. Do not reimplement code from Bird in the Parrot class.
(a) the class Parrot in the answer box below, making it a subclass of Bird.
(b) a private instance variable to the class called talking of type boolean.
(c) a public constructor for Parrot whose first parameter is used to set the parrot's nameand whose second parameter is the parrot's description.
The instance variable talking should be set to false
(d) standard getter and setter methods for talking called isTalking and setTalking.
a9bbf03aecbb45f0b38f3d2edb264776
(e) a public method addPhrase that adds the string phrase passed as argument to vocabulary. The method does not return a value.
(f) a public method sayPhrase that does not return a value and takes a string argument representing a phrase the parrot might say. It should have the following effects depending on the value of talking and whether the phrase is in vocabulary or not:
- If the parrot is talking and the phrase is in the parrot's vocabulary, the phrase is printed.
- If the parrot is talking but doesn't know the phrase then the phrase is added to the parrot's vocabulary and "Don't know that" is printed.
- If the parrot is not talking then "Not talking now" is printed.
(g) a public method updateFavouriteFood which takes a String argument and does not return a value. The method should prefix the parrot's favourite food with the String passed as argument and a space.
E.g. If favouriteFood is currently set to "seeds" and the argument passed is "millet" then the method should set favouriteFood to "millet seeds".
(h) Override the toString method inherited from Bird to add the text:
" and is talking"or " and is not talking" depending on the value of talking.
(i) Add a public method resetFavouriteFood that takes no argument and does not return a value. It should reset the parrot's favourite food to the characters after the first space if any in favouriteFood. If there is no space the favourite food should be unchanged.
E.g. if the favourite food is currently "millet seeds" then the method should reset this to "seeds".
For example:
Test | Result |
---|---|
//(d) isTalking Parrot p39 = new Parrot("Fred", "feisty"); System.out.println(p39.isTalking()); | false |