In: Computer Science
This exercise requires designing a program which solves the problem described in the problem statement below. Provide comments in your pseudo-code and Java program as necessary.
Your solution must include these components:
Problem Statement
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 hold the type of animal that a pet is (for example, “dog”, “cat”, “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 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.
Expected Output
Your results should be similar to the following:
Please enter the name of your pet: Maestro
Please enter the type of your pet: dog
Please enter the age of your pet: 8
The name of your pet is Maestro.
Maestro is a dog.
Maestro is 8 years old.
Submission
Submit your assignment as a Microsoft Word document (.docx) with all components in the one document. Once submitted, the answer will be revealed.
Grading
This practice problem is NOT graded. However, you must complete the practice problem and survey before your graded programming assignment problem will be revealed.
Solution:
Pseudocode:
code:
import java.util.Scanner;
public class Pet {
private String name; //name of the pet
private String type; //type of the pet
private int age; //age of the pet
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args)
{
String name;
String type;
int age;
Pet pt= new Pet(); //creating the
object of the pet class
Scanner sc= new
Scanner(System.in);
System.out.println("Please enter
the name of your pet: "); //Taking the input from the user
name= sc.nextLine();
System.out.println("Please enter
the type of your pet: ");
type= sc.nextLine();
System.out.println("Please enter
the age of your pet: ");
age= sc.nextInt();
pt.setName(name); //calling setters
to set the input
pt.setType(type);
pt.setAge(age);
System.out.println("The name of
your pet is: " + pt.getName()); //using the getter to show the
output
System.out.println(pt.getName() + "
is a " + pt.getType());
System.out.println(pt.getName()+"
is " + pt.getAge() + " years old");
}
}
5)
Output:
Please enter the name of your pet:
Maestro
Please enter the type of your pet:
dog
Please enter the age of your pet:
8
The name of your pet is: Maestro
Maestro is a dog
Maestro is 8 years old
Hit the thumbs up if you liked the answer. :)