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.print("Watcher1 " + ((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();
        }
    }
}
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.counter(3);
        System.out.println();
        observed.addObserver(observing2);
        observed.counter(3);
    }
}


Watcher1 3, Watcher1 2, Watcher1 1, Watcher1 0,
Watcher1 3, Watcher1 2, Watcher1 1, Done
Watcher1 0,
Watcher1 3, Watcher1 2, Watcher1 1, Done
Watcher1 0,
Watcher1 3, Watcher1 2, Watcher1 1, Done
Watcher1 0,
Watcher1 3, Watcher1 2, Watcher1 1, Watcher1 0,
Watcher1 3, Watcher1 2, Watcher1 1, Watcher1 0,
Compilation Error 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