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

C: Programming Create a Java program UsePet. Your program must have a method tha

ID: 3588326 • Letter: C

Question

C: Programming Create a Java program UsePet. Your program must have a method that takes a Pet object and a String as input and returns true if the pet has a child with the name as given by the string, and false otherwise. Your program must create the following Pet object: Fluffy, a cat, who was born in 2012. She has two children: Igor, a cat, who was born in 2013 (who has no children) and Wei, an owl (who was adopted), who was born in 2014 (who has no children). Your program should then ask for user input (using a Scanner object) for a name. Using your method from above), check if Fluffy has a child with the given name and output an appropriate message. For example, if the user input was "Goran", the output should be one of yes, Fluffy has a child named Goran. or no, Fluffy does not have a child named Goran Do not hardcode "fluffy" in the output message. It should print whatever name the Pet obiect in vour program has. Your program should work correctly no matter what Pet obiect we initially create. Do not hardcode your solution for the Fluffy pet. Do not create any UsePet objects in your program Potentially Useful Things java.util.Scanner has a next) method that returns the next String token (word) from a given scanner object.

Explanation / Answer

Program UsePet.java

which interact with user to enter the pet name and pet child name to see if the Fluffy has children and out the answer accordingly

public class UsePet {


public static boolean hasChild(Pet pet, String name){

if(pet.getPetName().equalsIgnoreCase(name))
{ return true;}
else
{return false;}
}
public static void main(String[] args) {
String pet_name, child_name;
  
boolean has_child;
Pet pet1 = new Pet("cat", "Igor", "2013", "fluffy");
Pet pet2 = new Pet("owl", "wei", "2014", "fluffy");//String petCategory, String petName, String petDob
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the your pet cat :");
pet_name = in.nextLine();
  
System.out.println("Enter the child name :");
child_name = in.next();
if((hasChild(pet1, child_name)) || (hasChild(pet2, child_name)))
{
System.out.println("yes,"+pet1.getPetParentName() + " has child named "+ child_name);
}else{
System.out.println("no,"+pet1.getPetParentName() + " does not have a child named "+ child_name);
}
  

}
}

Program Pet.java

A simple Pojo[bean - plain old java object] object that keeps pets information

public class Pet {
private String petCategory;
private String petName;
private String petDob;
private String petParentName;

public String getPetParentName() {
return petParentName;
}

public void setPetParentName(String petParentName) {
this.petParentName = petParentName;
}


public String getPetCategory() {
return petCategory;
}

public void setPetCategory(String petCategory) {
this.petCategory = petCategory;
}

public String getPetName() {
return petName;
}

public void setPetName(String petName) {
this.petName = petName;
}

public String getPetDob() {
return petDob;
}

public void setPetDob(String petDob) {
this.petDob = petDob;
}

public Pet(String petCategory, String petName, String petDob, String petParentName) {
this.petCategory = petCategory;
this.petName = petName;
this.petDob = petDob;
this.petParentName = petParentName;
}
  
}

Out put as follows

run:
Enter the name of the your pet cat :
fluffy
Enter the child name :
wei
yes,fluffy has child named wei
BUILD SUCCESSFUL (total time: 5 seconds)

// Execution instruction :: keep above two classes in same package to test

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote