Menu
Question Index
...

What is the output of the following program?

import java.util.*;
class Watcher1 implements Observer {
    public void update(Observable obs, Object arg) {
        System.out.println("Watcher " + ((Integer) arg).intValue());
    }
}
class Watcher2 implements Observer {
    public void update(Observable o, Object arg) {
        if (((Integer) arg).intValue() == 0) {
            System.out.println("Done");
        }
    }
}
class Watcher extends Observable {
    void counter(int input) {
        for (; input >= 0; input--) {
            setChanged();
            notifyObservers(new Integer(input));
            clearChanged();
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                System.out.println("Sleep Interrupted");
            }
        }
    }
}
public class MultipleObserves {
    public static void main(String[] args) {
        Watcher observed = new Watcher();
        Watcher1 observing1 = new Watcher1();
        Watcher2 observing2 = new Watcher2();
        observed.addObserver(observing1);
        observed.addObserver(observing2);
        observed.counter(3);
    }
}


Watcher 3
Watcher 2
Watcher 1
Watcher 0
Done
Watcher 0
Watcher 1
Watcher 2
Watcher 3
Done
Watcher 3
Watcher 2
Watcher 1
Done
Watcher 0
Watcher 3
Done
Compiles and executes without any output
Compilation or Runtime Error

Doubts

Problems

Topic: Java Observable

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App