Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

B: Programming Create a Pet class. Your class must use encapsulation (informatio

ID: 3919767 • Letter: B

Question

B: Programming Create a Pet class. Your class must use encapsulation (information hiding). Your Pet class is not 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). 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). The birth year must be a constant (i.e., once it is set it must never be changed) Provide a constructor that takes a name, birth year and type of animal as input and initializes the Pet object. The birth year can be given as 1-digit number (2, 4), a 2-digit number (12, 17, 99, etc) or a 4-digit number. You will store each birth year as a 4-digit number though. For example, 2 means 2002, 13 means 2013 and 1978 means 1978. We won't consider the complicating issues of very old pets.

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

Pet.java
------

public class Pet {
private String name;
private int birthYear;
private String type;
private Pet[] children;

public Pet(String name, int year, String type) {
this.name = name;
this.type = type;

if (year < 100) // single or double digit
year += 2000;

this.birthYear = year;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getBirthYear() {
return birthYear;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Pet[] getChildren() {
return children;
}

public void setChildren(Pet[] children) {
this.children = children;
}

}