FourGL - a Humble Attempt

Computer Science is currently straddled between 3rd Generation Languages and 4th Generation Applications.  Users assume that any computer will do all those wonderful things they need, like surfing to a web-site, downloading music files, copying CD's, playing videos, etc.  

Unfortunately, 3GL programming languages like Java don't provide :
(a) the power (commands) needed to accomplish all that multimedia magic;
(b) the natural language syntax expected by a "normal" human being (a la Google);
(c) common sense like remembering what you did yesterday (e.g. recent files), ignoring stupid requests (error warnings), etc.

This means that programmers have the unenviable task of providing users with extremely complex, powerful features, while using only rather simple, antiquated programming tools.

We will attempt to bridge this gap just slightly by making a simple prototype of a Fourth Generation Language, starting with the program below.

== Method Magic ==

Java and other 3GLs allow us to expand the language by adding our own commands.  We do this by writing methods.  Each new method has a name, and that identifier (name) becomes a new command.  We can write very powerful (albeit long) methods, and then use them just by "saying the magic word".  

The program below shows some examples of powerful methods.  It also encapsulates that magic into a very simple user interface.  That means a non-programmer (user) could also use the power of our new methods.

img1.gif

img3.gif


   private void goActionPerformed(java.awt.event.ActionEvent evt)
   {
      String info = command.getText();
      String[] terms = info.split("\\s");
      result.setText("");
      if (terms[0].equals("count"))
      {  if (terms.length == 1)          // count
         { count(); } 
         else if (terms.length == 3 && terms[1].equals("to"))
         { count(terms[2]); }                     // count to 20  
         else if ( terms.length == 5 
                   && terms[1].equals("from")     // count from 20 to 30
                   && terms[3].equals("to")
                 )
         { count(terms[2],terms[4]); }              
         else if ( terms.length == 7
                   && terms[1].equals("from")     // count from 5 to 100 by 5
                   && terms[3].equals("to")
                   && terms[5].equals("by")
                 )
         { count(terms[2],terms[4],terms[6]); }  
      }   
      else if (terms[0].equals("sum"))          // sum 1 5 8 3 9 --> 26
      {  sum(terms); }                          
      else if (terms[0].equals("browse"))       // browse http://www.cnn.com
      {
          explore(terms[1]);                    
      }
      else if (terms[0].equals("open"))         // open d:\active.doc
      {
          explore(terms[1]);
      }
   }

   public void count(String fromS, String toS, String byS)
   {
       int from = Integer.parseInt(fromS);
       int to = Integer.parseInt(toS);
       int by = Integer.parseInt(byS);
       for (int c = from; c <= to; c = c + by)
       {
           print(c + "\n");
       }
   }
   
   public void count(String fromS, String toS)
   {
       count(fromS,toS,"1");
   }
   
   public void count(String toS)
   {
       count("1",toS,"1");
   }
   
   public void count()
   {
      count("1","10","1");
   }
 
   public void print(String info)
   {
      String oldText = result.getText();
      result.setText(oldText + info);
   }
   
   public void date(String com)
   {
      String[] info = com.split("/");
      for (int c = 1; c < info.length; c++)
      {
         print(info[c] + "\n");
      }
   }   

   public void explore(String webSite)
   {
      try
      { Runtime.getRuntime().exec("explorer " + webSite);
      }
      catch(java.io.IOException ex)
      { };
   }
  
   public void sum(String[] info)
   {
      int total = 0;
      for (int c=1; c < info.length; c=c+1)
      {  
         int num = Integer.parseInt(info[c]);
         total = total + num;
      }    
      print(total + "\n");
   }
   
 

Assignment

  1. Copy the code for FourGL and insert it into a NetBeans program.
    You will need to make 3 controls on the form:
        TextField   :   command
        
    Button        :  go
        
    TextArea    :  result
     
  2. Run the program and check that the following commands work correctly:
         count
         count to 15
         count from 10 to 20
         count from 10 to 20 by 2
         sum  1 3 5 7 9
         browse http://www.bored.com
         open d:\somedocument.doc
     
  3. Now we wish to create some more commands for the FourGL language.
    Choose one of the following and program it.  Then do another.  Then another....

You may also add any other ideas you think of.  But keep it simple.