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

Create a class called Pet that contains information about an animal. The class s

ID: 675315 • Letter: C

Question

Create a class called Pet that contains information about an animal. The class should contain the

information:

Name – The name of the pet

Type – A string indicating the type of animal, such as “cat” or “dog”

Weight – The weight of the pet

All of these instance variables should be private. The class should have a constructor with two

parameters to initialize the name and type. The weight should be initialized to -1.0

Create the methods:

setWeightLb – sets the weight to the parameter given in pounds

setWeightKg – sets the weight to the parameter given in kilograms

getWeightLb – returns the weight in pounds

getWeightKg – returns the weight in kilograms

getType – returns the type of the pet

getName – returns the name of the pet

toString – returns a string containing “Name is a Type and weighs Weight kg” where Name,

Type and Weight are the values from the object’s data.

The pet only has one weight. One pound is equal to 0.453592 kilograms and 2.20462 pounds is

equal to one kilogram.

The Pet class should have no main method, no print statements and no file or keyboard read

statements.

Explanation / Answer

public class Pet {

   String name;

   String type;

double weight;

  

   public Pet(String n, String t) {

       name = n;

       type = t;

       weight= -1.0;

   }

  

   public void setWeightLb(double w) {

       weight = w*0.453592;

   }

  

   public void setWeightKg(double w) {

       weight = w;

   }

  

   public double getWeightLb() {

       return weight/0.453592;

   }

  

   public double getWeightKg() {

       return weight;

   }

  

   public String getType() {

       return type;

   }

  

   public String getName() {

       return name;

   }

  

@Override

   public String toString() {

       // TODO Auto-generated method stub

       return name+" is a "+type+" and weighs "+weight+" kg";

   }

}

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