Hi Guys,
While going through some websites, I have found this interesting java puzzle and thought of sharing it with you!! J
Problem: You have to print “I am added 30 Students to a LKGClass of strength 10!!” by running the Tester class.
Restrictions: You are not allowed to modify the LKGClass but allowed to modify the Tester class. You are allowed to use any java programming tactics.
public class Student{
}
import java.util.HashSet;
import java.util.Set;
public class LKGStudent{
private static final int STRENGTH=10;
private Set< Student > student=new HashSet< Student >();
public synchronized void addStudent(Student s){
if(student.size()==STRENGTH){
throw new IllegalStateException("I'm full");
}
else{
student.add(s);
}
}
public synchronized void solvedPuzzle(){
if(student.size()==30){
//The goal of this puzzle is to reach this line!
System.out.println("I am added 30 Students to a LKGClass of strength 10!!");
}
}
}
public class Tester{
//This tester is a starting point and doesn’t print "I am added 30 Students to a LKGClass of strength 10!!" //try by modifying this class!!
public static void main(String [] args){
LKGStudent lkg=new LKGStudent();
for(int i=0;i<20;i++){
lkg.addStudent(new Student());
}
lkg.solvedPuzzle();
}
}
Hope this Helps!!
No comments:
Post a Comment