Question
transfer this code to GUI java code public abstract class Animal { protected String name; protected int numOfLegs; /** * @return the name */ public
transfer this code to GUI java code
public abstract class Animal {
protected String name;
protected int numOfLegs;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the numOfLegs
*/
public int getNumOfLegs() {
return numOfLegs;
}
/**
* @param numOfLegs the numOfLegs to set
*/
public void setNumOfLegs(int numOfLegs) {
this.numOfLegs = numOfLegs;
}
public abstract void displayAnimal();
}
public interface Carnivore {
public static final String typeC = "";
public static final String foodC = "";
public void displayEatC();
}
public interface Herbivore {
public static final String typeH = "";
public static final String foodH = "";
public void displayEatH();
}
public class Cat extends Animal implements Carnivore{
private String sound;
private String colour;
/**
* @param sound
* @param colour
*/
public Cat(String sound, String colour) {
super();
this.sound = sound;
this.colour = colour;
}
public void displayEatC()
{
System.out.println("Cat likes to eat birds,mice,fish etc. "
+ "-------------------------------------------");
}
public void displayAnimal()
{
System.out.println(" Name: "+name);
System.out.println("Number of Legs: "+numOfLegs);
System.out.println("Sound: "+sound);
System.out.println("Colour: "+colour);
System.out.println(" "+name+" is a Carnivore and Meat eaters/Animal Sources.");
}
}
public class Bear extends Animal implements Herbivore,Carnivore{
private String sound;
private String colour;
/**
* @param sound
* @param colour
*/
public Bear(String sound, String colour) {
super();
this.sound = sound;
this.colour = colour;
}
public void displayEatH()
{
System.out.println(name + " eats both plants and also Meat Eaters/Animal Sources.");
}
public void displayEatC()
{
System.out.println("Bear likes to eat Berries, roots, fungi"
+ ", grasses, fish,"
+ " carrion, small mammals, and insects. "
+ "-------------------------------------------");
}
public void displayAnimal()
{
System.out.println(" Name: "+name);
System.out.println("Number of Legs: "+numOfLegs);
System.out.println("Sound: "+sound);
System.out.println("Colour: "+colour);
System.out.println(" "+name+" is an Omnivore. Omnivore is similar to both Herbivore and Carnivore.");
}
}
public class Elephant extends Animal implements Herbivore{
private String sound;
private String colour;
/**
* @param sound
* @param colour
*/
public Elephant(String sound, String colour) {
super();
this.sound = sound;
this.colour = colour;
}
public void displayEatH()
{
System.out.println("Elephant likes to eat grasses, small plants, bushes, fruit, twigs, tree bark, and roots. "
+ "-------------------------------------------");
}
public void displayAnimal()
{
System.out.println(" Name: "+name);
System.out.println("Number of Legs: "+numOfLegs);
System.out.println("Sound: "+sound);
System.out.println("Colour: "+colour);
System.out.println(" "+name+" is a Herbivore and eats Plants.");
}
}
import java.util.Scanner;
public class TestAnimals {
public static void main(String[] args) {
//Create Animal array
Animal [] animals=new Animal[3];
int animalCount=0;
Scanner sc=new Scanner(System.in);
String name="";
int numLegs=0;
String sound;
String colour;
//Loop for 3 animals
while(animalCount<3)
{
System.out.println("1. Bear 2. Cat 3. Elephant");
System.out.print("Choose the preferred Animal(1-3): ");
int choice=sc.nextInt();
sc.nextLine();
//Get bear data
if(choice==1)
{
System.out.println("You choosed Bear");
System.out.print("Name: ");
name=sc.nextLine();
System.out.print("Number of Legs: ");
numLegs=sc.nextInt();
sc.nextLine();
System.out.print("Sound: ");
sound=sc.nextLine();
System.out.print("Colour: ");
colour=sc.nextLine();
Bear b=new Bear(sound, colour);
b.setName(name);
b.setNumOfLegs(numLegs);
animals[animalCount]=b;
animalCount++;
}
//Get Cat data
else if(choice==2)
{
System.out.println("You choosed Cat");
System.out.print("Name: ");
name=sc.nextLine();
System.out.print("Number of Legs: ");
numLegs=sc.nextInt();
sc.nextLine();
System.out.print("Sound: ");
sound=sc.nextLine();
System.out.print("Colour: ");
colour=sc.nextLine();
Cat c=new Cat(sound, colour);
c.setName(name);
c.setNumOfLegs(numLegs);
animals[animalCount]=c;
animalCount++;
}
//Get elephant data
else if(choice==3)
{
System.out.println("You choosed Elephant");
System.out.print("Name: ");
name=sc.nextLine();
System.out.print("Number of Legs: ");
numLegs=sc.nextInt();
sc.nextLine();
System.out.print("Sound: ");
sound=sc.nextLine();
System.out.print("Colour: ");
colour=sc.nextLine();
Elephant e=new Elephant(sound, colour);
e.setName(name);
e.setNumOfLegs(numLegs);
animals[animalCount]=e;
animalCount++;
}
else
{
System.out.println("Invalid input!!! 1-3 ONLY");
}
}
//Display animals
System.out.println(" ----------LIST OF ANIMALS---------- ");
for(int i=0;i { if(animals[i] instanceof Bear) { Bear b=(Bear) animals[i]; b.displayAnimal(); b.displayEatH(); b.displayEatC(); } else if(animals[i] instanceof Elephant) { Elephant e=(Elephant) animals[i]; e.displayAnimal(); e.displayEatH(); } else if(animals[i] instanceof Cat) { Cat c=(Cat) animals[i]; c.displayAnimal(); c.displayEatC(); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started