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

1. Part A Create a class called Pet. There should be 4 instance variables, the n

ID: 3650306 • Letter: 1

Question

1. Part A Create a class called Pet. There should be 4 instance variables, the name (a String), the animal (a String: dog, cat, parrot,... ), the number of legs (an int), and the gender (an int: 1 for male, 2 for female).

Write a set method for each instance variable.
Write a method showInfo that prints all four instance variables, labeled, to make sure they are being properly set.

Write a test application, called PetTest. It should create a Pet, have the Pet call all four get methods, then call the showInfo method to make sure all is working.

A sample output at this point might be:

name: Toto
animal: dog
number of legs: 4
gender: 1
Part B

Once part A is working, proceed.

In Pet, write get methods for each instance variable.

Modify PetTest to call each of the get methods and print the results. You can either assign the return value to a variable in PetTest and print that variable as in this example:



We would like to improve showInfo to print the actual gender. Add some if statements to showInfo (in Pet) to print "male" instead of 1 and "female" instead of 2. So the output for showInfo might now be

name: Toto
animal: dog
number of legs: 4
gender: male
Part D - final version.

Modify PetTest to call the various set and get functions and showInfo to produce the following output.

This is about a male dog named Toto.
Toto has 4 legs.

Summary
name: Toto
animal: dog
number of legs: 4
gender: male
After using the values "Toto", "dog", 4, 1 in set functions, the rest of PetTest should never again use these literals directly. Instead they should come from get methods and showInfo.

Explanation / Answer

please rate - thanks

import java.util.*;
public class PetTester {
public static void main (String[] args)
{Scanner in=new Scanner(System.in);
Pet p =new Pet();
String sIn;
int i;
System.out.print("Enter pet name: ");
sIn=in.nextLine();
p.setName(sIn);
System.out.print("Enter animal type: ");
sIn=in.nextLine();
p.setAnimal(sIn);
System.out.print("Enter number of legs: ");
i=in.nextInt();
p.setLegs(i);
System.out.print("Enter gender(1-male, 2-female): ");
i=in.nextInt();
p.setGender(i);
showInfo(p);

}
public static void showInfo(Pet pet)
{System.out.print("This is about a ");
if(pet.getGender()==1)
     System.out.print("male ");
else
     System.out.print("female ");
System.out.println(pet.getAnimal()+" names "+pet.getName()+".");
System.out.println(pet.getName()+" has "+pet.getLegs()+" legs.");
System.out.println(" Summary");
   

System.out.println ("Name: " + pet.getName());
System.out.println("Animal: "+pet.getAnimal());
System.out.println ("legs: " + pet.getLegs());
if(pet.getGender()==1)
     System.out.println ("gender: male");
else
     System.out.println ("gender: female");
   
    }

}

------------------------------------------------------


public class Pet
{
private String name;
private String animal;
private int legs;
private int gender;

public Pet ()
{
}


public void setName (String n)
{
name = n;
}
public String getName ()
{return name;
}


public void setAnimal (String n)
{
animal = n;
}
public String getAnimal ()
{return animal;
}

public void setLegs (int l)
{legs=l;
}
public int getLegs ()
{return legs;
}

public void setGender (int g)
{gender=g;
}
public int getGender ()
{return gender;
}

}