Design a class called Pet (the class file should be called Pet.java), with the f
ID: 3857967 • Letter: D
Question
Design a class called Pet (the class file should be called Pet.java), with the following fields:
Name - The Name field holds the name of the pet Type - The Type field holds the type of animal that the pet is Age - The Age field holds the age of the pet (in years) Height - The Height field holds the height of the pet Weight - The Weight field holds the weight of the pet Legs - The Legs field holds the number of legs of the pet
The Pet class should also have the following methods:
SetName - The setName method stores a value in the Name field GetName - The getName method returns the value of the Name field SetType - The setType method stores a value in the Type field GetType - The getType method returns the value of the Type field SetAge - The setAge method stores a value in the Age field GetAge - The getAge method returns the value of the Age field SetHeight - The setHeight method stores a value in the Height field GetHeight - The getHeight method returns the value of the Height field SetWeight - The setWeight method stores a value in the Weight field GetWeight - The getWeight method returns the value of the Weight field SetLegs - The setLegs method stores a value in the Legs field GetLegs - The getLegs method returns the value of the Legs field PrintStory - The PrintStory method should display/output to the console the following message (inserting values for the class field into the appropriate location1 ): There once was a TYPE named NAME who lived in Dallas, Texas. NAME was a LEGSlegged pet. In 2017, when it was AGE years old, NAME was HEIGHT feet tall and WEIGHT pounds. NAME was a happy TYPE and had long and happy life.
Once you have designed the class, design a program/project/driver class (the program/project/driver class file should be called [YourName]Assignment1; replace [YourName] with your actual name) that creates an object of the class Pet and prompt the user to enter (in a descriptive/user-friendly way2 ) the name, type, age, height, weight, and number of legs, of his or her pet. This data should be stored in the object. Use the object’s accessor methods to retrieve the data from the object (the pet’s name, type, age, and weight) and confirm it (display in on the screen, in a user-friendly way). You will need to add the class Pet to the [YourName]Assignment1 project and add your code to the project/driver class main method.
Explanation / Answer
public class name {
//Passing pet object as a parameter to the method
public static void printStory(Pet pet)
{
//using getters to get the values form the pet class through pet object
System.out.println("There once was a "+pet.getType()+" named "+pet.getName()+" who lived in Dallas, Texas. "+pet.getName()+" was a "+pet.getLegs()+"legged pet. In 2017, when it was "+pet.getAge()+" years old, "+pet.getName()+" was "+pet.getHeight()+" feet tall and "+pet.getWeight()+" pounds. "+pet.getName()+" was a happy "+pet.getType()+" and had long and happy life.");
}
public static void main(String[] args)
{
//initializing a pet object
Pet pet = new Pet();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your pet Name");
pet.setName(sc.nextLine());
System.out.println("Enter your pet Type");
pet.setType(sc.nextLine());
System.out.println("Enter your pet Age");
pet.setAge(Integer.parseInt(sc.nextLine()));
System.out.println("Enter your pet Height");
pet.setHeight(Integer.parseInt(sc.nextLine()));
System.out.println("Enter your pet Weight");
pet.setWeight(Integer.parseInt(sc.nextLine()));
System.out.println("Enter your pets number of legs");
pet.setLegs(Integer.parseInt(sc.nextLine()));
System.out.println("Select option");
System.out.println("A: Print Story");
System.out.println("B: To See your Pet details");
String option = sc.nextLine();
if(option.equals("A"))
{
//passing a pet object to printStory method
printStory(pet);
}else
if(option.equals("B"))
{
System.out.println("your pet Name is "+pet.getName());
System.out.println("your pet Age is "+pet.getAge());
System.out.println("your pet Type is "+pet.getType());
System.out.println("your pet Height is "+pet.getHeight());
System.out.println("your pet Weight is "+pet.getWeight());
System.out.println("your pet has "+pet.getLegs()+" Legs");
}
}
}
public class Pet
{
private String Name;
private String Type;
private int Age;
private int height;
private int weight;
private int legs;
//Getters and Setters for above variables start here
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getLegs() {
return legs;
}
public void setLegs(int legs) {
this.legs = legs;
}
//Constructor for the fields start here
public Pet(String name, String type, int age, int height, int weight, int legs) {
super();
Name = name;
Type = type;
Age = age;
this.height = height;
this.weight = weight;
this.legs = legs;
}
//default constructor
public Pet() {
super();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.