Nice TWiki > Doc > CodeExamples > MethcallBeginnerExample (r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the BeginnerExamples before the NoviceExamples
To compile:
   nicec --sourcepath=.. -a methcall.jar methcall
To run:
   java -jar methcall.jar 1000000
*/


void main(String[] args){
   let n =  toSingleInt(args);
   boolean val;

   let toggle = new Toggle();
   for(var i=1; i<n; i++) val = toggle.activate().value();
   println( toggle.activate().value() );

   let ntoggle = new NToggle(toggleTrigger: 3);
   for(var i=1; i<n; i++) val = ntoggle.activate().value();
   println( ntoggle.activate().value() );
}



class Toggle {                  // define class Toggle       
   boolean state = true;        

   boolean value();             // declare method value
   Toggle activate();           // declare method activate
}

value(t@Toggle){                // implement method value 
   return t.state; 
}

activate(t@Toggle){             // implement method activate
   t.state = !t.state; 
   return t; 
}


class NToggle extends Toggle {  // define subclass NToggle        
   int toggleTrigger = 0;
   int count = 0;
}

activate(t@NToggle){            // override Toggle activate 
   t.count++;
   if (t.count >= t.toggleTrigger){
      t.state = !t.state;
      t.count = 0;
   }
   return t; 
}


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }


/* Notes - language
Compare with Java at the "Win32 Language Shootout"

*/
-- IsaacGouy - 27 Aug 2003

Topic MethcallBeginnerExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.6 | > | r1.5 | > | r1.4 | More }
Revision r1.1 - 27 Aug 2003 - 00:16 GMT - IsaacGouy
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.