The Java class Counter:

Here is a Java program in two files: Counter.java and CounterDemo.java that was an informal assignment in week 2:

// Counter.java: a class representing a counter object
public class Counter{
    // the value of the counter
    // myValue is an integer variable giving the counter value
    private int myValue;

    // constructor -- used when a new counter is created (starts at 0)
    public Counter(){
         myValue = 0;
    }

    // constructor -- this one starts at the parameter value
    public Counter(int init){
         myValue = init;
    }

    // accessor -- used to fetch the counter's value
    public int getValue(){
         return myValue;
    }

    // used to print the counter's value
    public void tellValue(){
         System.out.println("  From tellValue: I have value " + myValue);
    }

    // used to automatically print the counter's value   
    public String toString(){
         return "  From toString: I have value " + myValue;
    }

    // modifier -- used to increment the counter by 1
    public void incrementCounter(){
         myValue++;
    }

    // modifier -- used to increment the counter by parameter value
    public void incrementCounter(int incr){
         myValue += incr;
    }

    // modifier -- used to decrement the counter by 1
    public void decrementCounter(){
         myValue--;
    }

    // modifier -- used to decrement the counter by parameter value
    public void decrementCounter(int incr){
         myValue -= incr;
    }
}      


// CounterDemo.java: Test and demonstrate counter objects public class CounterDemo { public static void main(String args[]) { // create a counter, using the Counter() constructor int thisCounterValue; Counter thisCounter = new Counter(); // first work with thisCounter System.out.println("After Constructing thisCounter"); thisCounter.tellValue(); System.out.println("Increment thisCounter"); thisCounter.incrementCounter(); thisCounter.tellValue(); System.out.println("Increment thisCounter by 4"); thisCounter.incrementCounter(4); thisCounter.tellValue(); System.out.println("Decrement thisCounter"); thisCounter.decrementCounter(); thisCounter.tellValue(); System.out.println("Decrement thisCounter by 8"); thisCounter.decrementCounter(8); thisCounter.tellValue(); System.out.println("Call to GetValue for thisCounter"); thisCounterValue = thisCounter.getValue(); System.out.println("Current Value is: " + thisCounterValue); thisCounter.incrementCounter(); System.out.println("Call to toString, after incrementing "); System.out.println( thisCounter ); // we can create any number of separate counters // so try out another counter: anotherCounter, this one // uses the constructor Counter(int) Counter anotherCounter = new Counter(100); System.out.println("After constructing anotherCounter"); anotherCounter.tellValue(); // continue as before } }
Here is the output when the Java program is run:
 
After Constructing thisCounter
  From tellValue: I have value 0
Increment thisCounter
  From tellValue: I have value 1
Increment thisCounter by 4
  From tellValue: I have value 5
Decrement thisCounter
  From tellValue: I have value 4
Decrement thisCounter by 8
  From tellValue: I have value -4
Call to GetValue for thisCounter
Current Value is: -4
Call to toString, after incrementing 
  From toString: I have value -3
After constructing anotherCounter
  From tellValue: I have value 100


Revision date: 2001-01-27. (Please use ISO 8601, the International Standard.)