Explicitly and without apology a marketing vehicle MaryMaryQuiteContrary

Monday Aug 16, 2004

Ultimate Puzzler Challenge It's a MaryMaryQuiteContrary first! (well, not really but play along, will you?)

We've got a Friday Free Stuff tie! Christoph and Sue are finalists. They correctly answered last Friday's question. They get to take the Ultimate Puzzler Challenge, and battle it out for the specacular grand prize

picture of my books

(my entire library of books about the Java platform). Second runner up gets a leather Kenneth Cole PDA Holder. (sorry no picture handy; trust me, it's nice.)

But here at the MaryMaryQuiteContary everybody is a winner. Yes, you too can take the Ultimate Puzzler Challenge and get a piece of the Friday Free Stuff action. keep reading for details...

But first, last week's question:

In a recent blog entry I gave you two puzzlers from Dr. Josh and Dr. Neal. Each one consisted of a loop that looked like it did nothing, and you were supposed to come up with a declaration for the variable that caused it to spin forever, without using floating point. Here's the first loop:
 while (j != j + 0)
;
If it weren't for the restriction on using floating point, this would solve the puzzle:
 double j = Double.NaN;
Here's a solution that doesn't use floating point:
 String j = "Buy seventeen copies of Effective Java!";
This declaration turns the plus sign in the loop into the string concatenation operator. Then the int 0 is converted to the string "0" and appended to the blatant plug.

The plus sign is overloaded, and operator overloading can be very misleading: It looks like addition, but it's really string concatenation. It's all the more misleading because the variable is named j, which is generally used for ints. Good variable, method and class names are very important.

Now you're up-to-speed. You know what Christoph and Sue already knew. You're ready to take the Ultimate Puzzler Challenge. Please take out your Number 2 pencils...

The Ultimate Puzzler Challenge is this: without resorting to floating point, provide declarations for i and j that turn this loop below into an infinate loop (look carefully; it's not the same as Friday's):

 while (k != 0)
k >>>= 1;


Christoph, Sue: first one to post, wins.

Everybody else: post the right answer and I will send you FOC (Free of Charge) a full-color poster of the Java Technology Map. And while supplies last, I'll throw in a really nice ink pen too. See, there's something for everybody here at the MaryMaryQuiteContary blog. (or at least for the smart people ;-)

See you next time.

mary

p.s. i know you're not even going to believe this, but it's true. got email from Click and Hack. they signed it "kisses." putter, putter, my heart goes a-flutter.

p.p.s. this isn't a contest. i am giving away stuff that i pesonally own to people i choose. i pay for shipping with stamps that i buy with my own money from the post office.
Comments:

short k = -1; The short gets promoted to int and sign extended. The shift zeros the top bit, then it is silently narrowed back to a short, discarding the upper sixteen bits, including the newly zeroed top bit. A byte will work as well.

Posted by Andrew Taylor on August 16, 2004 at 02:53 PM PDT #

Well... ;)

Posted by Christoph on August 16, 2004 at 11:56 PM PDT #

Christoph wins the big grand prize. Sue, you get a prize too. Andrew, Luke: you (very smart) guys get posters. For the record, Luke was actually first. He emailed me the answer. As i'm quite clueless, I forwarded to Click and Hack, asking if Luke was right. They're response: "He's very clever." Luke has a secret. I've asked him if I could share it. If he says yes, I'll spill the beans. Can you stand the tension? mary p.s. i've got more posters. anybody who posts the right answer gets a free poster. please feel free to use my favorite marketing technique (which can certainly be applied to technical conversations): copy, paste.

Posted by mary on August 17, 2004 at 05:41 AM PDT #

I guess that is what I get for watching the olympics instead of checking the rss feeds :-D

Posted by Sue Henshaw on August 17, 2004 at 06:24 AM PDT #

Post a Comment:
Comments are closed for this entry.