You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 17 Current »

Counterint counterint endCounter(int start, int end)int getCounter()void count()void count(int increment)CounterProgramvoid init()void run()This class is only used for testing the Counter classcounter
Counter class
package stateandbehavior;
 
public class Counter {

	// internal state
	private int counter;
	private int end;

	public Counter(int start, int end) {
		this.counter = start;
		this.end = end;
	}

	public String toString() {
		return "[Counter counter=" + counter + " end=" + end + "]";
	}

	public int getCounter() {
		return counter;
	}

	public void count(int increment) {
		if (counter < end) {
			counter += increment;
		}
	}

	public void count() {
		count(1);
	}
}
CounterProgram class
package stateandbehavior;
 
public class CounterProgram {

	private Counter counter;
	
	public void init() {
		counter = new Counter(1, 3);
	}

	public void run() {
		System.out.println(counter);
		counter.count();
		System.out.println(counter);
		counter.count();
		System.out.println(counter);
		counter.count();
		System.out.println(counter);
	}

	public static void main(String[] args) {
		CounterProgram counterProgram = new CounterProgram();
		counterProgram.init();
		counterProgram.run();
	}
}
  • No labels