Nice TWiki > Doc > CodeExamples > MethcallBeginnerExample TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the BeginnerExamples
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 {                  
   boolean state = true;        

   public boolean value() = state;
             
   public Toggle activate(){
      state = !state; 
      return this; 
   }          
}

class NToggle extends Toggle {  
   int toggleTrigger = 0;
   int count = 0;

   activate(){
      count++;
      if (count >= toggleTrigger){
         state = !state;
         count = 0;
      }
      return this;
   }
}


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"

   println( toggle.activate().value() );
   println( ntoggle.activate.value );
When there are no parameters we don't need to use () in
method calls (we do need to use them in constructors).


*/

-- IsaacGouy - 27 Aug 2003

Topic MethcallBeginnerExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.6 | > | r1.5 | > | r1.4 | More }
Revision r1.6 - 10 Jan 2004 - 19:04 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.

Doc.MethcallBeginnerExample moved from Doc.MethcallNoviceExample on 27 Aug 2003 - 16:30 by IsaacGouy - put it back