Exercises - Classes and Interfaces
  1. Begin with the following interface
       public interface Topo { void exec (); }
    which will be defined in its own file named Topo.java. Create a Node class whose local state is just an identity string called id and a JTextArea called text. The constructor of class Node looks like this
       public Node (String id, JTextArea text) { this.id = id; this.text = text; }
    Have the Node class implement interface Topo by creating an exec method that just prints the id to text. Test it using E1.java. The result should be a list of numbers from 0 to 9.

  2. The above applet runs exec on all 10 of the Node objects that were created in actionPerformed. A for loop was used to do this: each Node object was referenced by an array element and the loop ran exec on each in turn. But even though the number of objects to consider is know in advance, a test is made at every iteration to see whether the end of the loop has been reached (i < 10). If there had been a million objects to consider in the loop, then a million unnecessary tests would have been executed. In this exercise we are going to write code to do as in E1.java except with no tests.

    Start with the following:

       public class Cell implements Topo {
          Cell next;
          Node node;
          public Cell () { next = null;  this.node = node; }
          public void exec () { }
    and develop a class called Neighbor which extends Cell (and thereby holds a Node object), and whose exec method executes the exec of its Node object then executes the exec of next. Test it using E1.java. The result should be a list of numbers from 0 to 9. Observe that the second loop is replaced by head.exec().

  3. The next step is to use this idea to remove all tests (that is, ifs and loops from a non-trivial program. The problem of topologically sorting a partial order is stated in exercise 13 of the Constructs 0 section. The solution to that problem entails visiting, in order, all dependent objects (called components in exercise 13) and testing to see whether a component is in the 'DONE' or 'ERROR' states. The visiting can be accomplished as in the previous exercise. Elimination of the other tests can be accomplished by developing classes called Error, Done, and Visit which implement the Topo interface. For example, class Visit looks like this:
       public class Visit implements Topo {
          Node node;
          public Visit (Node node) {  this.node = node;  }
          public void exec () {
             node.topo = new Error();// Replace 'visit' function with 'error' function
             node.neighbors.exec();  // Invoke the 'topo' of all neighbors
             node.topo = new Topo(); // Replace 'error' function with 'done' function
             text.append(node.getID()+" ");   // Output the node's identity
    where the Node class of the previous exercise has been expanded to record and hold the dependencies of a component (one Node object per component) and to return an id through getID().