You have to organize a racing event to find out top 3 horses. You have a 5 lane track and you DO NOT have stop watch.
-> Seven races
Best answer was given by Mandeep Singh (8) and then by Amritpal Singh (10)
You have to organize a racing event to find out top 3 horses. You have a 5 lane track and you DO NOT have stop watch.
You entered in room and noticed that there is nothing but a bulb hanging from the ceiling. You noticed that the bulb is exactly touching your head and it is in the center of roof (where diagonals meet.)
So, what is roof’s height? BTW, you just came from office. So, go ahead and take some assumptions
public class CallService {
Callable callMaker = new HomePhone();
public void callClient(int clientId){
// we do not care how to make a call, but we will need callMaker
callMaker.makeCall();
}
// other methods to exposed goes here.
}
interface Callable{
void makeCall();
}
class HomePhone implements Callable{
public void makeCall() {
System.out.println("Calling from HomePhone ...");
}
}
class MobilePhone implements Callable{
public void makeCall() {
System.out.println("Calling from MobilePhone ...");
}
}
public class CallService {
Callable callMaker = null;
public CallService(Callable callMaker){
this.callMaker = callMaker;
}
public void callClient(int clientId){
// we do not care how to make a call, but we will need callMaker
callMaker.makeCall();
}
// other methods to exposed goes here.
}
class Container {
private static Container INSTANCE = new Container();
public static Container getInstance() {
return INSTANCE;
}
Mapcomponents;
private Container() {
components = new HashMap();
try {
Properties properties = new Properties();
properties.load(new FileInputStream("testPackage/other/properties/container.properties"));
for (Map.Entry entry : properties.entrySet()){
process((String)entry.getKey(), (String)entry.getValue());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void process(String key, String value) throws Exception{
String[] arr = key.split("\\.");
if (arr.length == 1){
Object component = Class.forName(value).newInstance();
components.put(key, component);
}else{
Object component = components.get(arr[0]); // CallService
Object toBeSet = components.get(value); // CallMaker
PropertyUtils.setProperty(component, arr[1], toBeSet); //CallService.setCallMaker(...)
}
}
public Object getComponent(String key){
return components.get(key);
}
}
class CallServiceClient {
public static void main(String[] args){
Container container = Container.getInstance();
CallService service = (CallService)container.getComponent("callservice");
service.callClient(5);
}
}
/*
* CountManager
*/
public class CountManager {
static int count = 0;
public static synchronized void incrementFew(int n) {
for (int i = 0; i < n; i++) {
count++;
log("Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}
public synchronized void addFew(int n) {
for (int i = 0; i < n; i++) {
count++;
log("Non-Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}
/*
* CountMangagerClient, needs run()
*/
static abstract class CountManagerClient implements Runnable {
CountManager s;
public CountManagerClient(CountManager s) {
this.s = s;
}
}
private static void log(String str) {
System.out.println(str);
}
/* main */
public static void main(String[] args) {
CountManager s = new CountManager();
Thread t1 = new Thread(new CountManagerClient(s) {
public void run() {
s.addFew(5); // no change in client code
System.out.println("Result = " + CountManager.count);
}
});
Thread t2 = new Thread(new CountManagerClient(s) {
public void run() {
CountManager.incrementFew(5); // no change in client code
System.out.println("Result 2 = " + CountManager.count);
}
});
t1.start();
t2.start();
}
}
/*
* CountManager
* @Threadsafe
*/
public class CountManager {
static int count = 0;
/* Object to get lock from */
private static final Object obj = new Object();
public static void incrementFew(int n) {
synchronized (obj) { /* get lock from Object 'obj' instead of 'MyClass.class'*/
for (int i = 0; i < n; i++) {
count++;
log("Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}
}
public void addFew(int n) {
synchronized (obj) { /* get lock from Object 'obj' instead of 'this'*/
for (int i = 0; i < n; i++) {
count++;
log("Non-Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}
}
/*
* CountMangagerClient, needs run()
*/
static abstract class CountManagerClient implements Runnable {
CountManager s;
public CountManagerClient(CountManager s) {
this.s = s;
}
}
private static void log(String str) {
System.out.println(str);
}
public static void main(String[] args) {
CountManager s = new CountManager();
Thread t1 = new Thread(new CountManagerClient(s) {
public void run() {
s.addFew(5); // no change in client code
System.out.println("Result = " + CountManager.count);
}
});
Thread t2 = new Thread(new CountManagerClient(s) {
public void run() {
CountManager.incrementFew(5); // no change in client code
System.out.println("Result 2 = " + CountManager.count);
}
});
t1.start();
t2.start();
}
}
public static void main(String[] args){
int[] arr = {2,3,6,9,20,2,4,6,9,20,3};
int result = 0;
for(int i: arr){
result ^= i;
}
System.out.println(result); //prints 4
}
//Context ctx = getInitialContext("iiop://FA-LT5824.us.farwaha.com:38014");
// for cluster
Context ctx = getInitialContext("corbaloc::FA-LT5824.us.farwaha.com:9811,:MA-LT5184.us.farwaha.com:9811");
if (ctx == null) {
System.out.println("Context received is null");
System.exit(0);
}
//Object obj = ctx.lookup("FulfillmentService");
//cluster aware lookup
Object obj = ctx.lookup("cell/clusters/ClusterEJB/FulfillmentService");
IFulfillmentServiceBeanHome home = (IFulfillmentServiceBeanHome) PortableRemoteObject.narrow(
obj, IFulfillmentServiceBeanHome.class);
IFulfillmentService bean = home.create();
bean.importOrder("testXML"); // just test
def list = []
for (i in 1..5)
list.add(i);
/*I am treating doThis as a Closure, I could have written 'Closure doThis' as well */
def doSomething(toMe, doThis){
toMe.each(doThis)
}
doSomething(list, {print it + " "})
print("\n")
doSomething(list, {print it * 5 + " "})
print("\n")
list.each{print it + " "}
1 2 3 4 5
5 10 15 20 25
1 2 3 4 5
1 2 3 4 5
5 10 15 20 25
10 20 30 40 50
1 2 3 4 5