Hi there, can someone help me developing only the class Parrot? Class Bird is provided already in the testing environmen
Posted: Fri Jun 10, 2022 11:58 am
Hi there,
can someone help me developing only the
class Parrot? Class Bird is provided already in the testing
environment. I do not want to reimplement code from Bird in the
Parrot class.
This question concerns a class called Parrot to be
developed as a subclass of the class Bird (click to view
this file), which has already been developed.
Birds have a name,
a description, a favouriteFood 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.
(a) Add the class Parrot in the answer box below, making
it a subclass of Bird.
(b) Add a private instance variable to the class called
talking of type boolean.
(c) Add a public constructor for Parrot whose first
parameter is used to set the parrot's name and whose second
parameter is the parrot's description.
The instance variable talking should be set to
false
(d) Add standard getter and setter methods for talking
called isTalking and setTalking.
(e) Add a public method addPhrase that adds the string
phrase passed as argument to vocabulary. The method does not
return a value.
(f) Add 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:
(g) Add 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 "sunflower" then the method should set
favouriteFood to "sunflower 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 "sunflower seeds" then
the method should reset this to "seeds".
Bird class below:
import java.util.*;
/**
* An abstract class to model a Bird that has
* a name, a description, a favourite food
* and an empty vocabulary.
* @author M250 Module Team
* @version v1.0
*/
public abstract class Bird
{
private String name;
private String description;
private String favouriteFood;
private HashSet<String>
vocabulary;
/**
* @param aName the bird's name
* @param aDescription The bird's
description
*/
public Bird(String aName, String aDescription)
{
name = aName;
description = aDescription;
vocabulary = new HashSet<>();
//empty set
}
/**
* @return the bird's name
*/
public String getName()
{
return name;
}
/**
* Set the bird's name
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* Return a description of the bird
* @return the description
*/
public String getDescription()
{
return description;
}
/**
* Set the bird's description
* @param description the description to
set
*/
public void setDescription(String description)
{
this.description = description;
}
/**
* Return the bird's favourite food
* @return the favouriteFood
*/
public String getFavouriteFood()
{
return favouriteFood;
}
/**
* Return the bird's vocabulary set
* @return vocabulary
*/
public HashSet<String> getVocabulary()
{
return vocabulary;
}
/**
* Set the bird's favourite food
* @param favouriteFood the favouriteFood to
set
*/
public void setFavouriteFood(String
favouriteFood)
{
this.favouriteFood =
favouriteFood;
}
/**
* Tell whether a phrase is in the bird's
vocabulary or not
* @param phrase to search for in
vocabulary
* @return boolean true if phrase is in
vocabulary and false otherwise
*/
public boolean isInVocabulary(String phrase)
{
return
vocabulary.contains(phrase);
}
/**
* String representation of the bird
* @return a String representation of the
bird
*/
public String toString()
{
return " A bird named " + getName()
+ " which has " + getDescription()
+ ", likes to eat " +
getFavouriteFood();
}
/**
* Tell if two birds should be considered the
same
* @return true if the birds are of the same
class and have the same names
*/
public boolean equals(Object o)
{
Bird b = (Bird) o;
if
(getClass().equals(b.getClass()))
{
return
getName().equals(b.getName());
}
return false;
}
}
For example
can someone help me developing only the
class Parrot? Class Bird is provided already in the testing
environment. I do not want to reimplement code from Bird in the
Parrot class.
This question concerns a class called Parrot to be
developed as a subclass of the class Bird (click to view
this file), which has already been developed.
Birds have a name,
a description, a favouriteFood 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.
(a) Add the class Parrot in the answer box below, making
it a subclass of Bird.
(b) Add a private instance variable to the class called
talking of type boolean.
(c) Add a public constructor for Parrot whose first
parameter is used to set the parrot's name and whose second
parameter is the parrot's description.
The instance variable talking should be set to
false
(d) Add standard getter and setter methods for talking
called isTalking and setTalking.
(e) Add a public method addPhrase that adds the string
phrase passed as argument to vocabulary. The method does not
return a value.
(f) Add 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:
(g) Add 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 "sunflower" then the method should set
favouriteFood to "sunflower 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 "sunflower seeds" then
the method should reset this to "seeds".
Bird class below:
import java.util.*;
/**
* An abstract class to model a Bird that has
* a name, a description, a favourite food
* and an empty vocabulary.
* @author M250 Module Team
* @version v1.0
*/
public abstract class Bird
{
private String name;
private String description;
private String favouriteFood;
private HashSet<String>
vocabulary;
/**
* @param aName the bird's name
* @param aDescription The bird's
description
*/
public Bird(String aName, String aDescription)
{
name = aName;
description = aDescription;
vocabulary = new HashSet<>();
//empty set
}
/**
* @return the bird's name
*/
public String getName()
{
return name;
}
/**
* Set the bird's name
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* Return a description of the bird
* @return the description
*/
public String getDescription()
{
return description;
}
/**
* Set the bird's description
* @param description the description to
set
*/
public void setDescription(String description)
{
this.description = description;
}
/**
* Return the bird's favourite food
* @return the favouriteFood
*/
public String getFavouriteFood()
{
return favouriteFood;
}
/**
* Return the bird's vocabulary set
* @return vocabulary
*/
public HashSet<String> getVocabulary()
{
return vocabulary;
}
/**
* Set the bird's favourite food
* @param favouriteFood the favouriteFood to
set
*/
public void setFavouriteFood(String
favouriteFood)
{
this.favouriteFood =
favouriteFood;
}
/**
* Tell whether a phrase is in the bird's
vocabulary or not
* @param phrase to search for in
vocabulary
* @return boolean true if phrase is in
vocabulary and false otherwise
*/
public boolean isInVocabulary(String phrase)
{
return
vocabulary.contains(phrase);
}
/**
* String representation of the bird
* @return a String representation of the
bird
*/
public String toString()
{
return " A bird named " + getName()
+ " which has " + getDescription()
+ ", likes to eat " +
getFavouriteFood();
}
/**
* Tell if two birds should be considered the
same
* @return true if the birds are of the same
class and have the same names
*/
public boolean equals(Object o)
{
Bird b = (Bird) o;
if
(getClass().equals(b.getClass()))
{
return
getName().equals(b.getName());
}
return false;
}
}
For example