I need help with the following:
1. Re-Write AbstractTest as LoopTest.java. Create an array of Animal and then loop through the array using an enhanced for(), calling the describe(), sound(), sleep(), and move() methods, successively
2. Change the LoopTest.java to use an ArrayList instead of an array. print your source code and indicate whether or not your for() loop had to be modified when changing from an array to an ArrayList.
First example is the array(point 1 above) it has a compiling error and again I am not sure if I am doing this correctly.
Compiling error: F:\Assignments\Assignment 3\Chapter 10\2 - Using Abstract Classes and Methods\LoopTestArray.java:29: cannot find symbol
symbol : method getType()
location: class Animal
System.out.println("For the " + (i+1) + ": "+ma[i].getType());
Tool completed with exit code 1
Second example is the Arraylist (point 2 above) It compiled and works perfect. Not sure if this is what they asked though.
Can someone please see if 1) am I understanding the questions right by writing the right code and to assist with the compiling error in point 1.
1. Your class name is wrong.
2. Why are you creating those objects in lines 12 - 15 ? You've already created them when you created the array.
3. You're not using an enhanced for loop
4. If you specify the parameters of the for loop correctly, you don't need any if/else statements in the for loop.
1. Your ArrayList should be specified to allow Animals not Strings
2. you should be adding the objects you create on lines 12 - 15 to the ArrayList, not Strings.
3. See 4 above.
You've declared an ArrayList that is designed to hold Strings, but you actually want one that holds Animals. Can you change the code to fix that ?
Once you've done that the code on lines 18 - 21 won't work anymore (because your ArrayList doesn't hold Strings), so get rid of that and instead add the objects you created on lines 12 - 15 to the ArrayList. Remember that Cat, Robin, Dog and Fish all extend Animal, so there is no problem with doing that.
Next you need to change the for loop. Because myAnimals now contains Animals, ma needs to be of type Animal.
If you comment out lines 28, 37, 47 and 57, the new code should compile. Once you have it compiling, post the new code and we'll look at the next step.
I have changed the red text to black because many people find it difficult to read.
Have you not been taught about polymorphism? You should not be looking for dog or cat in the if statements. If you want different behaviours, you should create Cat, Dog, Fish, etc classes.
You should also avoid the == operator like the plague. You are almost assured of errors the way you have used ==.
Thanks for the reply. I will work on that now and post the code. Campbell I am studing through INTEC and if you don't know there is no tutors. We have to do this on our own and hope for the best. I have been at this for over a week and been reading up on polymorphism. But it is difficult to try and do this on your own. I really do appreciate the help and I appreciate the way that Stuart is doing it. He is not just giving the code he is "teaching" and helping me.
A helping hand goes a long way.
Okay this is what I have now. Everything compiles and the output is 98% correct.
Next to the "Animal" - Cat, Dog, Bird and Fish I get the "code" (@11b86e7) and that is the problem now.
For the Cat@11b86e7: - (@11b86e7 is the problem now)
For the Robin@35ce36:
For the Dog@757aef:
For the Fish@d9f9c3:
This is: Nemo, a breed of Fish called Clownfish
Sound: Nemo doesn't make a sound
Sleeping: Nemo sleeps with his eyes open!
Moving: Nemo swims with his tail.
Anita Lahav wrote:Next to the "Animal" - Cat, Dog, Bird and Fish I get the "code" (@11b86e7) and that is the problem now.
You need to override the toString method in each of your classes.
Also, on line 29 of your code change the type variable to be an Animal instead of an Object. You then won't need to cast type to an Animal inside the loop.
Anita Lahav wrote:Is this what you mean?
No. I meant change the type of type, not the name. Also, change the name of your ArrayList to animal. It is convention for variable names to start with a lower case letter and having a variable with the exact same name as the class is confusing. I've changed it in the code below.
I would also recommend that you change your describe, sleep. sound and move methods so that they return Strings rather than having a print statement in them.
Then you can do this
Stuart A. Burkett wrote:Also, on line 29 of your code change the type variable...
@Anita: Also, what is the 'type' variable on line 3 of your Animal class?
You don't appear to use it; and if you were planning on using it to determine the subtype of Animal that this is...DON'T.
Code that looks like:is called "dispatch code", and it's exactly what polymorphism was designed to prevent. It's also a major "code smell" when you get more experienced.
If you design your Animal hierarchy properly, you should have no need of a 'type' field.