6. Methods: Communicating with Objects

6.7. Special Topic: Intelligent Agents

Wouldn’t it be nice if we had a computer program that could schedule appointments for us, remind us of meetings and commitments, find in- formation for us on the WWW, and manage our e-mail messages for us? Wouldn’t it be nice to have a computerized personal assistant?

Actually, such programs are called intelligent agents, which are pro- grams that are capable of acting autonomously to carry out certain tasks. Intelligent agent technology is becoming an important research area in computer science. Most agent programs incorporate some kind of ma- chine learning capability, so that their performance improves over time.

As a typical agent activity, suppose I was able to tell my intelligent agent to buy me a copy of a certain book that I just heard about. Given a command like “buy me a copy of X,” the agent would perform a search of online book sellers and come up with the best deal. Once it had found the best buy, the agent would communicate with a computer-based agent representing the book seller. My agent would make the order and pay for it (assuming I gave it authority to do so), and the book seller’s agent would process the order.

As far-fetched as the capability may now seem, this is the direction that research in this area is headed. Researchers are developing agent languages and describing protocols that agents can use to exchange in- formation in a reliable and trustworthy environment. Obviously, you wouldn’t want your agent to give your money to a fraudulent book seller, so there are significant problems to solve in this area that go well beyond the problem of simply exchanging information between two agents.

The best way to learn more about this research area is to do a Web search using the search string “Intelligent Agent.” There are numerous re- search groups and companies that provide online descriptions and demos of their products.

 

SECTION 3.8 From the Java Library java.lang.Object135

From the Java Library java.lang.Object

 

 

The most general class in Java’s class hierarchy is the java.lang.Object class. It is the superclass of all classes that occur in Java programs. By de- fault, it is the direct superclass of any class that does not explicitly specify a pedigree in its class definition.

All subclasses of Object inherit the public and protected methods contained in Object, so all such methods can be thought of as belonging to the subclasses. This means that all classes inherit the methods of the Object class, because every class is a subclass of it. In this section, let’s look briefly at how we can use an inherited method and also at how we can override it–that is, redefine the method–if it doesn’t exactly suit our purposes.

One of the most useful methods in the Object class is the

toString() method:

,,


 

java.sun.com/j2se/1.5.0/docs/api/

 

 

 

 

J

The toString() method returns a String representation of its object. For example, o1.toString() will return a String that in some sense describes o1.

Because OneRowNim is a subclass of Object, it inherits the toString() method. To illustrate the default behavior of toString(), let’s use it with a OneRowNim instance:

,,

 

 

 

J

This code segment creates two OneRowNim instances, one named g1 and the other named g2. The inherited toString() method is then invoked on each OneRowNim instance, which produces the following output:

,,

 

J

What this experiment shows is that the default definition of toString() returns some kind of internal representation of its object. It looks as if it returns the name of the object’s class concatenated with its memory ad- dress. This may be useful for some applications. But for most objects we will want to override the default definition to make the toString() method return a string that is more appropriate for OneRowNim.

What String should the g1.toString() method return? Let’s have it return a String that reports the OneRowNim instances’s current state, which are the values stored in the two instance variables. To override a method, you simply define a method with the same signature in the

 

subclass. If you call toString() with an instance of the subclass, its version of the method will be used. In this way, the subclass method over- rides the superclass version. Thus, OneRowNim.toString() will have the following signature:

,,

 

J

Let us describe the state of a oneRowNim instance very briefly in the string returned by the toString() method:

,,

 

 

J

If we add the toString() method to the OneRowNim class and then run the program shown in Figure 3.18, we get the following output:

,,

 

J

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Inheritance


J

Figure 3.18: An application to test the overridden toString() method.

 

While this new method may not play an important role in the OneRowNim class, it does provide a very brief, understandable description of the state of the object. This is the reason that the toString() method was in- cluded in the Object class.