• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Abstract Classes and Methods  RSS feed

 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there:

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());
^
1 error

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.

Array code:


ArrayList Code:
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler error is because the Animal class does not have a getType method.

Class 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.

Class 2
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.
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very very very new at this and I have no tutor to help or ask. I have done what I thought was right and have no idea how to fix this. Will you be prepared to go into details (Lots of it) to help me understand what I should do.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your second piece of code is closer to what is required, so lets look at that.
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.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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 ==.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:Once you have it compiling, post the new code and we'll look at the next step.

I see Campbell has already given you a hint about the next step, so you should have a go at that as well.
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

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.

Thanks again.
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have



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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is code of the Animal Class and also the Cat class if that might help.


 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is this what you mean?

I am not sure what you mean, will have to read up quick on the toString() methods.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Something like

Then you can do this
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HIH

Winston
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart I don't understand.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anita Lahav wrote:Stuart I don't understand.

What don't you understand? Stuart showed you precisely what he meant in his post.

Winston
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just came right.

Thanks for the help.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, that is a lot better than before, isn't it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!