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

Use java Write an interface named Pet which declares a two methods: preferredFoo

ID: 3747355 • Letter: U

Question

Use java









Write an interface named Pet which declares a two methods: preferredFood ) which will return a string and howToCall (string name) which also returns a string. You then need to write 4 classes that implement your Pet interface. The name and description of each of these classes is: Dog howTocall() should return "Come here", a space, and then the value of name Dog's preferredFood () method should return the String "Kibble". Cat-- howTocall() should return "Open a can". cat's preferredFood() method should return the String "Meat'". Goat howTocal1() should return the value of name, a space, and then "to me". Goats's preferredFood( method should return the String "EVERYTHING". Fish - -howTocal1() should return null (you cannot call a fish). The Fish class will also need to define a field named wild of type boolean. You will also need to define a getter (named iswild) and setter (named setWild) for the instance variable. Write a constructor for Fish that has a single boolean parameter specifying if the instance is wild. Fish's preferredFood) method wll return the String "Flakes" when wild is false and the method returns "Worms" when wild is true

Explanation / Answer

interface Pet{
String preferredFood();
String howToCall(String name);
}

class Dog implements Pet{
public String howToCall(String name){
return ("Come here "+name);
}
public String preferredFood(){
return "Kibble";
}
}

class Cat implements Pet{
public String howToCall(String name){
return "Open a can";
}
public String preferredFood(){
return "Meat";
}
}

class Goat implements Pet{
public String howToCall(String name){
return (name + " to me");
}
public String preferredFood(){
return "EVERYTHING";
}
}

class Fish implements Pet{
boolean wild;
Fish(boolean wild){
this.wild = wild;
}
public boolean isWild(){
return this.wild;
}
public void setWild(boolean wild){
this.wild = wild;
}
public String howToCall(String name){
return null;
}
public String preferredFood(){
if(this.wild){
return "Worms";
}
else{
return "Flakes";
}
}
}