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

Write code for a Pet class. Part of this is shown (including the getAge method).

ID: 3844662 • Letter: W

Question

Write code for a Pet class. Part of this is shown (including the getAge method). Fill in other blanks. Class Pet {Pet private String name;//name of pet private String breed;//type of pet (dog, cat,//etc) private int age;//age of pet in years private//; must be > = 0 private float weight;//min weight//must be > = 0.5 int getAge () {return age;}//methods for the Pet class that you will need//to fill in go here... a) write a constructor method for the Pet class, to set the default name to "Fido", the default breed to "spider", the default age to 1, and the default weight to 0.5. Show the method header clearly. b) Fill in the set() method for the Pet class, with the given header; set name to newName, breed to newBreed, age to newAge, weight to newWeight. You must check that newAge is > = 0, and weight > =0.5. If either newAge or newWeight is not in the correct range, print an error message and return immediately, without changing any of the member variables of the Pet object. void set (String newName string new Breed, int newAge, float. new Weight) {

Explanation / Answer

Question a:

Answer:

Pet() {
this.name = "Fido";
this.breed = "spider";
this.age = 1;
this.weight = (float) 0.5; // conveted to float
}

Question b:

Answer:

void set(String newName, String newBreed, int newAge, float newWeight) {
if (newAge < 0) {
System.out.println("Age must be greater then equal to 0");
} else if (newWeight < 0.5) {
System.out.println("Weight must be greater then equal to 0.5");
} else {
this.name = newName;
this.breed = newBreed;
this.age = newAge;
this.weight = newWeight;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote