alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (methcall.java)

This example Groovy source code file (methcall.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

io, ioexception, ioexception, nthtoggle, nthtoggle, text, toggle, toggle, util

The Groovy methcall.java source code

// $Id: methcall.java,v 1.1 2004-05-23 07:14:27 bfulgham Exp $
// http://www.bagley.org/~doug/shootout/
// Collection class code is from my friend Phil Chu, Thanks Phil!

import java.io.*;
import java.util.*;
import java.text.*;

class Toggle {
    boolean state = true;
    public Toggle(boolean start_state) {
	this.state = start_state;
    }
    public boolean value() {
	return(this.state);
    }
    public Toggle activate() {
	this.state = !this.state;
	return(this);
    }
}

class NthToggle extends Toggle {
    int count_max = 0;
    int counter = 0;

    public NthToggle(boolean start_state, int max_counter) {
	super(start_state);
	this.count_max = max_counter;
	this.counter = 0;
    }
    public Toggle activate() {
	this.counter += 1;
	if (this.counter >= this.count_max) {
	    this.state = !this.state;
	    this.counter = 0;
	}
	return(this);
    }
}

public class methcall {
    public static void main(String args[]) throws IOException {
	int n = Integer.parseInt(args[0]);

	boolean val = true;
	Toggle toggle = new Toggle(val);
	for (int i=0; i<n; i++) {
	    val = toggle.activate().value();
	}
	System.out.println((val) ? "true" : "false");

	val = true;
	NthToggle ntoggle = new NthToggle(true, 3);
	for (int i=0; i<n; i++) {
	    val = ntoggle.activate().value();
	}
	System.out.println((val) ? "true" : "false");
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy methcall.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.