Two Laptop Bag*
The moose likes Beginning Java and the fly likes Head First Java problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java problem" Watch "Head First Java problem" New topic
Author

Head First Java problem

Phil Dixon
Greenhorn

Joined: Aug 13, 2005
Posts: 11
I'm working my way through Head First Java.
I'm not sure if I'm missing something or just being stupid. I was hoping that my first post would be a little more taxing than this!

I have two classes. MyAnimalList compiles ok



and AnimalClassTestDrive which will not compile.



I thought maybe it was because I didn't have a Cat or Dog class but then I think the hole point of this exercise is to create a class that can take any kind of subclass.

The error I get is

add(Animal) in MyAnimalList cannot be applies to (Dog
list.add(c);

and the same for cat.

Any guidance will be much appreciated.

Great forum by the way.

Phil.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Phil,

For those of us who do not have the book to hand: could you please post the Animal, Dog, and Cat classes?

Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Phil Dixon
Greenhorn

Joined: Aug 13, 2005
Posts: 11
Barry,

That's all there is. Two classes, MyAnimalList and AnimalTestDrive.

Phil.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
I don't have the book with me right now, but I think if you create a couple of classes like:

class Cat extends Animal {
}

and

class Dog extends Animal {
}

your problem will be resolved.

I think the whole point of this exercise is to create a class that can take any kind of subclass.

The problem is that currently you don't have any kind of subclass of Animal for your created class to use.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hi Phil,

Well you've made it to chapter 8, congrats! We hope you started from the beginning, or at least near the beginning! This chapter relies on chapter 7 as a foundation chapter, so if you just jumped in at chapter 8 you might miss some important stuff.
In any case, based on what you've told us, you must have at least one other class in your directory. Your first assignment is to figure out how I knew that based on your posts... once you've found that class see if you can determine what else you might need. Let us know what you discover.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Phil Dixon
Greenhorn

Joined: Aug 13, 2005
Posts: 11
Marilyn/Bert,

I have read the book from the start ("honest guv").

I tried creating a Dog and Cat class but did not use 'extends Animal'. All working now.

I had/have confused myself how the Animal object is created. I'll read through Ch 7 again and come back with my answer.

Thanks for your help.

[ September 18, 2005: Message edited by: Phil Dixon ]
[ September 18, 2005: Message edited by: Phil Dixon ]
 
GeeCON Prague 2014
 
subject: Head First Java problem