Develop an object oriented program in Java that will keep track of pets treated
ID: 3704285 • Letter: D
Question
Develop an object oriented program in Java that will keep track of pets treated in an animal hospital. Detail class specifications (data members, methods and access modifiers) are described below. Please note the following requirements:
1. Class: Pet
The class should have the following public data members - pet name (a String), owner name (a String), color (a String), and sex (an integer, but it will only hold one of the following four public static final int values: MALE, FEMALE, SPAYED and NEUTERED. You should define these four as static in your class).
Following are the public methods that this class should provide:
void setPetName();
String getPetName();
void setOwnerName();
String getOwnerName();
void setColor();
String getColor();
void setSex(int sexid);
String getSex(); // Should return the string equivalent of the gender, e.g the string “male” etc.
String printPetInfo(); // Should display the name, owner’s name, age, color, and gender (use getSex());
A Sample (preferred) of printPetInfo is as follows:
Spot owned by Mary
Color is Black and White
Sex is Male
2. Class: Cat
This class should extend the Pet class. In addition to the data members and methods inherited from Pet, the Cat class should have a private hairLength data member, which is a string.
Following are the public methods that this class should provide:
Cat (String name, String ownerName, String color, String hairLength); // Constructor
void setHairLength();
String getHairLength(); // returns the string hairLength
String printCatInfo() // Should display the pet as Cat and hair length and a complete description of the cat included in the Pet parent class
A Sample (preferred) of printCatInfo is as follows:
CAT:
Friskies owned by Bob
Color is black
Sex is spayed
Hair is short
3. Class: Dog
This class should extend the Pet class. In addition to the data members and methods inherited from Pet, the Dog class should have a private size data member, which is a string.
Following are the public methods that this class should provide:
Dog (String name, String ownerName, String color, String size); // Constructor
void setSize();
String getSize(); // returns the string size
String printDogInfo(); // Should display the pet as Dog and size and a complete description of the dog included in the Pet parent class
A Sample (preferred) of printDogInfo is as follows:
DOG:
Spot owned by Susan
Color: white
Sex: spayed
Size: medium
Explanation / Answer
public class ABC {
public static void main(String args[]) {
Pet a = new Pet("pat a", "Some x", "Pink", Pet.MALE);
System.out.println(a.printPetInfo());
Cat b = new Cat("cat b", "abc", "brown", "short");
System.out.println(b.printCatInfo());
Dog c = new Dog("dog c", "pqr", "white", "big");
System.out.println(c.printDogInfo());
}
}
class Pet{
public static final int FEMALE = 0;
public static final int MALE = 1;
public static final int SPAYED = 2;
public static final int NEUTERED = 3;
private String petName;
private String ownerName;
private String color;
private int sexid;
public Pet(String petName, String ownerName, String color, int sexid) {
this.petName = petName;
this.ownerName = ownerName;
this.color = color;
this.sexid = sexid;
}
public String getPetName() {
return petName;
}
public void setPetName(String petName) {
this.petName = petName;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSex() {
if (sexid == FEMALE)
return "FEMALE";
else if (sexid == MALE)
return "MALE";
else if (sexid == SPAYED)
return "SPAYED";
else if (sexid == NEUTERED)
return "NEUTERED";
else
return "";
}
public void setSex(int sexid) {
this.sexid = sexid;
}
public String printPetInfo() {
return petName + " owned by " + ownerName + " " +
"Color is " + color + " " +
"Sex is " + getSex();
}
}
class Cat extends Pet {
private String hairLength;
public String getHairLength() {
return hairLength;
}
public void setHairLength(String hairLength) {
this.hairLength = hairLength;
}
public String printCatInfo() {
return super.printPetInfo() + " " +
"Hair Length is " + hairLength;
}
public Cat(String petName, String ownerName, String color, String hairLength) {
super(petName, ownerName, color, Pet.SPAYED);
this.hairLength = hairLength;
}
}
class Dog extends Pet {
private String size;
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String printDogInfo() {
return super.printPetInfo() + " " +
"Size is " + size;
}
public Dog(String petName, String ownerName, String color, String size) {
super(petName, ownerName, color, Pet.SPAYED);
this.size = size;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.