JavaBOTL

Defining New Robot class


One of the things that is different in the JavaBOTL simulator is that new robot definition is not called a NewRobotType. Instead, it is called a new robot class. You cannot redefine the characteristics of a basic_bot.class, so if you want to change them  to something you prefer, you must define your own robot class.

First, however, you must decide the type of robot on which you wish to base your new class of robot. For now, the only defined robot class is basic_bot, so you must base your new robot on it. By basing a new robot class on an existing one, the new one inherits all the existing characteristics of the exisiting class. New characteristics can be defined without affecting any other robot types.

To define a new robot class, you must use the class and extends statements. Because NewRobotType is not supported, IsLikeA is not supported.


Example

                    class another_bot extends basic_bot{
                           public void run(){
                                if(FrontIsClear) Move(1);
                           }
                    }


 Now we can do something useful with a new robot class:
 
             public class demo1 extends Rob{ //Rob is the name of the JavaBOTL library
                                             //Each program should begin with this line
                      public void SetWorld(){
                          karel_smarter_bot karel = new karel_smarter_bot();
                          egghead_marter_bot egghead = new egghead_smarter_bot();
                          karel.place(Paradise,1,1);
                          egghead.place(Paradise,3,3);
                      }
             }
             class karel_smarter_bot extends base_smarter_bot{
                       public void run(){
                                for(int i=0;i<3;i++){
                                   if(RightIsClear){
                                        for(int j=0;j<3;j++)TurnLeft();
                                        Move(1);
                                   }
                                }
                        }
               }
             class egg_head_smart extends base_smarter_bot{
                        public void run(){
                                for(int i=0;i<4;i++)MoveRight();
                        }
              }
 
             class base_smarter_bot extends basic_bot{
                           int DefineInitialBeepers = 5;
                           public void turnright(){
                                for(int i=0;i<3;i++) TurnLeft();
                           }
                           public void moveright(){
                                if(RightIsClear){
                                    turnright();
                                    Move(1);
                                 }
                            }
              }


The result is here. (Be sure your browser supports Java)

The original robotl definition might be:


define
{
   NewRobotType smarter_bot 
   {
      IsLikeA basic_bot; 
      DefineInitialBeepers 5; 
      DefineNewInstruction turnright as iterate 3 times TurnLeft;       
      DefineNewInstruction moveright as
      {
         if (RightIsClear)
         {
            turnright;
            Move 1; 
         }
      }
   }
}
execute
{
   new basic_bot karel at 1,1; 
   tell karel: 
   {
      iterate 4 times 
      {
         if (RightIsClear) 
         {
            iterate 3 times TurnLeft;
            Move 1; 
         }
      }
      TurnOff; 
   }
   new smarter_bot egghead at 3,3;
   tell egghead: iterate 4 times moveright;
   tell egghead: TurnOff;
}