Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
Help needen with Pool Puzzle excercise on page 91 Head First book...pleaseee
Krisztina Tarko
Greenhorn
Posts: 3
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi..
I am struggling since more than 1 hour on finding where the mistakes lies in my code from the Puzzle Exercise ..
Here is how I solved the exercise:
public class Puzzle4 { public static void main(String[] args) { Puzzle4b[] obs = new Puzzle4b[6]; int y = 1; int x = 0; int result = 0; while (x < 6) { obs[x] = new Puzzle4b(); obs[x].ivar = y; y = y * 10; x = x + 1; } x = 6; while (x > 0) { x = x - 1; result = result + obs[x].doStuff(x); } System.out.println("result " + result); } class Puzzle4b { int ivar; public int doStuff (int factor) { if (ivar > 100) { return ivar * factor; } else { return ivar * (5 - factor); } } } }
And even though i really CAN'T see any difference from the given solution in the book..i get the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from Puzzle4 to Puzzle4.Puzzle4b at Puzzle4.main(Puzzle4.java:14)
Can someone see where i'm wrong..you know how they say: another pair of eyes always help:)
Many many thanks
Kris.
pete stein
Bartender
Posts: 1561
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'd take Puzzle4b out of the Puzzle4 class. Make it a stand-alone class. Otherwise you'll need to do funny and ugly constructs like this one:
obs[x] = new Puzzle4().new Puzzle4b();
One other option is to make Puzzle4b a static inner class:
static class Puzzle4b { //...
Krisztina Tarko
Greenhorn
Posts: 3
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yeah..that was it... i had the feeling that at this level it works to have them all in one..My mistake!!
Thanks a lot!
A good day!
Rob Spoor
Sheriff
Posts: 22781
131
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Next time, check out the compiler errors (and to a lesser extent its warnings) before running your code.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Krisztina Tarko
Greenhorn
Posts: 3
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yes I know..it was a stupid mistake ..and sorry for takin your time with it..i just was stuck. But learned a good lesson!
again: Thanks a lot for your quick reply!
I promiss I won't be bothering again with such obvious things:)
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Head first Java - Chapter 4 Pool Puzzle p.91
WHat is Exception in thread "main" java.lang.NullPointerException ?
Head first java exercise JVM problem
Trying to understand Puzzle4..Head First
Puzzle 4
More...