In Java, using Methods. Create a class called Pet that contains information abou
ID: 675344 • Letter: I
Question
In Java, using Methods.
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: setWeightTb - 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. Create the PetTest class shown below in a separate file.Explanation / Answer
//Pet.java
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";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.