B: Programming Create a Pet class. Your class must use encapsulation (informatio
ID: 3588325 • Letter: B
Question
B: Programming Create a Pet class. Your class must use encapsulation (information hiding). Your Pet class must not be a program. The state and behaviour of the class (and objects) is described as follows: A Pet object will have the following attributes: a name (String), birth year (int), type of pet (String such as "cat", "dog", "eel"), and a list of its children (array of Pet objects). A pet's birth year should never be able to be changed after it is set in the constructor Provide a constructor that takes a name, birth year and type of animal as input and initializes the Pet object. Each attribute should have a getter method. Provide a setter method only for the list of children (that should take an entire array of Pet objects as input) Add a method that returns the number of children a pet has. The method should be called numberOf Children.Explanation / Answer
/*******************************Pet.java*******************************/
/**
* The Class Pet.
*/
public class Pet {
/** The name. */
private String name;
/** The birth year. */
private int birthYear;
/** The type of pet. */
private String typeOfPet;
/** The pets. */
private Pet[] pets;
/**
* Instantiates a new pet.
*
* @param name
* the name
* @param birthYear
* the birth year
* @param typeOfPet
* the type of pet
*/
public Pet(String name, int birthYear, String typeOfPet) {
super();
this.name = name;
this.birthYear = birthYear;
this.typeOfPet = typeOfPet;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets the birth year.
*
* @return the birthYear
*/
public int getBirthYear() {
return birthYear;
}
/**
* Gets the type of pet.
*
* @return the typeOfPet
*/
public String getTypeOfPet() {
return typeOfPet;
}
/**
* Gets the pets.
*
* @return the pets
*/
public Pet[] getPets() {
return pets;
}
/**
* Sets the pets.
*
* @param pets
* the pets to set
*/
public void setPets(Pet[] pets) {
this.pets = pets;
}
/**
* Number of childern.
*
* @return the int
*/
public int numberOfChildern() {
if (this.pets != null) {
return this.pets.length;
}
return 0;
}
}
/***********************PetDriver.java**************************/
/**
* The Class PetDriver.
*/
public class PetDriver {
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
Pet obj = new Pet("Tom", 2009, "cat");
Pet obj1 = new Pet("Jerry", 2016, "cat");
Pet[] pets = new Pet[1];
pets[0] = obj1;
obj.setPets(pets);
Pet obj2 = new Pet("TamTam", 2017, "dog");
Pet obj3 = new Pet("amb", 2010, "eel");
Pet obj4 = new Pet("kdk", 2017, "eel");
Pet[] eels = new Pet[1];
eels[0] = obj4;
obj3.setPets(eels);
System.out.println("Displaying Information");
System.out.println(
obj2.getName() + ":" + obj2.getBirthYear() + ":" + obj2.getTypeOfPet() + ":" + obj2.numberOfChildern());
System.out.println(
obj.getName() + ":" + obj.getBirthYear() + ":" + obj.getTypeOfPet() + ":" + obj.numberOfChildern());
System.out.println(
obj3.getName() + ":" + obj3.getBirthYear() + ":" + obj3.getTypeOfPet() + ":" + obj3.numberOfChildern());
}
}
/***************************************output************************************************/
Displaying Information
TamTam:2017:dog:0
Tom:2009:cat:1
amb:2010:eel:1
Thanks a lot. Please let me know if you have any doubt.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.