Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

challenging puzzle

 
monis raza
Ranch Hand
Posts: 38
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for challenging puzzles visit
namaste-java.blogspot.com
 
Myke Enriq
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a puzzle form his blog:

cOoKiEs PuZzLe
If cookies were Strings, could you write the Cookie Monster? The class Count is given:

package count;

import monster.CookieMonster;

public class Count {
public static void main(String[] args) {
String noCookie = CookieMonster.eat("cookie");
if (noCookie.isEmpty() && CookieMonster.eat(noCookie).length() < noCookie.length()) {
// The goal is to reach this line
System.out.println("Minus one cookie!");
}
}
}
Edit the Cookie Monster to make it work:

package monster;

public class CookieMonster {
public static String eat(String cookie) {
return cookie.substring(0, cookie.length() - 6);
}
}

-------------------------------

Spring AOP and custom class loaders seem like cheating to me. I wonder how you can do this with Java SE only.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic