Question
I need help fixing some errors in my java code. I am getting error: return outside method return This is a + whatType; error:
I need help fixing some errors in my java code.
I am getting error: return outside method return "This is a " + whatType;
error: cannot reference name before supertype constructor has been called super(name, age);
abstract method toString() in Animal cannot be accessed directly return super.toString() + " It's " + name + "the" + age + "year old" + whatType;
public abstract class Animal { private String name; private int age; private static int animalCount = 0;
public Animal(String name, int age) { this.name = name; this.age = age; animalCount++; } public String getName() { return name; }
public int getAge() { return age; }
public static int getAnimalCount() { return animalCount; }
@Override public abstract String toString(); { return "This is a " + whatType; }
public abstract void eats(); }
public class Dragon extends Animal {
private String name; private String whatType; int age;
public Dragon(String aName) { super(name, age); whatType = "Dragon"; }
@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 Spider extends Animal { public Spider(String name, int age) { super(name, age); whatType = "Spider" }
@Override public String toString() { return super.toString() + " It's " + name + "the" + age + "year old" + whatType; }
@Override public void eats() { System.out.println("I eat insects"); }
}
public class Horse extends Animal { public Horse(String name, int age) { super(name, age); whatType = "Horse"; }
@Override public String toString() { return super.toString() + " It's " + name + "the" + age + "year old" + whatType; }
@Override public void eats() { System.out.println("I eat Apples"); }
}
public class Sheep extends Animal {
private String name; private String whatType; int age;
public Sheep(String aName) { super(name, age); whatType = "Sheep"; }
@Override public String toString() { return super.toString() + " It's " + name + "the" + age + "year old" + whatType; }
@Override public void eats() { System.out.println("I eat grass"); } }
public class Zoo { public static void main(String)[] args) {
Animal[] animals = new Animal[5];
animals[0] = new Dragon("Smaug", 500); animals[1] = new Spider("Charlotte", 1); animals[2] = new Horse("ThePiebald", 7); animals[3] = new Sheep("Dolly", 2); animals[4] = new Sheep(animals[3]);
for(Animal animal : animals) { System.out.println(animal); }
System.out.println("Welcome to the Zoo! We have " + Animal.getAnimalCount() + " animals in our Zoo!"); }
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.
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