Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Instantiating the Ferret

Now that we have a class that implements the Ferret, we can actually create and use one. The main application, ResearchProject, instantiates the GoogleFinder as a Ferret and attempts to call its methods—as shown in Listing 3.







Listing 3 Instantiating the Ferret

import org.chase.research.Ferret;
import org.chase.ferrets.GoogleFinder;

public class ResearchProject {

  public static void main
  (String[] args) {

    if (args.length > 0) {
    
      String searchTerm = args[0];

      Ferret theFerret = new GoogleFinder();    

      System.out.println("Getting Results ...");
      theFerret.find(searchTerm);

      int numResults = theFerret.getNumResults();
      if (numResults > -1) {
        System.out.println("\n=========================");
        System.out.println("Number of hits: ");
        System.out.println("=========================");
        System.out.println(numResults);
      } else {
        System.out.println("Result count not available.");
      }
      
      System.out.println("\n=========================");
      System.out.println("First Result: ");
      System.out.println("=========================");       
      System.out.println(theFerret.getFirstResult());
      
      System.out.println("\n=========================");
      System.out.println("All available results: ");
      System.out.println("=========================");
      System.out.println(theFerret.getResults());
 
      
    } else {
      
      System.out.println("You must enter a search term.");
      
    }
    
  }
}

Here, we create the new Ferret object, but we do it with the GoogleFinder class. Once we've done that, we have a Ferret object, not a GoogleFinder object. As you can see here, we can use all of the Ferret methods. If, on the other hand, we try to use the getSpelling() method defined in GoogleFinder (but not in Ferret), the application won't compile because getSpelling() is undefined for a Ferret.

In this way, we force people to use only the appropriate methods.

Executing the application with a search term of, say, hyperdrive, provides the following results:

Number of hits:
=========================
36300

=========================
First Result:
=========================

 [
 URL = "http://www.hyperdrive.com/"
 Title = ""
 Snippet = ""
 Directory Category = {SE="", FVN=""}
 Directory Title = ""
 Summary = ""
 Cached Size = ""
 Related information present = true
 Host Name = ""
 ]

=========================
All available results:
=========================
{
TM = 0.061629
Q = "hyperdrive"
CT = ""
TT = ""
CATs =
 {
 {SE="", FVN="Top/Shopping/Recreation_and_Hobbies/Models/Radio_Control/Cars_and
_Trucks"}
 }
Start Index = 1
End  Index = 10
Estimated Total Results Number = 36300
Document Filtering = false
Estimate Correct = false
Rs =
 {

 [
 URL = "http://www.hyperdrive.com/"
 Title = ""
 Snippet = ""
 Directory Category = {SE="", FVN=""}
 Directory Title = ""
 Summary = ""
 Cached Size = ""
 Related information present = true
 Host Name = ""
 ],

 [
 URL = "http://www.hyperdriveuk.com/hyperdrive_/hyperdrive.html"
 Title = "<b>Hyperdrive</b>"
 Snippet = ""
 Directory Category = {SE="", FVN=""}
 Directory Title = ""
 Summary = ""
 Cached Size = "1k"
 Related information present = true
 Host Name = ""
 ],
...
 }
}

I've omitted the other results that are part of the set, but you get the idea.

The results are here, but they're not...well, they're not entirely aesthetically pleasing. What if we built a second version that treated the results differently?

  • + Share This
  • 🔖 Save To Your Account