Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am a little lost and need help with my java code. create five user-defined data types. They are the abstract super class Animal and

I am a little lost and need help with my java code.

create five user-defined data types. They are the abstract super class Animal and the sub classes Sheep, Dragon, Spider, and Horse. The Animal class will have a constructor and the following class members. Methods abstract String eats( ); // will return a String with the food the animal eats abstract String whatType( ); // will return a String with the type of animal it is toString( ); // will show the member instance values of the object You will override the three methods above in each subclass and you will invoke the superclass default constructor in each subclass constructor via super( ). Please use the @Override annotation where appropriate. Instance Variables. String name int age The Animal class will also have appropriate accessor and mutator methods for these two variables. int static animalCount The Animal class will have an appropriate accessor method for this variable. This variable will increment by one (using the Animal constructor) every time an object that is a subclass of Animal is instantiated. Appropriate access modifiers will be assigned to all class members. You will clone one sheep. That is, you will create an additional constructor in the Sheep class that will take all the information from the first sheep and use this information to instantiate a second sheep, making a deep copy of a Sheep object. You will write an application (Zoo.java) that will create an object of each of the zoos four animals (and a sheep clone). Within the application you will create an array of type Animal and place each of the animals in the array. Then using a for loop and the Animals static variable animalCount, output to the screen each animals name, type, age, and diet using the overridden toString( ) method within each subclass.

This is what I have so far. I have only done one animal so far to make sure I get it right before continueing with the rest.

public abstract class Animal { private String name; private int age; private static int animalCount = 0;

public Animal(String name, String whatType, int age) { this.name = name; this.age = age; animalCount++; }

public String toSZtring() { return "This is a " + type; }

public abstract void eats(); }

public class Dragon extends Animal { private String name; private String whatType; int age; public Dragon(String aName) { super("Dragon"); name = aName int age = 0 whatType = "Unknown" } @Override public String toString() { return super.toString() + " It's " + name + "the" + age + "year old" + whatType; } @Override public void eats() { System.out.println("I eat Peanut Butter"); } }

public class Zoo { public class static void main(String)[] args) { theAnimals = new Animal[5]; theAnimals[0] = new Dragon("Smaug", 500); theAnimals[1] = new Spider("Charlotte", 1); theAnimals[2] = new Horse("ThePiebald", 7); theAnimals[3] = new Sheep("Dolly", 2); theAnimals[4] = new Sheep(new Sheep("Dolly", 2); for(Animal animal.theAnimals.length); { System.out.println(animal); } System.out.println("Welcome to the Zoo! We have " + Animal.getAnimalCount() + "in our Zoo!"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions