Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm probably overlooking something very stupid...  RSS feed

 
Daniel Gagnon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm new to Java but not to programming. I'm re-reading the excellent Head First Design Patterns because now I know the language it was written for instead of trying to translate to another language.

I'm stuck on the first pattern. I think I understand it but I'm unsure. I copied what's in the book to play with it by my IDE (Eclipse) is giving me an error on two lines.

I compared what I wrote and what's in the book but I can't spot the difference that makes my code fail. I looked until my brain started oozing out of my ears. I'm probably overlooking something really stupid. Can some more experienced with Java than me take a look and tell me why it doesn't work ?

I put everything in a package called duckies because Eclipse tells me it's not a good idea to let everything in default. I don't think it really matters, does it ?

Thanks a lot

Dan

So the pattern is Strategy. And the code goes as follow (problematic lines in bold) :

Duck.java


FlyBehavior.java


FlyWithWings.java


FlyNoWay.java


QuackBehavior.java


Quack.java


Squeak.java


MuteQuack.java


MallardDuck.java
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

In Java, a constructor invocation always includes parentheses, even if the argument list is empty. Both of those lines have the same problem: a missing set of empty parentheses right before the semicolon.
 
Daniel Gagnon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. That was a fast answer, thanks.

I feel like banging my head on a wall for missing that mistake though...

My code works now. Thank you very much
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You implied in your first message that you read first HFDP and applied the knowledge to some other language; I assumed it might be C++, where the no-parentheses thing would be correct. Anyway, just part of the territory; glad to help!
 
Daniel Gagnon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Python actually. It involved understanding the concepts from HFDP, lots of Googling to figure out how people were doing it in Python and trying it out. Because of the language differences, I feel I didn't get as much as I could from the book so now that I picked Java, I'm reading it again.

I do now C++ however which might be why I got confused.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!