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

I need help writing this program in Java. Design a class named Pet, which should

ID: 3596476 • Letter: I

Question

I need help writing this program in Java.

Design a class named Pet, which should have the following fields:

• name: The name field holds the name of a pet.

• type: The type field holds the type of animal that a pet is. Example values are “Dog”, “Cat”, and “Bird”.

• age: The age field holds the pet’s age.

The Pet class should also have the following methods:

• setName: The setName method stores a value in the name field.

• setType: The setType method stores a value in the type field.

• setAge: The setAge method stores a value in the age field.

• getName: The getName method returns the value of the name field.

• gettype: The getType method returns the value of the type field.

• getAge: The getAge method returns the value of the age field.

Once you have designed the class, design a driver program that creates an object of the class and prompts the user to enter the name, type, and age of his or her pet. This data should be stored in the object. Use the object’s accessor methods to retrieve the pet’s name, type, and age and display this data on the screen

Explanation / Answer

PetTest.java

import java.util.Scanner;

public class PetTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the per name: ");

String name = scan.next();

System.out.println("Enter the pet type: ");

String type = scan.next();

System.out.println("Enter the pet age: ");

int age = scan.nextInt();

Pet p = new Pet();

p.setAge(age);

p.setName(name);

p.setType(type);

System.out.println("Pet Name: "+p.getName());

System.out.println("Pet Type: "+p.getType());

System.out.println("Pet Age: "+p.getAge());

}

}

Pet.java

public class Pet {

private String type, name;

private int age;

public Pet() {

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

Output:

Enter the per name:
Puppy
Enter the pet type:
Dog
Enter the pet age:
4
Pet Name: Puppy
Pet Type: Dog
Pet Age: 4